#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const ll inf = 1e18;
const ll mod = 1e9 + 7;

void solve() {
    ll n, k; cin >> n >> k;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    ordered_set s;
    for(int i = 0; i < n; i++) s.insert(-a[i]);
    ll col = 0, ans = 0;
    for(ll el : s) {
        if (col >= k - 1) break;
        ans -= el;
        col++;
    }
    ll res = 0;
    for(int i = n - 1; i >= 0; i--) {
        if (s.size() < k - 1) break;
        ll last = -(*s.find_by_order(k - 2));;
        s.erase(s.find_by_order(s.order_of_key(-a[i])));
        if (s.size() < k - 1) break;
        if (last <= a[i]) {
            ll now = -(*s.find_by_order(k - 2));
            ans -= a[i];
            ans += now;
        }
        res = max(res, ans + i + 1 + a[i]);
    }
    cout << res << "\n";
}

int main() {
#ifdef KABANDJAN
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
//    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
