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

using namespace std;

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;
	int64_t cnt = 0;
	vector<pair<int64_t, int64_t>> seg = { {0, 0} };
	int64_t pos = 0;
	while (cnt != k) {
		pair<int64_t, int64_t> best{ 0,-1 };
		for (int64_t i = 0; i < n; ++i) {
			if (!p[i]) {
				if (i >= seg.back().first) {
					if (best.second == -1 || a[i] + i - seg.back().first + 1 > best.first) {
						best.first = a[i] + i - seg.back().first + 1;
						best.second = i;
					}
				}
				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;
						}
					}
					if (best.second == -1 || a[i] >= best.first) {
						best.first = a[i];
						best.second = i;
					}
				}
			}
		}
		int64_t i = best.second;
		p[i] = 1;
		if (i >= seg.back().first) {
			seg.emplace_back(i + 1, a[i] + i - seg.back().first + 1);
			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;
		}
		++cnt;
	}
	cout << res << '\n';
	return 0;
}