#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
    ll n, m;
    cin >> n >> m;
    vector <ll > a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll ans = 0;
    for (ll mask = 0; mask < (1 << n); mask++) {
        if (__builtin_popcount(mask) != m) {
            continue;
        }
        ll lid = 0, now = 0;
        for (ll i = 0; i < n; i++) {
            if ((mask >> i) & 1) {
                now += a[i];
                lid = i;
            }
        }
        now += lid + 1;
        ans = max(ans, now);
    }
    cout << ans << endl;
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    ll z; cin >> z; while(z--)
    solve();
    return 0;
}
