#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using sll = __int128;

template <typename T>
istream &operator>>(istream &in, pair<T, T> &a){
    in >> a.first >> a.second;
    return in;
}

template <typename T>
ostream &operator<<(ostream &out, pair<T, T> &a){
    out << a.first << ' ' << a.second;
    return out;
}

template <typename T>
istream &operator>>(istream &in, vector<T> &a){
    for(auto &x : a) in >> x;
    return in;
}

template <typename T>
ostream &operator<<(ostream &out, vector<T> &a){
    for(auto x : a) out << x << ' ';
    return out;
}

ll ans = 0;

void gen(int ind, vector<ll> mx, vector<ll> cnt, int n, int k, vector<ll> &a){
    if(ind == n){
        ll res = 0;
        for(int i = 1; i <= k; i++){
            if(cnt[i] == 0) return;
            res += mx[i];
        }
        ans = max(ans, res);
        return;
    }
    for (int i = 1; i <= k; i++) {
        ll prev = mx[i];
        cnt[i]++;
        mx[i] = max(mx[i], a[ind] + cnt[i]);
        gen(ind + 1, mx, cnt, n, k, a);
        mx[i] = prev;
        cnt[i]--;
    }
}

void solve(){
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    cin >> a;
    if(n <= 8){
        vector<ll> mx(k + 1), cnt(k + 1);
        if(k == n){
            for(int i = 0; i < n; i++){
                ans += a[i] + 1;
            }
            cout << ans;
            return;
        }
        gen(0, mx, cnt, n, k, a);
        cout << ans;
        return;
    }
    vector<pair<int, int>> ind(n);
    for(int i = 0; i < n; i++){
        ind[i] = {a[i], i};
    }
    sort(ind.begin(), ind.end(), greater<>());
    vector<int> mx;
    for(int i = 0; i < k - 1; i++){
        mx.push_back({ind[i].second});
    }
    sort(mx.begin(), mx.end());
//    cout << mx << '\n';
    ll x = 0;
    ll ans = 0;
    int l = 0;
    int cnt = 0;
    for(int i = 0; i < n; i++){
        if(l < mx.size() && i == mx[l]){
            ans += a[i] + 1;
            l++;
        } else {
            cnt++;
            x = max(x, a[i] + cnt);
        }
    }
//    cout << ans << ' ' << x << '\n';
    cout << ans + x;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test = 1;
//    cin >> test;
    while(test--){
        solve();
    }
}

