#pragma GNU optimize("O3")
#pragma GNU optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define in insert
#define int long long
#define ll long long
#define inf 1000000000000000000
#define N 300000
int cnt[8], r[8];
int g(int n, int k){
    int res = 1;
    for(int i = 0; i < n; i++) res *= k;
    return res;
}
void tup(){
    int n, k; cin >> n >> k;
    vector<int> a(n);
    for(int &i: a) cin >> i;
    int ans = 0;
    for(int j = 0; j <= g(n, k); j++){
        int p = j, f = 0, res = 0;
        for(int i = 0; i < n; i++){
            if(cnt[p % k] == 0) f++;
            r[p % k] = max(r[p % k], a[i] + ++cnt[p% k]);
            p /= k;
        }
        p = j;
        for(int i = 0; i < k; i++){
            res += r[i];
            cnt[i] = 0;
            r[i] = 0;
        }
        if(f == k)
            ans = max(ans, res);
    }
    cout << ans;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    tup();
}