#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
const ll mod2=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=205000,M=10050;
int n,k,a[N];

struct node {
	int cnt;
	ll s;
	node() { cnt=0; s=0LL; }
};

struct ST {
	node nd[4*N];
	void modify(int p,int l,int r,int tl,int x) {
		nd[p].cnt+=x;
		nd[p].s+=(l*x);
		if (l==r) {
		} else {
			int md=l+r>>1;
			if (tl<=md) modify(p*2,l,md,tl,x);
			else modify(p*2+1,md+1,r,tl,x);
		}
	}
	int walk(int p,int l,int r,int k) {
		if (l==r) return l;
		int md=l+r>>1;
		if (nd[p*2].cnt>=k) return walk(p*2,l,md,k);
		else return walk(p*2+1,md+1,r,k-nd[p*2].cnt);
	}
	ll qry(int p,int l,int r,int tl,int tr) {
		if (l>r||l>tr||r<tl) return 0;
		if (tl<=l&&r<=tr) return nd[p].s;
		int md=l+r>>1;
		return qry(p*2,l,md,tl,tr)+qry(p*2+1,md+1,r,tl,tr);
	}
	int cc(int p,int l,int r,int tl,int tr) {
		if (l>r||l>tr||r<tl) return 0;
		if (tl<=l&&r<=tr) return nd[p].cnt;
		int md=l+r>>1;
		return cc(p*2,l,md,tl,tr)+cc(p*2+1,md+1,r,tl,tr);
	}
	ll get(int c) {
		ll x=walk(1,-M,M,c);
		return qry(1,-M,M,-M,x-1)+(ll)(c-cc(1,-M,M,-M,x-1))*x;
	}
} stl,str;

ll gao() {
	rep(i,0,n) {
		str.modify(1,-M,M,a[i],1);
	}
	ll ans=-1e18;
	int lc=0,rc=k;
	rep(i,0,n) {
		stl.modify(1,-M,M,-a[i],1);
		str.modify(1,-M,M,a[i],-1);
		if (i==n) continue;
		while (rc>str.nd[1].cnt||(rc>0&&-stl.walk(1,-M,M,lc+1)>str.walk(1,-M,M,rc-1))) {
			lc++; rc--;
		}
		//printf("[%d]: lc=%d rc=%d\n",i+1,lc,rc);
		//printf("%d %d\n",stl.walk(1,-M,M,lc),str.walk(1,-M,M,rc));
		if (lc==0) {
			ans=max(ans,-stl.get(1)-str.get(rc-1));
		} else if (rc==0) {
			ans=max(ans,-stl.get(lc-1)-str.get(1));
		} else {
			ans=max(ans,-stl.get(lc)-str.get(rc));
		}
	}
	assert(ans!=-1e18);
	rep(i,0,n) stl.modify(1,-M,M,-a[i],-1);
	return ans;
}

int main() {
	scanf("%d%d",&n,&k);
	rep(i,0,n) scanf("%d",a+i);
	ll ans=gao();
	reverse(a,a+n);
	ans=max(ans,gao());
	printf("%lld",ans);
}