#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>

int main() {
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	int n, k;
	std::cin >> n >> k;
	std::vector<int> a(n);
	std::set<std::pair<int, int>> p;
	for (int i = 0; i < n; ++i) {
		std::cin >> a[i];
		p.insert({ -a[i], i });
	}
	std::vector<int> b(k);
	int left = 0;
	long long ans = 0;
	for (int i = 0; i < k - 1; ++i) {
		b[i] = (*p.begin()).second;
		ans += a[b[i]];
		p.erase(p.begin());
		left = std::max(left, b[i]);
	}
	int last = (*p.begin()).second;
	if (std::max(last, left) == n - 1) {
		std::cout << ans + a[last] + n << '\n';
		return 0;
	}
	if (last < left) {
		int p = a[last] + 1, d = 2;
		for (int i = left + 1; i < n; ++i) {
			p = std::max(p, a[i] + d);
			++d;
		}
		std::cout << ans + p + left << '\n';
	}
	else {
		int d = 1, p = 0;
		for (int i = left + 1; i < n; ++i) {
			p = std::max(p, a[i] + d);
			++d;
		}
		std::cout << ans + p + left + 1 << '\n';
	}
}
