#include <bits/stdc++.h>

using namespace std;
using ll = long long;

void solve();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll q = 1;
    while (q--) {
        solve();
    }
}

const ll INF = 1e18;

void solve() {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    if (k == 1) {
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, i + 1 + a[i]);
        }
        cout << ans;
        return;
    }
    ll sm = 0;
    multiset<ll> cur;
    vector<ll> dp(n, -INF);
    for (int i = 0; i < n; i++) {
        if (cur.size() < k - 1) {
            sm += a[i];
            cur.insert(a[i]);
            continue;
        }
        if (*cur.begin() < a[i]) {
            sm -= *cur.begin();
            sm += a[i];
            cur.erase(cur.begin());
            cur.insert(a[i]);
        }
        dp[i] = sm;
    }
    ll mx = -INF;
    ll ans = 0;
    for (int i = n - 1; i > 0; i--) {
        mx = max(mx, a[i] + i + 1);
        ans = max(ans, mx + dp[i - 1]);
    }
    cout << ans;
} 