#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <random>
#include <chrono>
using namespace std;
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
struct node {
	ll x, sz, sum, pr;
	node *l, *r;
	node(ll x) : x(x), sz(1), sum(x), pr(rnd()), l(nullptr), r(nullptr) {}
};
ll gsz(node* root) {
	if (!root) return 0;
	return root->sz;
}
ll gsum(node* root) {
	if (!root) return 0;
	return root->sum;
}
void rec(node* root) {
	if (!root) return;
	root->sz = gsz(root->l) + 1 + gsz(root->r);
	root->sum = gsum(root->l) + root->x + gsum(root->r);
}
void split(node* root, node*& l, node*& r, ll k) {
	if (!root) l = r = nullptr;
	else if (root->x < k) split(root->r, root->r, r, k), l = root;
	else split(root->l, l, root->l, k), r = root;
	rec(root);
}
void split1(node* root, node*& l, node*& r, ll k) {
	if (!root) l = r = nullptr;
	else if (gsz(root->r) >= k) split1(root->r, root->r, r, k), l = root;
	else split1(root->l, l, root->l, k - gsz(root->r) - 1), r = root;
	rec(root);
}
void merge(node*& root, node* l, node* r) {
	if (!l) root = r;
	else if (!r) root = l;
	else if (l->pr < r->pr) merge(l->r, l->r, r), root = l;
	else merge(r->l, l, r->l), root = r;
	rec(root);
}
void insert(node*& root, ll z) {
	node *t1 = nullptr, *t2 = nullptr;
	node* v = new node(z);
	split(root, t1, t2, z);
	merge(t1, t1, v);
	merge(root, t1, t2);
}
ll Sum(node* root, ll k) {
	node *t1 = nullptr, *t2 = nullptr;
	split1(root, t1, t2, k);
	ll ans = gsum(t2);
	merge(root, t1, t2);
	return ans;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	ll n, k;
	cin >> n >> k;
	vector<ll> a(n);
	node* root = nullptr;
	ll ans = 0;
	for (ll i = 0; i < n; ++i) {
		ll z = 0;
		if (i >= k - 1) z = Sum(root, k - 1) + i + 1;
		cin >> a[i];
		z += a[i];
		if (i >= k - 1) ans = max(ans, z);
		insert(root, a[i]);
	}
	cout << ans;
	cout << endl;
	return 0;
}