//#define _GLIBCXX_DEBUG
//#define _GLIBCXX_DEBUG_PEDANTIC

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define OPTIMIZETHISSHIT ios::sync_with_stdio(false); cin.tie(nullptr);
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937_64 rng(0);

int rand(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

int get_ans(vector<int>& a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++) {
        ans = max(ans, a[i] + i);
    }
    return ans;
}

int solve(int n, int k, vector<int> a) {
    for (int i = 0; i < n; i++) {
        a[i]++;
    }
    vector<pair<int, int>> b;
    for (int i = 0; i < n; i++) {
        b.pb({a[i], i});
    }
    sort(rall(b));

    set<int> ind;
    int total = 0;
    for (int i = 0; i < k; i++) {
        ind.insert(b[i].se);
        total += b[i].fi;
    }
    int max_ans = 0;

    vector<int> pref_max(n);
    vector<int> pref_sz(n);
    int sz = 0;
    for (int i = 0; i < n; i++) {
        if (i == 0) {
            if (ind.find(i) == ind.end()) {
                pref_max[i] = a[i];
            } else {
                sz++;
            }
        } else {
            pref_max[i] = pref_max[i - 1];
            if (ind.find(i) == ind.end()) {
                pref_max[i] = max(pref_max[i], a[i] + i - sz);
            } else {
                sz++;
            }
        }
        pref_sz[i] = sz;
    }

    vector<int> suff_max(n);
    for (int i = n - 1; i >= 0; i--) {
        if (ind.find(i) == ind.end()) {
            if (i != n - 1) {
                suff_max[i] = suff_max[i + 1] + 1;
            }
            suff_max[i] = max(suff_max[i], a[i]);
        } else {
            if (i != n - 1) {
                suff_max[i] = suff_max[i + 1];
            }
        }
    }

    auto it = ind.begin();
    for (int i = 0; i < k; i++) {
        int j = *it;
        int res = total - a[j];

        int add = a[j] + j;

        if (j - 1 >= 0) {
            add -= pref_sz[j - 1];
            add = max(add, pref_max[j - 1]);
        }
        if (j + 1 < n && suff_max[j + 1] != 0) {
            int v = suff_max[j + 1] + j + 1;
            if (j - 1 >= 0) {
                v -= pref_sz[j - 1];
            }
            add = max(add, v);
        }
        max_ans = max(max_ans, res + add);
        it++;
    }
    return max_ans;
}

int solve2(int n, int k, vector<int> a) {
    for (int i = 0; i < n; i++) {
        a[i]++;
    }
    vector<pair<int, int>> b;
    for (int i = 0; i < n; i++) {
        b.pb({a[i], i});
    }
    sort(rall(b));
    vector<int> ind;
    int total = 0;
    for (int i = 0; i < k; i++) {
        ind.pb(b[i].se);
        total += b[i].fi;
    }
    sort(all(ind));
    int max_ans = 0;
    for (int i = 0; i < k; i++) {
        int res = total - a[ind[i]];
        int add = 0;
        int sz = 0;
        for (int ii= 0, j = 0; ii < n; ii++) {
            if (j < k && ind[j] == ii) {
                j++;
                if (j - 1 != i) {
                    continue;
                }
            }
            add = max(add, a[ii] + sz);
            sz++;
        }
        max_ans = max(max_ans, res + add);
        cout << ind[i] << ' ' << res + add << endl;
    }
    return max_ans;
}

//#define STRESS
signed main() {
    OPTIMIZETHISSHIT
#ifndef STRESS
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    cout << solve(n, k, a) << endl;
#else
    int ii = 0;
    while (true) {
        int n, k;
        n = rand(1, 5);
        k = rand(1, n);
        vector<int> a(n);
        for (int i = 0; i < n; i++) a[i] = rand(1, 5);
        int f1 = solve(n, k, a);
        int f2 = solve2(n, k, a);
        if (f1 != f2) {
            cout << "brokd" << endl;
            cout << n << ' ' << k << endl;
            for (int i: a) {
                cout << i << ' ';
            }
            cout << endl;
            cout << f1 << ' ' << f2 << endl;
            break;
        }
        cout << "test " << ++ii << endl;
    }


#endif

}