#include <bits/stdc++.h>

using namespace std;

#define int long long

main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (auto &i : a) cin >> i;
    vector<pair<int, int>> sa(n);
    for (int i = 0; i < n; i++) {
        sa[i] = {a[i], i};
    }
    sort(sa.begin(), sa.end());
    reverse(sa.begin(), sa.end());
    int fans = 0;
    int lans = 0;
    int maxi = 0;
    vector<bool> used(n);
    for (int i = 0; i < k - 1; i++) {
        lans += sa[i].first;
        maxi = max(maxi, sa[i].second);
        used[sa[i].second] = true;
    }
    for (int i = 0; i < n; i++) {
        if (used[i]) continue;
        fans = max(fans, lans + a[i] + max(maxi, i) + 1);
    }
    cout << fans << endl;
}