#include <bits/stdc++.h>

using namespace std;
using ll = long long;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    k = min(k, n);
    for (auto &x : a) cin >> x;
    set<pair<ll, ll>, greater<>> s;
    map<ll, bool> used;
    multiset<ll> tk;
    for (ll i = 0; i < n; ++i) s.insert({a[i], i});
    ll summx = 0;
    for (ll i = 0; i < k; ++i) {
        summx += s.begin()->first;
        tk.insert({s.begin()->first});
        used[s.begin()->second] = true;
        s.erase(s.begin());
    }
    ll ans = 0;
    for (ll i = n - 1; i >= 0; --i) {
        ll delt = 0;
        if (!used[i] && !tk.empty()) delt = -*tk.begin() + a[i];
        ans = max(ans, summx + delt + n - (n - i - 1));
        if (used[i] && !s.empty()) {
            tk.erase(a[i]);
            summx -= a[i];
            summx += s.begin()->first;
            tk.insert({s.begin()->first});
            used[s.begin()->second] = true;
            s.erase(s.begin());
        }
    }
    cout << ans << '\n';
}
