#include <iostream>
#include <algorithm>
#define forn(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const int oo = 2e5+7;

int n, k, a[oo];
bool used[oo]; // was it popped
bool insum[oo]; // is it in the sum
int idx[oo]; // idx[i] = j, b[j].second = i
ll top;
pair<int, int> b[oo];
int xj;
int next() {
    while (xj < n) {
        if (!used[xj] && !insum[xj]) return xj;
        xj++;
    }
    throw 0;
}
void pop(int x) {
    x = idx[x];
    used[x] = true;
    if (insum[x]) {
        insum[x] = false;
        top -= b[x].first;

        int nx = next();
        insum[nx] = true;
        top += b[nx].first;
    }
}

void init() {
    top = 0;
    xj = 0;
    forn(i, n) used[i] = insum[i] = false;
    forn(i, k - 1) top += b[i].first, insum[i] = true;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    forn(i, n) cin >> a[i];
    
    forn(i, n) b[i] = {a[i], i};
    sort(b, b + n, greater<pair<int, int>>());
    forn(i, n) idx[b[i].second] = i;
    ll ans = -1;

    init();
    for (int i = 0; i < n - 1; i++) {
        try { pop(i); } catch (...) { break; }
        ans = max(ans, -a[i] + top);
    }

    init();
    for (int i = n-1; i > 0; i--) {
        try { pop(i); } catch (...) { break; }
        ans = max(ans, -a[i] + top);
    }

    cout << ans << endl;

    #ifdef CHUNGUS
    system("pause");
    #endif
}