#include "bits/stdc++.h"
using namespace std;

#define forn(i,n) for(int i=0;i<n;++i)
#define fornn(i,l,r) for(int i=l;i<=r;++i)
#define ford(i,n) for(int i=n-1;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;

const int maxn=1e5+7;
const int logn = 32;
const int mod = 1e9 + 7;

int n,k; // 1 <= k <= n <= 5e5

vector<int> get(int x) {
    vector<int> a;
    while(x>0) {
        a.pb(x%8);
        x/=8;
    }
    while (a.size()<n) a.pb(0);
    return a;
}

void solve() {
    cin>>n>>k;
    vector<ll> a(n); // 1 <= a[i] <= 1e9
    forn(i,n) cin>>a[i];

    ll count = 1;
    forn(i,8) count *= 8;
    count=min(count, (ll)1e8);

    ll ans=0;
    forn(i,count) {
        vector<int> g=get(i);
        vector<int> index(8,1);
        vector<ll> mx(8,0);
        forn(j,n) {
            mx[g[j]]=max(mx[g[j]], a[j]+index[g[j]]);
            index[g[j]]++;
        }
        ll res=0;
        forn(j,k) res+=mx[j];
        ans=max(ans,res);
    }
    cout<<ans<<endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

#ifdef LOCAL
    freopen("C:\\work\\untitled\\d.txt", "r", stdin);
    freopen("C:\\work\\untitled\\out.txt", "w", stdout);

    while (!cin.eof()) {
        solve();
        cout<<endl;
    }
#else
    solve();
#endif

    return 0;
}
