#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>

using namespace std;
using ll = long long;
using ld = long double;
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("Ofast")
#define int long long

template<typename T>
void input(vector<T>& arr,size_t size) {
	arr.resize(size);
	for (T& i : arr) {
		cin >> i;
	}
}
template<typename T>
void print(vector<T>& arr) {
	for (T& i : arr) {
		cout << i << ' ';
	}
	cout << '\n';
}


void solve() {
	int n, k;
	cin >> n >> k;
	k = min(k, n);
	vector<pair<int, int>> a(n);
	for (size_t i = 0; i < n; i++)
	{
		cin >> a[i].first;
		a[i].second = i;
	}
	auto c = a;
	sort(all(a));
	reverse(all(a));
	vector<bool> used(n,0);
	int ind = -1;
	for (int i = 0; i < k - 1; i++)
	{
		if (ind < a[i].second) {
			ind = a[i].second;
		}
	}
	vector<ll> s,t;
	ll ans = 0;
	for (int i = 0; i < k - 1; i++)
	{
		if (a[i].second != ind) {
			used[a[i].second] = 1;
			ans += a[i].first + 1;
		}
	}
	int pos = -1, x = -1;
	for (size_t i = 0; i < n; i++)
	{
		if (!used[i]) {
			if (pos == -1 and c[i].first == a[k - 1].first) {
				pos = s.size();
			}
			if (i == ind) {
				x = s.size();
			}
			s.push_back(c[i].first);
			t.push_back(c[i].second);
		}
	}
	ll f = -1, sc = -1;
	for (size_t i = 0; i < s.size(); i++)
	{
		if (f == -1 or s[f] + f < s[i] + i) {
			sc = f;
			f = i;
		}
		else if (sc == -1 or s[i] + i > s[sc] + sc) {
			sc = i;
		}
	}
	ll ot = 0;
	for (size_t i = 0; i < s.size(); i++)
	{
		if (i == f) {
			ot = max(ot, s[f] + 1 + s[sc] + sc + (f > sc));
		}
		else {
			ot = max(ot, s[i] + 1 + s[f] + f + (i > f));
		}
	}
	cout << ans + ot;
}

int32_t main() {
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif // _DEBUG
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt = 1;
#ifdef _DEBUG
	cin >> tt;
#endif // _DEBUG
	while (tt--)
	{
		solve();
#ifdef _DEBUG
		cout << "\n================\n";
#endif // _DEBUG
	}
}