#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define int long long

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, k;
vector<int> a;
vector<pair<int, int>> b;

int f(int x) {
    vector<vector<int>> c(k);
    int i = 0;
    for (int i = 0; i < n; i++) {
        c[x % k].push_back(a[i]);
        x /= k;
    }
    int res = 0;
    for (auto &x: c) {
        if (x.empty())
            return 0;
        int cur = 0;
        for (int i = 0; i < x.size(); i++)
            cur = max(cur, x[i] + i + 1);
        res += cur;
    }
    return res;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k;
    a.resize(n);
    for (auto &x: a) cin >> x;
    int l = pow(n, k);
    int res = 0;
    for (int i = 0; i < l; i++)
        res = max(res, f(i));
    cout << res << '\n';
    return 0;
}
