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

using namespace std;

struct segtree {
	struct node {
		int64_t val, pos, push;
		node() {
			val = 0;
			pos = 0;
			push = 0;
		}
		node(int64_t _v, int64_t _p) {
			val = _v;
			push = 0;
			pos = _p;
		}
	};
	vector<node> t;
	int64_t n;
	node merge(node l, node r) {
		node res;
		if (l.val > r.val) {
			res = l;
		}
		else {
			res = r;
		}
		res.push = 0;
		return res;
	}
	segtree() {
		n = 0;
	}
	segtree(int64_t _n) {
		n = _n;
		t.resize(4 * n);
	}

	void apply(int64_t v,int64_t _v) {
		t[v].val += _v;
		t[v].push += _v;
	}

	void push(int64_t v) {
		if (v < t.size() && t[v].push != 0) {
			apply(v * 2, t[v].push);
			apply(v * 2 + 1, t[v].push);
			t[v].push = 0;
		}
	}

	node mx(int64_t v, int64_t tl, int64_t tr, int64_t l, int64_t r) {
		if (l > r) {
			return node();
		}
		else if (tl == l && r == tr) {
			return t[v];
		}
		int64_t tm = (tl + tr) / 2;
		push(v);
		return merge(mx(v * 2, tl, tm, l, min(r, tm)), mx(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
	}

	void add(int64_t v, int64_t tl, int64_t tr, int64_t l, int64_t r, int64_t val) {
		if (l > r) {
			return;
		}
		else if (tl == l && r == tr) {
			if (tl == tr) {
				t[v].pos = tl;
			}
			apply(v, val);
			return;
		}
		int64_t tm = (tl + tr) / 2;
		push(v);
		add(v * 2, tl, tm, l, min(r, tm), val);
		add(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
		t[v] = merge(t[v * 2], t[v * 2 + 1]);
	}

	node mx(int64_t l, int64_t r) {
		return mx(1, 0, n - 1, l, r);
	}

	void add(int64_t l, int64_t r, int64_t val) {
		return add(1, 0, n - 1, l, r, 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} };
	segtree 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).pos;
		//for (int64_t i = 0; i < n; ++i) {
		//	cout << x.mx(i, i).val << ' ';
		//}
		//cout << '\n';
		p[i] = 1;
		x.add(i, i, -1e9);
		if (i >= seg.back().first) {
			int64_t ps = seg.back().first;
			seg.emplace_back(i + 1, a[i] + i - seg.back().first + 1);
			for (int64_t i = ps; i < seg.back().first; ++i) {
				if (!p[i]) {
					x.add(i, i, -x.mx(i, i).val);
					x.add(i, i, a[i]);
				}
			}
			x.add(seg.back().first, n - 1, -(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;
}