#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using ll = long long;
using namespace __gnu_pbds;
using ord_set = tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll ans = 0;
    for (int mask = 0; mask < (pow(k, n)); mask++) {
        int x = mask;
        vector<int> b;
        vector<int> used(k);
        while (b.size() < n) {
            b.push_back(x % k);
            used[x % k] = 1;
            x /= k;
        }
        int cnt = 0;
        for (int i = 0; i < k; i++) {
            cnt += used[i];
        }
        if (cnt < k) {
            continue;
        }
        reverse(b.begin(), b.end());
        vector<int> c(k), mx(k);
        for (int i = 0; i < n; i++) {
            c[b[i]]++;
            mx[b[i]] = max(mx[b[i]], a[i] + c[b[i]]);
        }
        ll cur = 0;
        for (auto u : mx) cur += u;
        ans = max(ans, cur);
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int tt = 1;
//    cin >> tt;
    while (tt--) {
        solve();
    }
}
