#include <bits/stdc++.h>
#include <bits/extc++.h>

#pragma GCC target("avx2")
#ifndef ABOBA
#pragma GCC optimize("O3")
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = int;
using ld = long double;
using ull = unsigned long long;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll inf = 1e9, mod = 1e9 + 7;

void solve() {
    ld TIME1 = clock();
    random_device rd;
    mt19937 rnd(rd());
    ll n, k;
    long long ans = 0;
    cin >> n >> k;
    vector<ll> a(n);
    for (auto &i: a) {
        cin >> i;
    }
    while ((clock() - TIME1) / CLOCKS_PER_SEC < 0.9) {
        long long cur_ans = 0;
        vector<vector<ll>> cur(k);
        for (ll i = 0; i < n; ++i) {
            cur[rnd() % k].emplace_back(a[i]);
        }
        for (ll i = 0; i < k; ++i) {
            if (cur[i].empty()) {
                break;
            }
            ll cnt = 0;
            for (ll j = 0; j < cur[i].size(); ++j) {
                cnt = max(cnt, cur[i][j] + j + 1);
            }
            cur_ans += cnt;
        }
        ans = max(ans, cur_ans);
    }
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll tt = 1;
    while (tt--) solve();
}//