#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
#define ll long long

using namespace std;
//using namespace __gnu_pbds;

ll n, k;
vector<ll> a;

void input() {
    cin >> n >> k;
    a.resize(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
}

void solve() {
    multiset<ll> s;
    ll sum = 0, ans = 0;
    for (ll i = 0; i < n; i++) {
        ll tek = sum + i + a[i] - s.size() + k;
        //cout << sum << " " << i << " " << a[i] << " " << s.size() << " " << k << endl;
        ans = max(ans, tek);
        if (s.size() < k - 1 || (*s.begin()) < a[i]) {
            if (s.size() == k - 1) {
                sum -= (*s.begin());
                s.erase(s.begin());
            }
            sum += a[i];
            s.insert(a[i]);
        }
    }
    cout << ans;
}

void output() {

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    input();
    solve();
    output();
    return 0;
}
