#include <bits/stdc++.h>

#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
mt19937_64 gen;
const ll mod = 1e9 + 7;
const ll INFL = 1e18;
ll binpow(ll a, ll n) {
    if(n == 0) return 1;
    else if(n % 2 == 1) {
        return a * binpow(a, n - 1) % mod;
    } else {
        ll b = binpow(a, n / 2) % mod;
        return b * b % mod;
    }
}
int main()
{
    //seed.time(gen());
    //clock_t start = clock();
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k; cin >> n >> k;
    vector<int>a(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    //vector<int>ans(n + 1);
    vector<int>p(n + 1);
    int cnt = 0;
    vector<int>kol(k + 1);
    for(int i = 1; i <= n; i++) {
        cnt++;
        if(cnt == (k + 1)) cnt = 1;
        p[i] = cnt;
        kol[cnt]++;
    }
    ld t = 1;
    function<int(int, int)> f = [&](int in, int c) {
        p[in] = c;
        kol[c]++;
        vector<vector<int>>g(k + 1);
        for(int i = 1; i <= n; i++) {
            g[p[i]].push_back(a[i]);
        }
        ll rs = 0;
        for(int i = 1; i <= k; i++) {
            ll res = 0;
            for(int j = 1; j <= g[i].size(); j++) {
                res = max(ll(g[i][j - 1] + j), res);
            }
            rs+=res;
        }
        return rs;
    };
    ll ans = f(1, 1);
    int itr = 1e5;
    while(itr--) {
        //t*=0.99;
        int ind, c;
        while(true) {
            ind = gen() % n + 1;
            c = gen() % k + 1;
            int pr = p[ind];
            if(kol[pr] > 1) break;
        }

        int pr = p[ind];
        ll ans2 = f(ind, c);

        if(ans2 > ans) {
            ans = ans2;
        } /*else {
            ld q2 = (gen() / INFL) % INFL; ld q = exp(ld(abs(ans - ans2)) / ld(t));
            if(q2 < q) {
                ans = ans2;
            } else {
                f(ind, pr);
            }
        }*/
    }
    cout << ans << "\n";


    return 0;
}
