#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    multiset<int> s;
    long long ans = 0, sum_nw = 0;
    for (int i = 0; i < n; ++i) {
        ans = max(ans, sum_nw + a[i] + i + 1);
        s.insert(a[i]);
        sum_nw += a[i];
        if (s.size() == k) {
            sum_nw -= *s.begin();
            s.erase(s.begin());
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
