#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("02")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> sz(k,  0);
    vector<int> res(k, 0);
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        int d = -1;
        int best = -1;
        for (int j = 0; j < k; j++){
            if (max(res[j], x + sz[j] + 1) - res[j]> d){
                d = max(res[j], x + sz[j] + 1) - res[j];
                best = j;
            } else if (max(res[j], x + sz[j] + 1) == d && (best == -1 || sz[j] > sz[best])){
                best = j;
            }
        }
        //cout << best << "\n";
        sz[best]++;
        res[best] = max(res[best], x + sz[best]);
    }
    int ans = 0;
    for (int i = 0 ; i < k; i++){
        ans += res[i];
    }
    cout << ans;
}
