#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (ll &i : a) cin >> i;
    vector<pair<ll, ll>> s;
    for (ll i = 0; i < n; i++) {
        s.emplace_back(a[i], i);
    }
    sort(s.begin(), s.end(), [] (pair<ll, ll> &A, pair<ll, ll> &B) {
        if (A.first != B.first) return A.first > B.first;
        return A.second < B.second;
    });
    set<ll> b;
    for (ll i = 0; i < k - 1; i++) {
        b.insert(s[i].second);
    }
    ll prev = -1, ans = 0;
    for (ll i : b) {
        //cout << i << ' ' << prev << '\n';
        ans += a[i] + (i - prev);
        prev = i;
    }
    if (prev != n - 1) {
        ll res = -1;
        for (ll i = prev + 1; i < n; i++) {
            res = max(res, a[i] + i - prev);
        }
        ans += res;
    }
    else {
        ll res = -1;
        for (ll i = 0; i < n; i++) {
            if (b.find(i) == b.end() && a[i] > res) {
                res = a[i];
            }
        }
        ans += res;
    }
    cout << ans << '\n';
}
