#include <bits/extc++.h>
using namespace std;
vector <deque <int > > q;
signed main(){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin>>n>>k;
    q.resize(k);
    vector <pair <int, int> > a(n), b;
    set <int> indexes;
    for (int i = 0; i < n; i++){
        cin>>a[i].first;
        a[i].second = i;
        indexes.insert(i);
    }
    b = a;
    sort(b.rbegin(), b.rend());
    vector <bool> used(n, false);
    
    used[n - 1] = true;
    for (int i = 0; i < n; i++){
        if (b[i].second == n - 1){
            q[k - 1].push_front(b[i].second);
            indexes.erase(b[i].second);
        }
    }
    int cnt = 0;
    for (int i = 0; i < n && cnt + 1 < k; i++){
        if (!used[i]){
            q[cnt].push_front(b[i].second);
            cnt++;
            used[b[i].second] = true;
            indexes.erase(b[i].second);
        }
    }
    cnt = 0;
    while (!indexes.empty()){
        int x = *indexes.begin();
        if(x < q[cnt].back()){
            q[cnt].push_front(x);
            indexes.erase(x);
        }
        else{
            cnt++;
        }
    }
    int ans = 0;
    for (int i = 0; i < k; i++){
        int tek = 0;
        for (int j = 0; j < q[i].size(); j++){
            tek = max(tek, a[q[i][j]].first + j + 1);
        }
        ans += tek;
    }
    cout<<ans<<endl;
    
    
}