#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll N = 5e5 + 5;

ll a[N];
void solve() {
    ll n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    multiset<ll> q;
    for (int i = n; i > n - k ; --i) {
        q.insert(a[i] + 1);
    }
    for (int i = n - k; i > 0; --i) {
        auto u = q.lower_bound(a[i]);
        if (u == q.begin()) {
            q.erase(q.begin());
            q.insert(a[i] + 1);
        }
        --u;
        ll o = *u + 1;
        q.erase(u);
        q.insert(o);
    }
    ll ans = 0;
    for(ll u: q) {
        ans += u;
    }
    cout << ans;
}

int main() {
    int test = 1;
    // cin >> test;
    while (test--) {
        solve();
    }
}
