#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<queue>

using namespace std;


struct stupid {
	int64_t n;
	vector<int64_t> a;
	stupid(int64_t _n) {
		n = _n;
		a.resize(n);
	}
	pair<int64_t, int64_t> mx(int64_t l, int64_t r) {
		int64_t pos = l;
		for (int64_t i = l; i <= r; ++i) {
			if (a[i] > a[pos]) {
				pos = i;
			}
		}
		return { a[pos],pos };
	}
	void add(int64_t l, int64_t r, int64_t val) {
		for (int64_t i = l; i <= r; ++i) {
			a[i] += val;
		}
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int64_t n, k;
	cin >> n >> k;
	vector<int64_t> a(n);
	for (int64_t i = 0; i < n; ++i) {
		cin >> a[i];
	}
	vector<int64_t> p(n);
	sort(p.begin(), p.end(), [&](auto l, auto r) {return a[l] > a[r]; });
	int64_t res = 0;
	vector<pair<int64_t, int64_t>> seg = { {0, 0} };
	stupid x(n);
	for (int64_t i = 0; i < n; ++i) {
		if (i >= seg.back().first) {
			x.add(i, i, a[i] + i - seg.back().first + 1);
		}
		else {
			x.add(i, i, a[i]);
		}
	}
	for (int64_t c = 0; c < k; ++c) {
		int64_t i = x.mx(0, n - 1).second;
		p[i] = 1;
		x.add(i, i, -1e9);
		if (i >= seg.back().first) {
			int ps = seg.back().first;
			seg.emplace_back(i + 1, a[i] + i - seg.back().first + 1);
			for (int i = ps; i < seg.back().first; ++i) {
				if (!p[i]) {
					x.add(i, i, -x.mx(i,i).first);
					x.add(i, i, a[i]);
				}
			}
			for (int i = seg.back().first; i < n; ++i) {
				x.add(i, i, -(seg.back().first - ps));
			}
			res += seg.back().second;
		}
		else {
			int64_t l = 0, r = seg.size();
			while (r - l > 1) {
				int64_t middle = (l + r) / 2;
				if (seg[middle].first < i) {
					l = middle;
				}
				else {
					r = middle;
				}
			}
			res -= seg[r].second;
			seg[r].second--;
			res += a[i] + 1 + seg[r].second;
		}
	}
	cout << res << '\n';
	return 0;
}