#include <bits/stdc++.h>
using namespace std;
using ll = long long;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k; cin >> n >> k;
    vector<int> arr(n);
    for (auto & it : arr) cin >> it;
    vector<pair<int, int>> mx;
    for (int i = 0; i < n; ++i) {
        mx.push_back({arr[i], i});
    }
    sort(mx.begin(), mx.end());
    ll l = n, sm = 0, cnt = 0, ans = 0;
    for (int i = n - 1; i >= 1; --i) {
        while (cnt < k - 1 && l > 0) {
            --l;
            if (mx[l].second < i) {
                cnt++;
                sm += mx[l].first;
            }
        }
        if (cnt < k - 1) break;
        ans = max(ans, sm + i + 1 + arr[i]);
        if (make_pair(arr[i - 1], i - 1) >= mx[l]) {
            --cnt;
            sm -= arr[i - 1];
        }
    }
    cout << ans << "\n";
    return 0;
}
