#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> A(n);
    vector<int> C;
    vector<int> D;
    vector<pair<int, int>> B;
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
        cin >> A[i];
        B.push_back({A[i], -i});
        mp[i] = 1;
    }
    sort(B.begin(), B.end());
    long long sum;
    sum = 0;
    for (int i = 0; i < k-1; ++i) {
        sum += B[n - i - 1].first;
        mp[-B[n - i - 1].second] = 0;
    }
    int s = n - 1;
    while (mp[s] == 1) {
        D.push_back(A[s]);
        mp[s] = 0;
        s--;
    }
    for (int i = 0; i < n; ++i) {
        if (mp[i] == 1)
            C.push_back(A[i]);
    }
    int sum1 = 0;
    int sum2 = n - k - D.size();
    int Max = 0;
    for (int i = 0; i < D.size(); ++i) {
        sum1 = max(sum1, D[i] + (int)D.size() - i - 1);
    }
    sum1 += C.size();
    for (int i = 0; i < C.size(); ++i) {
        Max = max(Max, C[i]);
    }
    sum2 += Max;
    cout << (sum + max(sum1, sum2) + k) << endl;
    return 0;
}
