#include <bits/stdc++.h>
//#define int long long
using namespace std;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    set<pair<int, int>> st;
    long long ans = 0, sumst = 0;
    for (int i = 0; i < k; ++i) {
        st.insert({a[i], i});
        ans += (long long) a[i];
        sumst += (long long) a[i];
    }
    sumst -= (*(st.begin())).first;
    st.erase(st.begin());
    ans += k;
    for (int i = k; i < n; ++i) {
        long long it = sumst + (long long) (a[i] + i + 1);
        ans = max(ans, it);
        pair<int, int> pi = {a[i], i}, pid = *(st.begin());
        if (pi > pid) {
            sumst -= (long long) pid.first;
            sumst += (long long) a[i];
            st.erase(st.begin());
            st.insert(pi);
        }
    }
    cout << ans;
}
