#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define vec vector
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pii pair < int, int >
#define f first
#define s second

using namespace std;

template <typename T>
bool umn (T &a, T b) {return a > b ? (a = b, 1) : 0; }
template <typename T>
bool umx (T &a, T b) {return a < b ? (a = b, 1) : 0; }

const int N = 5e5 + 5;
const int oo = 2e9;
const ll OO = 1e18;
const int mod = 1e9 + 7;

int add (int x, int y) {
    x += y;
    if (x >= mod)
        x -= mod;
    return x >= 0 ? x : x + mod;
}

int mul (int x, int y) {
    return (1ll * x * y) % mod;
}

int bpow (int a, int b) {
    if (b == 0)
        return 1;
    if (b % 2)
        return mul(bpow(a, b - 1), a);
    int t = bpow(a, b / 2);
    return mul(t, t);
}

using namespace __gnu_pbds;

template <typename T>
using oset = tree < T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update >;

int a[N];

void slv () {
    int n, k, f = 0;
    cin >> n >> k;

    oset < pii >  st;
    ll sumof = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        st.insert({a[i], f++});
    }
    for (int i = 1; i < k; ++i)
        sumof += (*st.find_by_order(sz(st) - i)).f;

    ll best = 0;
    if (k == 2) {
        int le = 0, best1 = 0;
        vec < int > ri(n);
        ri[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; --i) {
            ri[i] = max(ri[i + 1], a[i]);
        }
        for (int i = 0; i < n; ++i) {
            umx(best1, a[i] + i + k + max(le, (i + 1 < n ? ri[i + 1] : 0)));

            umx(le, a[i] - 1);
        }
        cout << best1;

        return;
    }


    for (int i = 0; i < n; ++i) {
        ll res = a[i] + i + k;
        umx(best, res);
        if (k == 1) continue;

        pii kek = {a[i], 0};
        int pos = st.order_of_key(kek);
        bool qq = 0;
        if (pos >= sz(st) - k + 1) {
            sumof -= a[i];
            qq = 1;
        }
        st.erase(st.find_by_order(pos));
        if (qq)
            sumof += (*st.find_by_order(sz(st) - k + 1)).f;
//        auto it = st.end();
//        for (int j = 0; j < k - 1; ++j) {
//            --it;
//            res += *it;
//        }
        umx(best, res + sumof);
        int last = (*st.find_by_order(sz(st) - k + 1)).f;
        if (a[i] - 1 > last) {
            sumof -= last;
            sumof += a[i] - 1;
        }
        st.insert({a[i] - 1, f++});
    }
    cout << best;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        slv();
    }
}
