#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll inf = 1e18;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    ll ans = 0, sm = 0;
    multiset<ll> mx;
    for (int ls = 0; ls < n; ++ls) {
        ans = max(ans, sm + a[ls] + ls + 1);
        if (mx.size() < k - 1) {
            mx.insert(a[ls]);
            sm += a[ls];
        } else {
            ll v = *mx.begin();
            if (v < a[ls]) {
                sm = sm + a[ls] - v;
                mx.erase(mx.begin());
                mx.insert(a[ls]);
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
