#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
#define int long long
const int N=1e5+10;


signed main(){
    int n,m,i,j;
    cin >> n >> m;
    vector<int>a(n);
    vector<pair<int,int>>b;
    for(i=0;i<n;i++){
        cin >> a[i];
        b.push_back({a[i],i});
    }
    vector<int>v(n,0);
    sort(b.begin(),b.end());
    for(i=n-1;i>n-1-m;i--){
        v[b[i].second]=1;
    }
    int q=1,ans=0,w=0;
    for(i=0;i<n;i++){
        if(v[i]==0)q++;
        else {
            if(i==b[n-m].second){
                w=q;
            }
            ans+=q+a[i];
            q=1;
        }
    }
    if(q!=0){
        ans-=b[n-m].first+w;
        int r=b[n-m].first+w;
        w++;
        for(i=n-q+1;i<n;i++){
            r=max(r,a[i]+w);
            w++;
        }
        cout << ans+r;
    }else{
        cout << ans;
    }
    return 0;
}