#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const ll mod = 1e9+7;

ll bpow(ll a, ll n) {
    ll res = 1;
    while (n) {
        res *= a;
        if (n & 1) a *= a;
        n /= 2;
    }
    return res;
}

const int N = 5e5+52;
const int M = 2e3+52;

//pair<ll,ll> a[N];
ll p[N];
ll a[N];

int main() {

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
//        a[i] = ;
    }
 ////    sort(a, a + n);
//    ll ans = 0;
    multiset<pair<ll,ll>> st;
    for (int i = 0; i < k; i++) {
        st.insert({a[i]+1,-1});
    }
    ll sum = 0;
    for (int i = k; i < n; i++) {
        sum++;
        pair<ll,ll> x = *st.begin();
        x.se = -x.se;
//        cout << x.se << " ";
        if (sum + a[i] + x.se >= x.fi) {
            st.erase(st.begin());
            st.insert({a[i] + sum + x.se, -(x.se + sum)});
            sum = 0;
        }
    }
//    cout << "\n";
    ll ans = 0;
    for (auto it : st) {
        ans += it.fi;
//        cout << it.fi << " ";
    }
//    cout << "\n";
    cout << ans << "\n";
    return 0;
}
