#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

#define int long long

using namespace std;



int32_t main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, k;
	cin >> n >> k;
	vector<int>v(n);
	for (int i = 0; i < n; ++i)cin >> v[i];
	set<pair<int,int>>ans;
	int cnt = 0;
	int lol = 0;
	for (int i = 0; i < n; ++i) {
		if (ans.size() < k) {
			cnt += v[i];
			ans.insert({ v[i],i });
			lol = max(lol, cnt + i + 1);
		}
		else {
			if (ans.begin()->first <= v[i]) {
				cnt += v[i] - ans.begin()->first;
				lol = max(lol, cnt + i+1);
				ans.erase(ans.begin());
				ans.insert({ v[i],i });
			}
			else {
				cnt += v[i] - ans.begin()->first;
				lol = max(lol, cnt + i+1);
				cnt -= v[i] - ans.begin()->first;
			}
		}
	}
	cout << lol;
	return 0;
}