#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <chrono>
using namespace std;
using ll = long long;
using lld = long double;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif;
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int msk = pow(k, n);
    int ans = 0;
    vector<int> w;
    for (int m = 0; m < msk; ++m) {
        vector<int> p(n);
        int f = m, ss = 0;
        while (f > 0) {
            p[ss] = f % k;
            ss++;
            f /= k;
        }
        vector<int> cnt(k), kans(k);
        for (int i = 0; i < n; ++i) {
            cnt[p[i]]++;
            kans[p[i]] = max(kans[p[i]], cnt[p[i]] + a[i]);
        }
        int res = 0;
        bool good = true;
        for (int i = 0; i < k; ++i) {
            res += kans[i];
            if (cnt[i] == 0)
                good = false;
        }
        if (good && res > ans) {
            ans = res;
            w = p;
        }
    }
    cout << ans << '\n';
}