#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, k;
vector<int> a;
vector<pair<int, int>> b;

struct dsu {
    vector<int> d;

    dsu() {
        d.resize(n);
        for (int i = 0; i < n; i++)
            d[i] = i;
    }

    int find(int u) {
        if (d[u] == u)
            return u;
        return d[u] = find(d[u]);
    }

    void merge(int u, int v) {
        u = find(u), v = find(v);
        d[u] = v;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k;
    a.resize(n);
    for (auto &x: a) cin >> x;
    for (int i = 0; i < n; i++)
        b.emplace_back(a[i], i);
    sort(b.begin(), b.end());
    dsu ds;
    for (int _ = 0; _ < n - k; _++) {
        if (b[_].second == n - 1) {
            k--;
            continue;
        }
        ds.merge(b[_].second, b[_].second + 1);
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
        ds.find(i);
        int cur = a[i] + 1;
        int j = i;
        while (i < n and ds.d[i] != i)
            cur = max(cur, a[i++] + (i - j));
        cur = max(cur, a[i] + (i - j + 1));
        res += cur;
    }
    cout << res << '\n';
    return 0;
}
