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

int main(){
    long long n,k;
    cin >> n >> k;
    vector<long long> a(n);
    multiset<long long> s;
    for(long long i = 0;i < n;i++){
        cin >> a[i];
        //s.insert({a[i] + i,a[i]});
    }
    if(k == 1){
        long long ma = 0;
        for(long long i = 0;i < n;i++){
            if(a[i] + i + 1 > ma){
                ma = a[i] + i + 1;
            }
        }
        cout << ma;
        return 0;
    }
    long long ans = 0,val = 0;
    for(long long i = 0;i < k - 1;i++){
        s.insert(a[i]);
        val += a[i];
    }
    multiset<long long> s1;
    for(long long i = k - 1;i < n;i++){
        s1.insert(a[i] + i - k + 1);
    }
    ans = val + k + *s1.rbegin();
    for(long long i = k - 1;i < n - 1;i++){
        s1.erase(s1.find(a[i] + i - k + 1));
        if(a[i] >= *s.begin()){
            val += a[i] - *s.begin();
            s.erase(s.find(*s.begin()));
            s.insert(a[i]);
            //cout << i << " " << val + *s1.rbegin() + k << endl;
            if(val + *s1.rbegin() + k > ans){
                ans = val + *s1.rbegin() + k;
            }
        }


    }
    cout << ans;
    return 0;
}
