#include <bits/extc++.h>
#define int long long
using namespace std;
using namespace __gnu_pbds;
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k; cin >> n >> k;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    set<int> q;
    int ans = 0, sum = 0;
    for (int i = 1; i <= n; ++i) {
        if (q.size() == k - 1) {
            ans = max(ans, sum + a[i] + i);
        }
        if (q.size() != k - 1) {
            sum += a[i];
            q.insert(a[i]);
        } else {
            auto it = *q.begin();
            if (it < a[i]) {
                sum -= it;
                q.erase(q.begin());
                sum += a[i];
                q.insert(a[i]);
            }
        }
    }
    cout << ans;
    return 0;
}
