

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 12;

#define int long long

mt19937_64 rng (time (0));
int n, k, a[N], used[N];
int ans = 0, sum = 0;
signed main() {
//#ifdef local
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
//#endif // local
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    int all = 0;
    for (int i = 1; i <= n; ++i) cin >> a[i], all += a[i];
    set<pair<int,int>> stL, stR;
    vector<pair<int,int>> A;
    for (int i = 2; i <= n; ++i) A.push_back({a[i], i});
    sort (begin(A), end (A));
    int sumR = 0, sumL = 0;
    for (int i = 0; i < n - k; ++i) stR.insert (A[i]), sumR += A[i].first;
    set<pair<int,int>> R;
    set<pair<int,int>> can_be;
    for (int i = 2; i <= n; ++i) R.insert ({a[i], i});
    for (int posmx = 1; posmx <= n; ++posmx) {
        ans = max (ans, (k - 1) + all - (sumL + sumR) + (int)stL.size() + 1);
        if (posmx + 1 <= n) {
            R.erase ({a[posmx + 1], posmx + 1});
            if (stR.find ({a[posmx] + 1, posmx + 1}) != stR.end()) {
                stR.erase ({a[posmx] + 1, posmx + 1});
                sumR -= a[posmx];
                if ((int)R.size()) stR.insert (*R.begin()), sumR += (*R.begin()).first;

            }
            can_be.insert ({a[posmx], posmx});
            while ((int)can_be.size()) {
                pair<int,int> a_psmx = *can_be.begin();
                if ((int)stL.size() + (int)stR.size() < n - k) {
                    sumL += a_psmx.first;
                    stL.insert (a_psmx);
                    can_be.erase (can_be.begin());
                    continue;
                }
                int delta1 = -1e18, delta2 = -1e18;
                if ((int)stR.size() && (*stR.rbegin()).first >= a_psmx.first) {
                    delta1 = (*stR.rbegin()).first - a_psmx.first + 1;
                }
                if ((int)stL.size() && (*stL.rbegin()).first > a_psmx.first) {
                    delta2 = (*stL.rbegin()).first - a_psmx.first;
                }
                if (delta1 > delta2) {
                    sumR -= (*stR.rbegin()).first;
                    sumL += a_psmx.first;
                    stR.erase (--stR.end());
                    stL.insert (a_psmx);
                    can_be.erase (can_be.begin());
                } else if (delta2 > delta1) {
                    sumL -= (*stL.rbegin()).first;
                    sumL += a_psmx.first;
                    stL.erase (--stL.end());
                    stL.insert (a_psmx);
                    can_be.erase(can_be.begin());
                }
                else break;
            }
        }
    }


    cout << ans << '\n';
}
