#include <iostream>

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    ll n, k, ans = 0, kmax = 0, x = -1e18;
    cin >> n >> k;
    vector<ll> b(n), a;
    for (int i = 0; i < n; ++i){
        cin >> b[i];
    }
    a = b;
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    for (int i = 0; i < k; ++i){
        ans += a[i];
        kmax = a[i];
    }
    ll cnt = -1;
    for (int i = n - 1; i >= 0; --i){
        if (b[i] >= kmax){
            break;
        }
        cnt = n - 1 - i;
        x = max(x, b[i] - cnt);
    }
    x = max(x, kmax - cnt - 1);
    cout << ans + n - kmax + x << endl;
    /*
    ll t;
    cin >> t;
    for (int _ = 0; _ < t; ++_){
        ll n, m;
        cin >> n >> m;
        vector<ll> x(m);
        for (int i = 0; i < m; ++i){
            cin >> x[i];
        }
        vector<vector<vector<ll>>> dp(m, vector<vector<ll>>(n + 1, vector<ll>(2, -1e18)));
        dp[0][0][1] = x[0];
        ll ans = -1e18;
        for (int i = 1; i < m; ++i){
            for (int j = 0; j < n + 1; ++j){
                if (j != 0){
                    dp[i][j][0] = max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1]) - x[i];
                }
                if (j < n){
                    dp[i][j][1] = max(dp[i - 1][j + 1][0], dp[i - 1][j + 1][1]) + x[i];
                }
            }
        }
        for (int i = 0; i < n + 1; ++i){
            ans = max(ans, max(dp[m - 1][i][0], dp[m - 1][i][1]));
        }
        cout << ans << endl;
    }*/
    return 0;
}


/*
vector<ll> st, a;

void build(int v, int tl, int tr){
    if (tl == tr){
        st[v] = a[tl];
    } else{
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm + 1, tr);
        st[v] = st[2 * v] + st[2 * v + 1];
    }
}

void upd(int v, int tl, int tr, int ind, int val){
    if (tl == tr){
        st[v] = val;
        a[ind] = val;
    } else{
        int tm = (tl + tr) / 2;
        if (ind <= tm){
            upd(2 * v, tl, tm, ind, val);
        } else{
            upd(2 * v + 1, tm + 1, tr, ind, val);
        }
        st[v] = st[2 * v] + st[2 * v + 1];
    }
}

ll getVal(int v, int tl, int tr, int l, int r){
    if (l <= tl && tr <= r){
        return st[v];
    } else{
        int tm = (tl + tr) / 2;
        if (r <= tm){
            return getVal(2 * v, tl, tm, l, r);
        }
        if (l > tm){
            return getVal(2 * v + 1, tm + 1, tr, l, r);
        }
        return getVal(2 * v, tl, tm, l, r) + getVal(2 * v + 1, tm + 1, tr, l, r);
    }
}*/
