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

//#pragma GCC optimize("O3")
//#pragma GCC target("avx2,bmi")

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define endl "\n"
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<int, int>> otr;
    int l = 0;
    int coun = 0;
    int ans = 0;
    while (l < n) {
        int last = l;
        int mx = a[l] + 1;
        l++;
        int c = 2;
        while (l < n && a[l] > a[l - 1]) {
            mx = a[l] + c;
            l++;
            c++;
        }
        ans += mx;
        coun++;
        otr.push_back({last, l - 1});
    }
    set<pair<int, int>> st;
    for (int i = 0; i < coun; i++) {
        if (otr[i].second - otr[i].first > 0) {
            st.insert({-(a[otr[i].second - 1]), i});
        }
    }
    while (coun < k) {
        auto [last, id] = *st.begin();
        last = -last;
        st.erase(st.begin());
        if (otr[id].second - otr[id].first == 0) continue;
        ans -= a[otr[id].second] + (otr[id].second - otr[id].first + 1);
        ans += last + (otr[id].second - otr[id].first);
        ans += a[otr[id].second] + 1;
        otr[id].second--;
        coun++;
        st.insert({id, a[otr[id].second] + (otr[id].second - otr[id].first + 1)});
    }
    cout << ans;
}
