#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n, k;
    cin >> n >> k;

    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<pair<ll, ll>> gr(k, {0, 0});
    for (ll i = 0; i < n; i++) {
        ll mx = -LONG_LONG_MAX, ind = 0;
        for (ll j = 0; j < k; j++) {
            if (a[i] + gr[j].first + 1 > gr[j].second) {
                if (mx < a[i] + gr[j].first + 1 - gr[j].second) {
                    mx = a[i] + gr[j].first + 1 - gr[j].second;
                    ind = j;
                }
            } else {
                if (mx < -a[i]) {
                    mx = -a[i];
                    ind = j;
                }
            }
        }

        gr[ind].first++;
        gr[ind].second = max(gr[ind].second, a[i] + gr[ind].first);
    }

    ll sum = 0;
    for (auto [e, mx] : gr) {
        sum += mx;
    }

    cout << sum << '\n';

    return 0;
}
