#include<bits/stdc++.h>

using namespace std;

vector<pair<int,int>>t;

void upd(int v,int tl,int tr,int x){
    if(tr<x||tl>x){
        return;
    }
    if(tl==tr){
        t[v].first++;
        return;
    }
    int m = (tl+tr)/2;
    upd(v*2,tl,m,x);
    upd(v*2+1,m+1,tr,x);
    t[v].first = t[v*2].first+t[v*2+1].first;
}

void push(int v){
    if(t[v].second==1){
        t[v].second = 0;
        t[v*2].first = 0;
        t[v*2+1].first = 0;
        t[v*2+1].second = 1;
        t[v*2].second =1;
    }
}

int sum(int v,int tl,int tr,int x){
    if(tl>=x){
        return 0;
    }
    if(tr<x){
        t[v].second = 1;
        int res = t[v].first;
        t[v].first = 0;
        return res;
    }
    push(v);
    int m = (tl+tr)/2;
    int res = sum(v*2,tl,m,x);
    res+=sum(v*2+1,m+1,tr,x);
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,k;
    cin>>n>>k;
    vector<int>a(n);
    vector<pair<int,int>>b(n);
    t.resize(4*n);
    for(int i = 0;i<n;++i){
        cin>>a[i];
        b[i] = {a[i],i};
    }
    sort(b.begin(),b.end());
    vector<int>used(n);
    for(int i = 0;i<n-k;++i){
        upd(1,0,n-1,b[i].second);
        used[b[i].second] = 1;
    }
    int ans = 0;
    vector<int>e(n);
    int last = 0;
    int lastx = 0;
    int mn = 1e18,mnx = 0;
    int s = 0;
    for(int i = 0;i<n;++i){
        if(!used[i]){
            int f = sum(1,0,n-1,i);
            e[i]+=f;
            s+=(a[i]+f+1);
            lastx = i;
        }
    }
    if(t[1].first){
    int mx = 0;
    for(int i = lastx+1,c = 1;i<n;++i,++c){
        mx = max(mx,c+a[i]);
    }
    for(int i = 0;i<n;++i){
        if(!used[i]){
            ans = max(ans,s-a[i]-e[i]-1+max(mx+e[i]+1,a[i]+e[i]+1));
        }
    }
    }else{
        ans = s;
    }
    cout<<ans;
}