#include <bits/stdc++.h>

using ll = long long;
using namespace std;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    ll maxi = 0;
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i] > a[maxi]) maxi = i;
    }
    if (k == 1) {
        ll ans = a[0] + 1;
        for (ll i = 0; i < n; ++i) {
            ans = max(ans, a[i] + i + 1);
        }
        cout << ans;
        return 0;
    }
    multiset<ll> ms;
    ll nows = 0;
    for (ll i = 0; i < k - 1; ++i) {
        nows += a[i];
        ms.insert(a[i]);
    }
    ll ans = -1;
    for (ll i = k - 1; i < n; ++i) {
        if (i >= maxi) {
            ans = max(ans, a[i] + nows + i + 1);
        }
        auto c = ms.lower_bound(0);
        if (*c < a[i]) {
            nows += a[i] - *c;
            ms.erase(*c);
            ms.insert(a[i]);
        }
    }
    cout << ans;
}