#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define int ll

int n, k = 2;
vector<list<int>> p(1);

void input() {
    cin >> n >> k;
    int x;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        p[0].push_back(x);
    }
}

struct t_ans {
    int val;
    list<int> l1, l2;
};

t_ans split(list<int> l) {
    int mx = l.front() + 1;
    int mxid = 1;
    auto mxit = l.begin();
    {
        auto it = l.begin();
        for (int i = 1; it != l.end(); it++, i++) {
            if (*it + i >= mx) {
                mx = *it + i;
                mxid = i;
                mxit = it;
            }
        }
    }

    int mx2 = 0;
    {
        auto it = mxit;
        it++;
        for (int i = mxid + 1; it != l.end(); it++, i++) {
            mx2 = max(mx2, *it + i - mxid);
        }
    }

    int mx3 = 0;
    auto mx3it = l.begin();
    auto it = l.begin();
    for (int i = 1; it != mxit; it++, i++) {
        if (*it + 1 > mx3) {
            mx3 = *it + 1;
            mx3it = it;
        }
    }

    int ans;
    list<int> l1, l2;
    if (mx + mx2 > mx + mx3 - 1) {
        ans = mx + mx2;
        mxit++;
        l1 = list(l.begin(), mxit);
        l2 = list(mxit, l.end());
    } else {
        ans = mx + mx3 - 1;
        l1.push_back(*mx3it);
        l.erase(mx3it);
        l2 = l;
    }

    return { ans, l1, l2 };
}

int calc(list<int> l) {
    int ans = 0;
    for (auto [it, i] = tuple(l.begin(), 1); it != l.end(); it++, i++) {
        ans = max(ans, *it + i);
    }
    return ans;
}

void solve() {
    input();

    while (p.size() != k) {
        t_ans mx {.val = 0};
        int mxid = 0;
        for (int i = 0; i < p.size(); i++) {
            if (p[i].size() == 1) continue;
            auto tmp = split(p[i]);
            if (tmp.val > mx.val) {
                mx = tmp;
                mxid = i;
            }
        }
        p[mxid] = mx.l1;
        p.push_back(mx.l2);
    }

    int ans = 0;
    for (auto l : p) ans += calc(l);
    cout << ans;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef local
    freopen("i.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
