#include "bits/stdc++.h"

using namespace std;
#define ll long long
#define ff first
#define ss second

void solve() {
    int n, k;
    cin >> n >> k;

    vector<int> v(n);
    multiset<int> s;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    vector<int> v1;
    for (int i = 0; i < k; i++)s.insert(0);
    for (int i = n - 1; i >= 0; i--) {
        int fi = *s.begin();
        int en = *s.rbegin();
        if (v[i] < en) {
            v1.push_back(v[i]);
            s.erase(s.find(en));
            s.insert(en + 1);
        } else {
            if (fi < v[i]) {
                s.erase(s.begin());
                s.insert(v[i]);
            } else {
                v1.push_back(v[i]);
            }
        }
    }
    int cnt3 = 0;
    for (auto i: s)if (i == 0)cnt3++;
    std::sort(v1.rbegin(), v1.rend());
    ll ans = k;
    for (int i = 0; i < cnt3; i++) {
        ans -= 1;
        ans += v1[i];
    }
    for (auto i: s)ans += i;
    cout << ans;


}

signed main() {
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}