#include <bits/stdc++.h>

using ll = long long;
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    int maxi = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i] > a[maxi]) maxi = i;
    }
    if (k == 1) {
        int ans = a[0] + 1;
        for (int i = 0; i < n; ++i) {
            ans = max(ans, a[i] + i + 1);
        }
        cout << ans;
        return 0;
    }
    multiset<int> ms;
    int nows = 0;
    for (int i = 0; i < k - 1; ++i) {
        nows += a[i];
        ms.insert(a[i]);
    }
    int ans = -1;
    for (int 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;
}