//#pragma GCC optimize("unroll-loops","Ofast")
//#pragma GCC target("sse","sse2","avx","avx2")
#include <bits/stdc++.h>

#define int long long
#define F first
#define S second
using namespace std;

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int ans = 0;
    multiset<int> ms;
    for (int i = 0; i < n; ++i) {
        ms.insert(a[i] + 1);
    }
    multiset<int> bestk_1;
    int sumk_1 = 0;
    for (int i = 0; i < k - 1; ++i) {
        auto it = --ms.end();
        bestk_1.insert(*it);
        sumk_1 += *it;
        ms.erase(--ms.end());
    }
    for (int i = 0; i < n; ++i) {
        int toer = a[i] + 1;
        if (ms.find(toer) != ms.end()) {
            ms.erase(ms.find(toer));
        } else {
            sumk_1 -= toer;
            bestk_1.erase(bestk_1.find(toer));
            auto newbest = --ms.end();
            sumk_1 += *newbest;
            bestk_1.insert(*newbest);
            ms.erase(--ms.end());
        }
        if (i != 0) {
            int add = a[i - 1];
            if (add <= *bestk_1.begin()) {
                ms.insert(add);
            } else {
                sumk_1 -= *bestk_1.begin();
                ms.insert(*bestk_1.begin());
                bestk_1.erase(bestk_1.begin());
                bestk_1.insert(add);
                sumk_1 += add;
            }
        }
        ans = max(ans, a[i] + (i + 1) + sumk_1);
    }
    cout << ans;
    return 0;
}
//не забудь сохранять коды!!!