#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define int long long
// #define ll __int128
using namespace std;
// using namespace __gnu_pbds;
// #define unordered_map gp_hash_table
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

const int INF = 1e13 + 7;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> t(n);
    for (int &i : t) {
        cin >> i;
    }
    vector<pair<int, int>> st;
    for (int i = 0; i < n; i++) {
        st.push_back({t[i] + i + 1, -i});
    }
    sort(st.rbegin(), st.rend());
    int delta = 0;
    multiset<int> just;
    int res = 0;
    int last_used = -1;
    int st_used = 0;
    while (k--) {
        int a, b;
        int a_ind;
        while (st_used < n && -st[st_used].second <= last_used) {
            st_used++;
        }
        if (st_used >= n) {
            a = -INF;
        } else {
            a = st[st_used].first + delta;
            a_ind = -st[st_used].second;
        }
        if (just.empty()) {
            b = -INF;
        } else {
            b = *just.rbegin();
        }
        if (a >= b) {
            int d_delta = a_ind - last_used;
            for (int i = last_used + 1; i <= a_ind; i++) {
                just.insert(t[i]);
            }
            just.extract(t[a_ind]);
            res += a;
            delta -= d_delta;
            last_used = a_ind;
            st_used++;
        } else {
            res += b;
            just.extract(b);
        }
    }
    cout << res << "\n";
}

signed main() {
    // cout << fixed << setprecision(20);
    #ifdef LOCAL
    freopen("C:\\work\\input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    #endif
    solve();
}