1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;

constexpr int maxn = 201, maxk = 62;
constexpr long long INF = 3LL * 1000000000LL * 1000000000LL;

int n, k;
long long m;
long long quality[maxn], pref[maxn];
long long dp[maxk][maxn][maxn], fin[maxk][maxn];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;

    if(m == 0) {
        cout << "0\n";
        return 0;
    }

    k = __lg(m);
    for(int i = 0; i < n; i++)
        cin >> quality[i];

    partial_sum(quality, quality + n, pref + 1);
    auto get_sum = [&](int l, int r) {
        return pref[r] - pref[l];
    };

    for(int i = 0; i < n; i++) {
        fill(dp[0][i], dp[0][i] + n + 1, -INF);
        dp[0][i][i] = dp[0][i][i + 1] = 0;
    }

    for(int lg = 1; lg <= k; lg++) {
        for(int len = 1; len <= n; len++) {
            for(int a = 0; a + len <= n; a++) {
                auto b = a + len;
                dp[lg][a][b] = -INF;
                for(int mid = a; mid <= b; mid++) {
                    dp[lg][a][b] = max(dp[lg][a][b],
                                       dp[lg - 1][a][mid] + dp[lg - 1][mid][b] + get_sum(mid, b));
                }
            }
        }
    }

    copy(dp[k][0], dp[k][0] + n + 1, fin[k]);

    for(int i = k - 1, cnt = 0; i >= 0; i--) {
        if((m & (1LL << i)) == 0) {
            copy(fin[i + 1], fin[i + 1] + n + 1, fin[i]);
        } else {
            cnt += 1;
            for(int j = 0; j <= n; j++) {
                fin[i][j] = fin[i + 1][j];
                for(int l = j; l >= 0; l--) {
                    fin[i][j] = max(fin[i][j], fin[i + 1][l] + dp[i][l][j] + get_sum(l, j) * cnt);
                }
            }
        }
    }

    cout << max(fin[0][n], fin[0][n - 1] + quality[n - 1] * __builtin_popcountll(m)) << "\n";
}