#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<random>
#include<chrono>
#include<set>
#include<deque>
#define int long long
#define ll long long
#define ld long double
using namespace std;
mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());

void solve() {
	int n, k;
	cin >> n >> k;
	vector<int> v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}
	vector<int> p(n,-1e18);
	multiset<int> st;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		st.insert(v[i]);
		sum += v[i];
		while (st.size() >= k) {
			sum -= *st.begin();
			st.erase(st.begin());
		}
		if (st.size() == k - 1)p[i] = sum;
	}
	int ans = -1e18;
	for (int i = k - 1; i < n; i++) {
		ans = max(ans, p[i - 1] + v[i] + i + 1);
	}
	cout << ans;

}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	//cin >> t;
	t = 1;
	while (t--) {
		solve();
	}
}
