#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define siz(x) int(x.size())
#define ceil(a, b) (((a) + (b) - 1) / (b))
const int INF = 2e9 + 7;
const ll LINF = 1e18 + 7;

void solve() {
    int n, k;
    ll sum = 0, ans = -1;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; ++i) cin >> a[i];
    if(k == 1) {
        for(int i = 0; i < n; ++i) ans = max(ans, a[i] + i + 1ll);
    }
    else {
        set<int> st;
        for (int i = 0; i < k - 1; ++i) {
            st.insert(a[i]);
            sum += a[i];
        }
        for (int i = k - 1; i < n; ++i) {
            ans = max(ans, sum + a[i] + (i - k + 2) + (k - 1));
            if (*st.begin() < a[i]) {
                sum -= *st.begin();
                st.erase(st.begin());
                sum += a[i];
            }
        }
    }
    cout << ans;
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout << fixed; cout.precision(8);
#ifdef INEYOK
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    solve();
}
