#include <bits/stdc++.h>

using namespace std;

struct node {
	long long sum;
	long long cnt;
};

const int N = 2e5+10;
const int nn = 2e5;

long long n, k;
long long a[N];
node st[4*N];
long long pref_mn[N], suf_mx[N];

long long Inv(long long x) { return 200000-x; }

void update(int index, int l, int r, long long x, long long val) {
	if(l > r || r < x || x < l) return;
	if(l == r) {
		st[index].cnt += val;
		st[index].sum += x * val;
		return;
	}
	int mid = (l + r) >> 1;
	update(2*index, l, mid, x, val);
	update(2*index+1, mid+1, r, x, val);
	st[index].cnt = st[2*index].cnt + st[2*index+1].cnt;
	st[index].sum = st[2*index].sum + st[2*index+1].sum;
}

long long brojac;
pair<long long, long long> ind;
void walk(long long index, long long l, long long r) {
	if(l == r) {
		ind.first = l;
		ind.second = index;
		return;
	}
	int mid = (l + r) >> 1;
	if(brojac - st[2*index].cnt < k) walk(2*index, l, mid);
	else {
		brojac -= st[2*index].cnt;
		walk(2*index+1, mid+1, r);
	}
}

node get(int index, int l, int r, int L, int R) {
	if(l > r || r < L || R < l) return {0, 0};
	if(L <= l && r <= R) return st[index];
	int mid = (l + r) >> 1;
	node nd1 = get(2*index, l, mid, L, R);
	node nd2 = get(2*index+1, mid+1, r, L, R);
	return {nd1.sum + nd2.sum, nd1.cnt + nd2.cnt};
}

long long solve() {
    for(int i = 0; i <= 4*N; i++) st[i].sum = st[i].cnt = 0;
    pref_mn[0] = 1e18;
    suf_mx[n] = a[n];
    for(int i = 1; i <= n; i++) pref_mn[i] = min(pref_mn[i-1], a[i]);
	for(int i = n-1; i >= 1; i--) suf_mx[i] = max(suf_mx[i+1], a[i]);
    for(int i = 1; i <= n; i++) update(1, 0, nn, a[i], 1);
    long long ret = -1e18;
    for(int i = 1; i <= n-1; i++) {
    	update(1, 0, nn, a[i], -1);
    	update(1, 0, nn, Inv(a[i]), 1);
    	brojac = st[1].cnt;
		walk(1, 0LL, nn);
    	node nd = get(1, 0, nn, ind.first, nn);
    	long long rez = nd.sum - nd.cnt * 100000;
    	if(ind.first != 100000) rez -= (brojac - k) * (st[ind.second].sum - 100000);
    	// proveriti
    	if(Inv(pref_mn[i]) < ind.first) rez += Inv(pref_mn[i]) - ind.first;
    	else if(suf_mx[i+1] < ind.first) rez += suf_mx[i+1] - ind.first;
    	// ovo
    	ret = max(ret, rez);
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
    	cin >> a[i];
    	a[i] += 100000;
    }
    long long rez = -1e18;
    rez = max(rez, solve());
    for(int l = 1, r = n; l <= r; l++, r--) swap(a[l], a[r]);
    rez = max(rez, solve());
    cout << rez << "\n";
    return 0;
}