//#define _GLIBCXX_DEBUG
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")

#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
using ll = long long;
#define int ll
using namespace std;
//using namespace __gnu_pbds;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 mt(52);
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(auto &el : a) cin >> el;
    if(k == 1){
        int ma = 0;
        for(int i=0; i<n; ++i) ma = max(a[i]+i+1, ma);
        cout << ma;
        return;
    }
    vector<pair<int, int>> p;
    for(int i=0; i<n; ++i) p.emplace_back(a[i], i);
    sort(p.rbegin(), p.rend());
    set<int> ids;
    for(int i=0; i<n; ++i) if(a[i] < p[k-1].first) ids.insert(i);
    vector<vector<int>> ars;
    for(int i=0; i<k-1; ++i){
        vector<int> cur;
        while(!ids.empty() && *ids.begin() < p[i].second){
            cur.push_back(*ids.begin());
            ids.erase(ids.begin());
        }
        cur.push_back(p[i].second);
        ars.push_back(cur);
    }
    set<int> nu; for(int i=0; i<n; ++i) nu.insert(i);
    for(auto &el : ars) for(auto &e : el) nu.erase(e);
    ars.push_back({});
    for(auto &i : nu) ars.back().push_back(i);
    int ans = 0;
    for(auto &el : ars){
        int ma = 0;
        for(int i=0; i<el.size(); ++i) ma = max(ma, a[el[i]]+i+1);
        ans += ma;
    }
    cout << ans;
}


signed main(){
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    bool cfmode = !(42);
    if(cfmode){
        int t;
        cin >> t;
        while(t--){
            solve();
            cout << '\n';
        }
    } else solve();
}