
#include <iostream>
#include <limits>
#include <chrono>
#include <random>
#include <iomanip>
#include <algorithm>
#include <chrono>
#include <random>
#include <math.h>
#include <set>
using namespace std;
#define int long long
void check(vector <int>& vec, int n,int k) {
	int sum = 0;
	multiset <pair<int, int>> st;
	for (int i = 0;i < n - 1;i++)st.insert({ vec[i],i });
	while (st.size() > k) {
		st.erase(st.begin());
	}
	int max_pos = 0;
	while (st.size()) {
		sum += st.begin()->first;
		max_pos = max(max_pos, st.begin()->second);
		st.erase(st.begin());
	}
	if (max_pos == n - 1) {
		cout << sum + n;
		exit(0);
	}
}
signed main()
{
	int n, k;
	cin >> n >> k;
	vector <int> vec(n);
	int sum = 0;
	for (int i = 0;i < n;i++)cin >> vec[i];
	multiset <pair<int, int>> st;
	check(vec, n, k);
	for (int i = 0;i < n - 1;i++)st.insert({ vec[i],i });
	while (st.size() >= k) {
		st.erase(st.begin());
	}
	vector <bool> fl(n, false);
	int max_pos = 0;
	while (st.size()) {
		sum += st.begin()->first;
		fl[st.begin()->second] = 1;
		max_pos = max(max_pos, st.begin()->second);
		st.erase(st.begin());
	}
	int res = -1, pos = -1;
	int cnt = 0;
	for (int i = 0;i < max_pos;i++) {
		sum++;
		fl[i] = 1;
	}
	if (pos == n - 1) {
		sum += res;
		cout << sum + k - 1;
		exit(0);
	}
	res = 0;
	cnt = 0;
	for (int i = max_pos + 1;i < n;i++) {
		cnt++;
		if (res < cnt + vec[i]) {
			res = cnt + vec[i];
		}
	}
	cout << sum + res;
}
