#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define vi vector<ll>
#define vii vector<vi>
#define vip vector<pair<ll, ll>>
#define viip vector<vip>
#define endl '\n'
#define pb push_back
#define all(a) a.begin(), a.end()

void solve() {
    ll n, k;
    cin >> n >> k;
    set<pair<ll, ll>> s;
    vi a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (ll i = n - 1; i >= 0; i--) {
        if (s.size() < k) {
            s.insert({a[i], i});
        } else {
            auto [f, id] = *s.begin();
            if (a[i] <= f) {
                s.erase(s.begin());
                s.insert({f + 1, id});
            } else {
                s.erase(s.begin());
                if (!s.empty()) {
                    ll d1 = -1, id1;
                    for (auto it = --s.end();; it--) {
                        ll fl = 0;
                        if (it == s.begin()) {
                            fl = 1;
                        }
                        auto [d, j] = *it;
                        if (j > id) {
                            d1 = d;
                            id1 = j;
                            break;
                        }
                        if (fl)
                            break;
                    }
                    if (d1 != -1) {
                        s.erase({d1, id1});
                        if (f > d1 + 1) {
                            s.insert({max(f, d1 + 1), id});
                        } else {
                            s.insert({max(f, d1 + 1), id1});
                        }
                    }
                }
                s.insert({a[i], i});
            }
        }
    }
    ll ans = 0;
    for (auto [x, y] : s) {
        ans += x;
    }
    cout << ans + k;
}

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
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}
