#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

#define ll long long

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll x1 = 1;
    for (ll i = 0; i < n; i++) {
        x1 *= k;
    }
    ll answer1 = 0;
    for (ll i1 = 0; i1 < x1; i1++) {
        ll x = i1;
        vector<ll> s1(k);
        vector<ll> answer(k, -1e18);
        ll k1 = k;
        ll answer2 = 0;
        string s2 = "";
        for (ll i = 0; i < n; i++) {
            ll ind = x % k;
            if (answer[ind] != -1e18) {
                answer2 -= answer[ind];
            } else {
                k1--;
            }
            s2.push_back('0' + x % 2);
            s1[ind]++;
            answer[ind] = max(answer[ind], a[i] + s1[ind]);
            answer2 += answer[ind];
            x = x / k;
        }
        if (k1 != 0) {
            continue;
        }
        answer1 = max(answer1, answer2);
    }
    cout << answer1 << '\n';
}