#define _CRT_SECURE_NO_WARNINGS


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>


using namespace std;
using ll=long long;
const ll inf = 1e6;


ll a[500001];
int main() {
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	ll ans = 0;
	multiset<ll> s;
	ll sum = 0;
	for (int i = 1; i < k; ++i) {
		s.insert(a[i]);
		sum += a[i];
	}
	ans = 0;
	for (int i = k; i <= n; ++i) {
		ans = max(ans, sum + a[i] + i);
		auto it = s.begin();
		if (*it < a[i]) {
			sum -= *it;
			sum += a[i];
			s.erase(it);
			s.insert(a[i]);
		}
	}
	cout << ans;
}