#include <bits/stdc++.h>


using namespace std;


using ll = long long;


template<typename T>
ostream & operator <<(ostream &os, const vector<T> &a) {
    for (T el : a) {
        os << el << " ";
    }
    os << '\n';
    return os;
}


#include <bits/stdc++.h>


using namespace std;


using ll = long long;


const ll inf = 1e18 + 7;


struct segTree {
    vector<pair<ll, int>> T;
    segTree(int maxn, const vector<ll> &a);
    void build(int v, int tl, int tr, const vector<ll> &a);
    void upd(int v, int tl, int tr, int pos, ll x);
    pair<ll, int> get(int v, int tl, int tr, int ql, int qr);
};


segTree::segTree(int maxn, const vector<ll> &a) {
    T.resize(maxn);
    build(0, 0, int(a.size()), a);
}


void segTree::build(int v, int tl, int tr, const vector<ll> &a) {
    if (tl + 1 == tr) {
        T.at(v) = {a.at(tl), tl};
        return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2 + 1, tl, tm, a);
    build(v * 2 + 2, tm, tr, a);
    T.at(v) = min(T.at(v * 2 + 1), T.at(v * 2 + 2));
}


void segTree::upd(int v, int tl, int tr, int pos, ll x) {
    if (tl + 1 == tr) {
        T.at(v) = {x, tl};
        return;
    }
    int tm = (tl + tr) / 2;
    if (pos < tm) {
        upd(v * 2 + 1, tl, tm, pos, x);
    } else {
        upd(v * 2 + 2, tm, tr, pos, x);
    }
    T.at(v) = min(T.at(v * 2 + 1), T.at(v * 2 + 2));
}


pair<ll, int> segTree::get(int v, int tl, int tr, int ql, int qr) {
    if (tl == ql && tr == qr) {
        return T.at(v);
    }
    int tm = (tl + tr) / 2;
    if (qr <= tm) {
        return get(v * 2 + 1, tl, tm, ql, qr);
    } else if (ql >= tm) {
        return get(v * 2 + 2, tm, tr, ql, qr);
    }
    return min(get(v * 2 + 1, tl, tm, ql, tm), get(v * 2 + 2, tm, tr, tm, qr));
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    vector<ll> en(k, 0);
    vector<ll> cnt(k, 0);
    for (int i = 0; i < n; i++) {
        cin >> a.at(i);
    }
    ll ans = 0;
    int buf = 0;
    int mnpos = 0;
    segTree T(4 * k, en);
    for (int i = 0; i < n; i++) {
        int pos = -1;
        auto res = T.get(0, 0, k, 0, k);
        if (res.first - 1 - buf <= a.at(i)) {
            pos = res.second;
        }
        if (pos != -1) {
            en.at(pos) = a.at(i);
            cnt.at(pos) += buf + 1;
            T.upd(0, 0, k, pos, a.at(i));
            buf = 0;
        } else {
            buf++;
        }
    }
    for (int i = 0; i < k; i++) {
        ans += en.at(i);
        ans += cnt.at(i);
    }
    cout << ans;
    return 0;
}
