#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <random>
#include <climits>
#include <time.h>
#include <cmath>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")

using namespace std;
using ll = long long;
using ld = long double;
ll inf = 4e18;

random_device rd;
mt19937 gen(rd());
uniform_int_distribution <> distr(0, INT_MAX);

ll binpow(ll x, ll d, ll mod) {
	if (d == 0)
		return 1;
	if (d == 1)
		return x;
	ll cur = binpow(x, d >> 1, mod) * binpow(x, d >> 1, mod) % mod;
	if (d & 1)
		cur = cur * x % mod;
	return cur;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> v(n);
	multiset <pair<ll, ll>> ms;
	vector<pair<ll, ll>> vc;
	for (ll i = 0; i < n; i++) {
		cin >> v[i];
		ms.insert({ v[i], i });
		vc.push_back({ v[i], i });
	}
	sort(vc.rbegin(), vc.rend());
	ll ans = 0;
	ll summax = 0;
	vector<bool> used(n, 0);
	for (ll i = 0; i < k - 1; i++) {
		summax += vc[i].first;
		used[vc[i].second] = 1;
		ms.erase(ms.find(vc[i]));
	}
	for (ll i = n - 1; i >= 0; i--) {
		if (ms.find({ v[i], i }) != ms.end())
			ms.erase(ms.find({ v[i], i }));
		if (ms.empty())
			break;
		if (used[i]) {
			summax -= v[i];
			auto cur = *ms.rbegin();
			summax += cur.first;
			used[cur.second] = 1;
		}
		ans = max(ans, summax + v[i] + i + 1);
	}
	cout << ans;
}