#include <bits/stdc++.h>

using namespace std;
#define int int64_t

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    if (k <= 2) {
        vector<int> p(n + 1);
        for (int i = 1; i <= n; i++) {
            p[i] = a[i] + i;
        }
        int mx = 0, pos = 0;
        for (int i = 1; i <= n; i++) {
            if (p[i] > mx) {
                mx = p[i];
                pos = i;
            }
        }
        if (pos == 1) {
                int h = 0;
            for (int j = pos + 1; j <= n; j++) {
                h = max(h, a[j] + j - pos);
            }
            cout << mx + h;
            return;
        }
        int y = 0;
        for (int i = 1; i < pos; i++) {
            y = max(y, a[i]);
        }
        y++;
        for (int j = pos + 1; j <= n; j++) {
            y = max(y, a[j] + j - pos + 1);
        }
        mx--;
        cout << mx + y;
        return;
    }
    set<pair<int,int> > st;
    int ans = 0;
    for (int i = 1; i <= k; i++) {
        st.insert({a[i], i});
        ans += (a[i] + 1);
    }
    for (int i = k + 1; i <= n; i++) {
        auto it = st.begin();
        pair<int,int> h = (*it);
        st.erase(it);
        if (a[i] < h.first) {
            h.first--;
            st.insert(h);
            continue;
        }
        ans += (a[i] - h.first + 1);
        h.first = a[i];
        st.insert(h);
    }
    cout << ans;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
