#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define all(vec) vec.begin(), vec.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

void solve() {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (auto &now : a) cin >> now;
    vector<vector<ll>> mas(k);
    reverse(all(a));
    int dost = -1;
    for (auto now : a) {
        int ind = -1;
        for (int i = 0; i <= dost; i++) {
            if (mas[i].back() >= now && (ind == -1 || mas[i].back() < mas[ind].back())) ind = i;
        }
        if (ind != -1) {
            mas[ind].push_back(now);
        } else {
            if (dost == k - 1) {
                mas[0].push_back(now);
            } else {
                dost++;
                mas[dost].push_back(now);
            }
        }
    }
    for (int i = 0; i <= dost; i++) {
        reverse(all(mas[i]));
    }
    for (int i = dost + 1; i < k; i++) {
        int ind = -1, best = 0;
        for (int j = 0; j <= dost; j++) {
            if (mas[j].size() == 1) continue;
            if (ind == -1 || mas[j][mas[j].size() - 2] >= best) {
                ind = j;
                best = mas[j][mas[j].size() - 2];
            }
        }
        swap(mas[ind][mas[ind].size() - 1], mas[ind][mas[ind].size() - 2]);
        mas[i].push_back(mas[ind].back());
        mas[ind].pop_back();
    }
    ll ans = 0;
    for (int i = 0; i < k; i++) {
        ll mx = 0;
        assert(mas[i].size() > 0);
        for (int j = 0; j < mas[i].size(); j++) {
            mx = max(mx, mas[i][j] + j + 1);
        }
        ans += mx;
    }
    cout << ans << "\n";
}

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