#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (ll &i : a) cin >> i;
    ll sum = 0;
    multiset<ll> s;
    for (ll i = 0; i < k - 1; i++) {
        sum += a[i];
        s.insert(a[i]);
    }
    ll ans = 0;
    for (ll i = k - 1; i < n; i++) {
        ll res = a[i] + i + 1 + sum;
        ans = max(ans, res);
        if (a[i] > *s.begin()) {
            sum += a[i] - *s.begin();
            s.erase(*s.begin());
            s.insert(a[i]);
        }
    }
    cout << ans << '\n';
}
