//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
//#pragma GCC target("avx2")
using namespace std;
using ll = int64_t;

void solve() {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (auto &i : a) {
        cin >> i;
    }
    vector<pair<ll, ll>> b(n);
    for (ll i = 0; i < n; ++i) {
        b[i] = pair(a[i], i);
    }
    ll ans = 0;
    vector<bool> used(n, 0);
    sort(b.begin(), b.end(), [&](pair<ll, ll>& a, pair<ll, ll>& b) {
        if (a.first > b.first) {
            return true;
        } else if (a.first == b.first) {
            return a.second < b.second;
        }
        return false;
    });

    ll ind_r = 0;

    for (ll i = 0; i + 1 < k; ++i) {
        ans += b[i].first + 1;
        used[b[i].second] = 1;
        ind_r = max(b[i].second, ind_r + 1);
    }

    vector<ll> pref_sum(n + 1, 0);
    for (ll i = 1; i <= n; ++i) {
        pref_sum[i] = pref_sum[i - 1] + !used[i - 1];
    }

    ll res = ans;

    ll mx = 0, cnt = 0;
    for (ll i = 0; i < n; ++i) {
        if (!used[i]) {
            ++cnt;
            mx = max(a[i] + cnt, mx);
            ll tmp = mx + ans;
            if (ind_r > i) {
                tmp += pref_sum[ind_r] - pref_sum[i + 1];
            }
            res = max(res, tmp);
        }
    }
    
    cout << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t = 1;
//    cin >> t;
    while (t --> 0) {
        solve();
    }

    return 0;
}
