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

using namespace __gnu_pbds;
using namespace std;

template<class T>
using order_set = tree<T, nullptr_t, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;
using ld = long double;
using mll = map<ll, ll>;
using pll = pair<ll, ll>;
using vll = vector<ll>;

const ll inf = 1e16;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;

const ll nmax = 5e5 + 1;
ll n, k;


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    cin >> n >> k;
    vector<ll> a(n);
    for (ll &i: a)
        cin >> i;

    vll p;
    for (ll i = 0; i < n; i++)
        for (ll j = 0; j < k; j++)
            p.push_back(j);

    vll len(k), res(k), was(k);
    ll ans = 0;
    do {
        for (ll i = 0; i < k; i++) {
            len[i] = 1;
            res[i] = 0;
            was[i] = 0;
        }
        for (ll i = 0; i < n; i++) {
            was[p[i]]++;
            res[p[i]] = max(res[p[i]], a[i] + len[p[i]]++);
        }
        
        if (*min_element(was.begin(), was.end()) < k)
            continue;
        
        ans = max(ans, (ll) accumulate(res.begin(), res.end(), 0));
    } while (next_permutation(p.begin(), p.end()));
    cout << ans << '\n';
}