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

using ll = long long;

using namespace std;
using namespace __gnu_pbds;

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

inline void solve() {
    /*ordered_set<ll> s;
    cout << (*s.find_by_order(2)) << endl;
    cout << s.order_of_key(2) << endl;*/
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    ll ans = 0;
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> p(k);
    for (ll i = 0; i < n; i++) {
        pair<ll, ll> t;
        for (ll j = 0; j < k; j++) {
            if (max(p[j].first, a[i] + 1 + p[j].second) - p[j].first > t.first || (max(p[j].first, a[i] + 1 + p[j].second) - p[j].first == t.first && p[j].second > p[t.second].second)) {
                t.first = max(p[j].first, a[i] + 1 + p[j].second) - p[j].first;
                t.second = j;
            }
        }
        p[t.second].first += t.first;
        p[t.second].second++;
    }
    for (ll i = 0; i < k; i++) {
        ans += p[i].first;
    }
    p = vector<pair<ll, ll>> (k);
    ll tans = 0;
    for (ll i = 0; i < n; i++) {
        pair<ll, ll> t;
        for (ll j = 0; j < k; j++) {
            if (max(p[j].first, a[i] + 1 + p[j].second) - p[j].first > t.first) {
                t.first = max(p[j].first, a[i] + 1 + p[j].second) - p[j].first;
                t.second = j;
            }
        }
        p[t.second].first += t.first;
        p[t.second].second++;
    }
    for (ll i = 0; i < k; i++) {
        tans += p[i].first;
    }
    ans = max(ans, tans);
    cout << ans << endl;
    /*vector<vector<pair<ll, ll>>> d(n, vector<ll> (n + 1, 0));
    d[0][1] = a[0] + 1;
    d[0][0] = a[0] + 1;
    for (ll i = 1; i < n; i++) {
        for (ll j = 0; j < n; j++) {
            if (j > 0) {
                d[i][j] = max(d[i - 1][j] + a[i] + 1 + i - j, d[i - 1][j - 1] + a[i] + 1 + j - 1);
            } else {
                d[i][j] = d[i - 1][j] + a[i] + 1 + i - j;
            }
        }
    }
    for (ll i = 1; i < n; i++) {
        ans = max(ans, d[n - 1][i]);
    }
    cout << ans << endl;*/
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    ll q = 1;
    // cin >> q;
    while (q--) {
        solve();
    }
}
