#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 {
		int val, pos, push;
		node() {
			val = 0;
			pos = 0;
			push = 0;
		}
		node(int _v, int _p) {
			val = _v;
			push = 0;
			pos = _p;
		}
	};
	vector<node> t;
	int n;
	node merge(node l, node r) {
		node res;
		if (l.val == r.val) {
			res = l;
		}
		else if (l.val > r.val) {
			res = l;
		}
		else {
			res = r;
		}
		res.push = 0;
		return res;
	}
	segtree() {
		n = 0;
	}
	segtree(int _n) {
		n = _n;
		t.resize(4 * n);
	}



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

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

	node mx(int v, int tl, int tr, int l, int r) {
		if (l > r) {
			return node(0, 0);
		}
		else if (tl == l && r == tr) {
			return t[v];
		}
		int tm = (tl + tr) / 2;
		push(v, tl, tr);
		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(int v, int tl, int tr, int l, int r, int val) {
		if (l > r) {
			return;
		}
		else if (tl == l && r == tr) {
			if (tl == tr) {
				t[v].pos = tl;
			}
			apply(v, tl, tr, val);
			return;
		}
		int tm = (tl + tr) / 2;
		push(v, tl, tr);
		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(int l, int r) {
		return mx(1, 0, n - 1, l, r);
	}

	void add(int l, int r, int val) {
		return add(1, 0, n - 1, l, r, val);
	}
};

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} };
	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 (int 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) {
			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).val);
					x.add(i, i, a[i]);
				}
			}
			/*for (int i = seg.back().first; i < n; ++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;
}