#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
using ll = long long;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll ans = 0;
    multiset<int> elems;
    ll sm = 0;
    for (int i = 0; i < k - 1; i++) {
        elems.insert(a[i]);
        sm += a[i];
    }
    for (int i = k - 1; i < n; i++) {
        ans = max(ans, sm + a[i] + i + 1);
        if (a[i] > *(elems.begin())) {
            sm -= *(elems.begin());
            elems.erase(elems.begin());
            sm += a[i];
            elems.insert(a[i]);
        }
    }
    cout << ans << endl;

}