//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>

using ll = long long;
using ld = long double;

#define all(a) a.begin(), a.end()
#define pb push_back

using namespace std;
using namespace literals;
//using namespace __gnu_pbds;

//using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//gp_hash_table<ll, ll> s;

mt19937 rnd;

void solve();
void precalc();

int main() {
#ifdef LOCAL:
    freopen("INPUT.TXT", "r", stdin);
    freopen("OUTPUT.TXT", "w", stdout);
    const auto start = chrono::steady_clock::now();
#else
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    precalc();
    bool cfmode = false;
    if(cfmode) {
        ll t;
        cin >> t;
        while(t--) {
            solve();
        }
    } else {
        solve();
    }
#ifdef LOCAL:
    const auto end = chrono::steady_clock::now();
    cout << "\n";
    cout << fixed << setprecision(10) << "xxxTIMExxx: " << (end - start) / 1ms;
#endif
}

void precalc() {

}

void solve() {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    set<pair<ll, ll>> s;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        s.insert({a[i], i});
    }
    ll last = 0;
    multiset<ll> answers;
    vector<bool> was(n, false);
    for(int i = 0; i < k; i++) {
        ll x = s.rbegin()->second;
        was[x] = true;
        answers.insert(a[x] + 1);
        s.erase(*s.rbegin());
        last = max(x, last);
    }
    set<pair<ll, ll>> s1;
    ll ans = 0;
    for(int i = 0; i < last; i++) {
        if(!was[i]) {
            ans++;
        }
    }
    for(int i = last + 1; i < n; i++) {
        s1.insert({a[i] + i + 1, i});
    }
    while(s1.size() > 0 && (s1.rbegin()->first - last > *answers.begin())) {
        if(s1.rbegin()->second < last) {
            s1.erase(*s1.rbegin());
            continue;
        }
        answers.erase(answers.find(*answers.begin()));
        answers.insert(s1.rbegin()->first - last);
        last = max(last, s1.rbegin()->second);
        s1.erase(*s1.rbegin());
    }
    for(auto i : answers) {
        ans += i;
    }
    cout << ans;
}
