#include <bits/extc++.h>
using namespace std;
#define int long long
vector <set <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());
    int cnt = 0;
    for (int i = 0; i < k - 1; i++){
        q[i].insert(b[i].second);
        indexes.erase(b[i].second);
    }
    if (indexes.find(n - 1) != indexes.end()){
        q[k - 1].insert(n - 1);
        indexes.erase(n - 1);
    }
    else{
        q[k - 1].insert(b[k - 1].second);
        indexes.erase(b[k - 1].second);
    }
    for (int i = 0; i < k; i++){
        int x = *indexes.begin();
        while (!indexes.empty() && x < *q[i].rbegin()){
            q[i].insert(x);
            indexes.erase(x);
            if (!indexes.empty()){
               x = *indexes.begin();  
            }
           
        }
    }
    int ans = 0;
    for (int i = 0; i < k; i++){
        int tek = 0, cnt = 1;
        for (auto& pos: q[i]){
            tek = max(tek, a[pos].first + cnt);
            cnt++;
        }
        ans += tek;
    }
    cout<<ans<<endl;
    
    
}