#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;


int main(int, char**) {
    cout << setprecision(15) << fixed;
#ifdef DEBUGMB
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif

    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    vector<ll> asort;
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
    }
    asort = a;
    sort(asort.begin(), asort.end());

    
    ll mn1 = asort[n - k];
    ll right1 = 0;
    for (ll i = 0; i < n; ++i) {
        if (a[i] >= mn1) {
            right1 = i;
        }
    }
    ll ans1 = right1 + 1 + accumulate(asort.end() - k, asort.end(), 0ll);
    for (ll i = right1 + 1; i < n; ++i) {
        ans1 = max(ans1, ans1 - mn1 - 1 + a[i] + i - right1 + 1);
    }
    if (k == 1) {
        cout << ans1;
        return 0;
    }
    
    ll mn2 = asort[n - k + 1];
    ll right2 = 0;
    for (ll i = 0; i < n; ++i) {
        if (a[i] >= mn2) {
            right2 = i;
        }
    }
    ll res2 = right2 + 1 + accumulate(asort.end() - k + 1, asort.end(), 0ll);
    ll ans2 = res2;
    for (ll i = right2 + 1; i < n; ++i) {
        ans2 = max(ans2, res2 + a[i] + i - right2);
    }
    cout << max(ans1, ans2);


    


    return 0;
}
