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

using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	vector<int> pr(n);
	vector<pair<long long, long long>> a(n);
	vector<pair<long long, long long>> b;
	for (int i = 0; i < n; i++) {
		cin >> a[i].first;
		a[i].second = i;
		pr[i] = i;
	}
	b = a;
	sort(a.begin(), a.end());
	map<pair<int, int>, int> m1;
	long long maxind = 0;
	for (int i = n - 1; i >= n - k; i--) {
		m1[a[i]] = 1;
		maxind = max(maxind, a[i].second);
	}
	int cnt = 0;
	for (int i = 0; i < maxind; i++) {
		if (m1[b[i]] == 1) continue;
		cnt++;
	}
	m1[b[maxind]] = cnt + 1;
	pair<long long, long long> mm = a[n - k];
	long long max1 = mm.first + m1[mm];
	long long ans = 0;
	long long curp = m1[mm];
	for (int i = maxind + 1; i < n; i++) {
		curp++;
		max1 = max(max1, b[i].first + curp);
	}
	ans += max1;
	for (int i = 0; i < n; i++) {
		if (b[i] == mm) continue;
		if (m1[b[i]] > 0) {
			ans += m1[b[i]] + b[i].first;
		}
	}
	cout << ans;

}