#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
#include <cmath>
#include <map>
#include <iomanip>
#include <queue>
#include <unordered_map>
using namespace std;
using ll = long long;

//struct Seg {
//	ll N;
//	vector<ll> t;
//	void create(ll n) {
//		N = 1ll << (ll)ceil(log2(n));
//		t.resize(2 * N);
//	}
//	void add(ll pos, ll l, ll r, ll ind) {
//		if (l > pos || r < pos) return;
//		if (l == r) {
//			++t[ind];
//			
//		}
//	}
//};

void solve() {
	ll n, k;
	cin >> n >> k;
	vector<ll> v(n);
	map<ll, ll> sj;
	for (ll i = 0; i < n; ++i)
		cin >> v[i], sj[v[i]] = 1;
	vector<ll> val;
	ll num = 0, ind = 0;
	for (auto& to : sj) {
		to.second = num++;
		val.push_back(to.first);
	}
	vector<ll> cnt(num + 1);
	ll sum = 0, c = 0, ans = 0;
	for (ll i = 0; i < n; ++i) {
		ll tsum = sum;
		if (c > k - 1)
			tsum -= (c - (k - 1)) * val[ind];
		ans = max(ans, i + 1 + v[i] + tsum);
		++cnt[sj[v[i]]];
		if (sj[v[i]] >= ind)
			sum += v[i], ++c;
		while (c > k - 1) {
			if (c - cnt[ind] >= k - 1) {
				sum -= cnt[ind] * val[ind], c -= cnt[ind], ind++;
				continue;
			}
			break;
		}
	}
	cout << ans << endl;
}

int main() {
#ifdef _DEBUG
	freopen("input3.txt", "r", stdin);
	freopen("output3.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false); cin.tie(0);
	ll t = 1;
	// cin >> t;
	while (t--)
		solve();
}