#include<iostream>
#include<set>
#include<vector>
//#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;
        ans = max(ans, tek);
        if (s.size() < k - 1) {
            sum += a[i];
            s.insert(a[i]);
        } else {
            ll f = *s.begin();
            if (f < a[i]) {
                s.erase(s.begin());
                s.insert(a[i]);
                sum += a[i] - f;
            }
        }
    }
    cout << ans;
}

void output() {

}

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