#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/

using namespace std;
/*using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;*/

#define int long long

signed main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<pair<int, int>> b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        b[i] = {a[i], i};
    }
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());
    int ans = 0, mx = 0;
    for (int i = 0; i < k - 1; ++i) {
        ans += b[i].first;
        mx = max(mx, b[i].second);
    }
    int answer = 0;
    for (int i = k; i < n; ++i) {
        ans += b[i].first;
        int mx1 = max(mx, b[i].second);
        answer = max(answer, ans + mx1 + 1);
    }
    cout << answer;
    return 0;
}
