#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,avx,bmi2")
//#include "ext/pb_ds/assoc_container.hpp"
//#include "ext/pb_ds/tree_policy.hpp"
//using namespace __gnu_pbds;
//template<class T> using oredered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long

const int maxn = 1e5, inf = 8e18, mod = 1e9 + 7;

int p[9];

void solve() {
    int n, k;
    cin >> n >> k;
    p[0] = 1;
    for (int i = 1; i < 9; i++) p[i] = p[i - 1] * k;
    vector<int> a(n);
    for (int& i : a) cin >> i;
    int res = 0;
    int maskmax = p[n];
    for (int mask = 0; mask < maskmax; mask++) {
        vector<int> ind(n), mx(k);
        for (int i = 0; i < n; i++) {
            int z = (mask / p[i]) % k;
            ind[z]++;
            mx[z] = max(mx[z], a[i] + ind[z]);
        }
        res = max(res, accumulate(mx.begin(), mx.end(), 0ll));
    }
    cout << res << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
// wow1