#ifdef LOCAL
#define _GLIBCXX_DEBUG
#else
#pragma GCC optimize("O3")
#endif

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

#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
using ld = long double;
using ll = int64_t;
using ull = uint64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;

void solve();

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while (t--) {
        solve();
    }
    return 0;
}

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<pii> sorted_a(n);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a[i] = x;
        sorted_a[i] = {x, i};
    }
    sort(rall(sorted_a));
    vector<vector<int>> indexes(k);
    int mx_i = 0;
    for (int i = 0; i < k; i++) {
        indexes[i] = {sorted_a[i].ss};
        if (sorted_a[i].ss > sorted_a[mx_i].ss) {
            mx_i = i;
        }
    }
    for (int i = k; i < n; i++) {
        if (sorted_a[i].ss < sorted_a[mx_i].ss)
            indexes[mx_i].push_back(sorted_a[i].ss);
        else
            indexes[k - 1].push_back(sorted_a[i].ss);
    }
    ll ans = 0;
    for (int i = 0; i < k; i++) {
        sort(all(indexes[i]));
        int now_ans = 0;
        for (int j = 0; j < indexes[i].size(); j++) {
            now_ans = max(now_ans, a[indexes[i][j]] + j + 1);
        }
        ans += now_ans;
    }
    cout << ans << '\n';
}