#include <iostream>
#include <set>
#include <vector>
#include <random>
#include <iomanip>
//#include <fstream>
#include <algorithm>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
#define int long long
int get(vector<int> b) {
    int mxv = 0;
    for (int i = 0; i < b.size(); i++) {
        mxv = max(mxv, i + b[i] + 1);
    }
    return mxv;
}
signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false); 
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<pair<int, int>> pr(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        pr[i] = { a[i], i };
    }
    sort(pr.rbegin(), pr.rend());
    multiset<int> gd;
    for (int i = k; i < n; i++) {
        gd.insert(pr[i].first);
    }
    vector<bool> used(n);
    int ans = 0;
    for (int i = 0; i < k; i++) {
        int mxv = -1, id = -1;
        for (int j = n - 1; j > -1; j--) {
            if (!used[j] and a[j] > mxv) {
                mxv = a[j];
                id = j;
            }
        }
        vector<int> b;
        if (i == k - 1) {
            for (int j = 0; j < n; j++) {
                if (!used[j]) {
                    b.push_back(a[j]);
                }
            }
        }
        else {
            for (int j = id; j > -1; j--) {
                if (!used[j]) {
                    if (j == id or (gd.find(a[j]) != gd.end())) {
                        b.push_back(a[j]);
                        used[j] = true;
                        if (j != id) {
                            gd.erase(a[j]);
                        }
                    }
                }
            }
            reverse(b.begin(), b.end());
        }
       
       
        ans += get(b);
    }
    cout << ans << '\n';
}