#include <bits/stdc++.h>
using namespace std;
using ll = int;
using pll = pair<ll,ll>;
/*struct segtree{
    ll n;
    vector<ll> t;
    vector<ll> add;
    segtree(vector<ll> &zxc){
        n = zxc.size();
        t.resize(4 * n);
        add.resize(4 * n);
        build(0, 0, n-1,zxc);
    }
    ll F(ll a, ll b){
        return max(a,b);
    }
    void build (ll v,ll l, ll r,vector<ll> &a){
        if(l ==  r){
            t[v] = a[l];
            return;
        }
        ll m = (l + r)/2;
        build(v * 2 + 1,l,m,a);
        build(v * 2 + 2,m + 1,r,a);
        t[v] = F(t[v * 2 + 1], t[v * 2 + 2]);
    }
    void push (ll v){
        if(2 * v + 2 >= 4 * n){
            return;
        }
        t[v] = add[v];
        add[2 * v + 1] = add[v];

    }
};*/
/*vector<bool> used;
vector<vector<ll>> g;
void dfs(ll v){
    used[v] = true;
    for(auto u : g[v]){
        if(!used[u]){
            dfs(u);
        }
    }
}*/
//const ll inf = 1e17;
int main() {
    ll n,k;
    cin >> n >> k;
    vector<pll> a(n);
    vector<ll> fa(n);
    for(int i = 0; i < n; i++){
        cin >> a[i].first;
        fa[i] = a[i].first;
        //a[i].first += i;
        a[i].second = -i;
    }
    if(k == 1){
        ll mx = 0;
        for(int i = 0; i < n; i++){
            mx = max(mx,fa[i] + i + 1);
        }
        cout << mx;
        return 0;
    }
    sort(a.begin(),a.end());
    ll zxc = 0;
    vector<bool> used(n,true);
    ll mx = 0;
    for(auto v : a){
        if(zxc == n - k){
            used[v.second * -1] = false;
            mx = v.first;
            break;
        }
        zxc++;
        used[v.second * -1] = false;
    }
    vector<vector<ll>> ans(k);
    mx--;
    ll tec = 0;
    int i = 0;
    for(; i < n; i++){
        ans[min(tec,k-1)].push_back(fa[i]);
        if(used[i]){
            tec++;
            if(tec == k-1){
                break;
            }
        }
    }
    i++;
    ll ni = i;
    while(i < n){
        mx = max(mx,fa[i] + i - ni + 1);
        i++;
    }
    ll anss = mx;
    for(int i = 0; i < k - 1; i++){
        ll mx = 0;
        for(int j = 0; j < ans[i].size(); j++){
            mx = max(mx,ans[i][j] + j + 1);
        }
        anss +=mx;
    }
    cout << anss;
}
