#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll

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

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
}