#include <iostream>
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define st first
#define nd second

/**
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
**/

int a[500'500];

int pow(int a, int n)
{
    int ans = 1;
    while (n)
    {
        if (n & 1)
        {
            ans *= a;
        }
        a *= a;
        n >>= 1;
    }
    return ans;
}

signed main()
{
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    vector<int> z(n + 1);
    int cnt[k] = {};
    int mxx[k] = {};
    int ans = 0;
    for (int i = 0; i < pow(k, n); ++i)
    {
        //for (int j = 0; j < n; ++j)
        //{
        //    printf("%d", z[j]);
        //}

        for (int j = 0; j < k; ++j)
        { cnt[j] = 1; mxx[j] = 0; }

        for (int j = 0; j < n; ++j)
        {
            mxx[z[j]] = max(mxx[z[j]], a[j] + cnt[z[j]]++);
        }
        int sum = 0;
        int qqq = 0;
        for (int j = 0; j < k; ++j)
        {
            sum += mxx[j];
            qqq += !!mxx[j];
        }
        //printf(" %d\n", sum);
        if (qqq == k) { ans = max(ans, sum); }

        ++z[0];
        for (int j = 1; j <= n; ++j)
        {
            z[j] += z[j - 1] / k;
            z[j - 1] %= k;
        }
    }

    cout << ans << '\n';
    return 0;
}
