#include <bits/stdc++.h>
#define int long long

using ll = long long;
using ld = long double;
using namespace std;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, k;
    cin >> n >> k;

    vector<pair<int, int>> a(n);
    vector<pair<pair<int, int>, int>> val(n);
    set<pair<pair<int, int>, int>> s;

    int sum = n;
    for(int i = 1; i <= n; ++i){
        int x;
        cin >> x;
        if(i == n)
            val[i - 1] = {{x + 1, i}, 0};
        else
            val[i - 1] = {{x, i}, 1};
        s.insert(val[i - 1]);
        a[i - 1] = {i - 2, i};
        sum += x;
    }

    for(int i = 0; i < n - k; ++i){
        pair<pair<int, int>, int> x = *(s.begin());
        s.erase(x);
        sum -= x.first.first;
        if(x.second == 0){
            int prev = a[x.first.second - 1].first;
            pair<pair<int, int>, int> y = val[prev];
            a[prev].second = n;
            s.erase(y);
            y.first.first += y.second;
            y.second = 0;
            val[prev] = y;
            s.insert(y);
        } else{
            int prev = a[x.first.second - 1].first;
            int next = a[x.first.second - 1].second;
            auto y = val[next];
            if(prev != -1)
                a[prev].second = next;
            a[next].first = prev;
            s.erase(y);
            if(val[next].second != 0)
                y.second += x.second;
            else
                y.first.first += x.second;
            val[next] = y;
            s.insert(y);
        }
    }
    cout << sum << endl;

    return 0;
}
