#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long const int maxN = 256; const int LG = 60; const ll inf = 3e18; ll dp[maxN][maxN][LG]; ll dp_[maxN][LG]; bool B[maxN][maxN][LG]; bool B_[maxN][LG]; ll sums[maxN]; int N, M, lg; inline ll sum(int a, int b) { return sums[b] - sums[a - 1]; } ll cnt(int a, int b, int l) { if (b - a + 1 > (1LL << l)) return -inf; ll& res = dp[a][b][l]; if (B[a][b][l]) return res; B[a][b][l] = 1; if (l == 1) { res = sum(b, b); if (a == b && res < 0) res = 0; return res; } res = max(0LL, sum(a, b)) + cnt(a, b, l - 1); for (int i = a; i < b; ++i) res = max(res, cnt(a, i, l - 1) + cnt(i + 1, b, l - 1) + sum(i + 1, b)); return res; } ll cnt_(int a, int l) { if (N - a + 1 > 1 + (((1LL << l) - 1) & M)) return -inf; ll& res = dp_[a][l]; if (B_[a][l]) return res; B_[a][l] = 1; if (l == 1) { res = sum(N, N) * (M & 1); if (a == N && res < 0) res = 0; return res; } if ((M & (1LL << (l - 1))) == 0) { return res = cnt_(a, l - 1); } res = max(cnt(a, N, l - 1), cnt_(a, l - 1) + sum(a, N)); for (int i = a; i < N; ++i) res = max(res, cnt(a, i, l - 1) + cnt_(i + 1, l - 1) + sum(i + 1, N)); return res; } #undef int int main() { #define int long long scanf("%lld%lld", &N, &M); if (M == 0) { puts("0"); return 0; } for (int i = 1; i <= N; ++i) { scanf("%lld", sums + i); sums[i] += sums[i - 1]; } while ((1LL << lg) <= M) lg++; printf("%lld\n", cnt_(1, lg)); }
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long const int maxN = 256; const int LG = 60; const ll inf = 3e18; ll dp[maxN][maxN][LG]; ll dp_[maxN][LG]; bool B[maxN][maxN][LG]; bool B_[maxN][LG]; ll sums[maxN]; int N, M, lg; inline ll sum(int a, int b) { return sums[b] - sums[a - 1]; } ll cnt(int a, int b, int l) { if (b - a + 1 > (1LL << l)) return -inf; ll& res = dp[a][b][l]; if (B[a][b][l]) return res; B[a][b][l] = 1; if (l == 1) { res = sum(b, b); if (a == b && res < 0) res = 0; return res; } res = max(0LL, sum(a, b)) + cnt(a, b, l - 1); for (int i = a; i < b; ++i) res = max(res, cnt(a, i, l - 1) + cnt(i + 1, b, l - 1) + sum(i + 1, b)); return res; } ll cnt_(int a, int l) { if (N - a + 1 > 1 + (((1LL << l) - 1) & M)) return -inf; ll& res = dp_[a][l]; if (B_[a][l]) return res; B_[a][l] = 1; if (l == 1) { res = sum(N, N) * (M & 1); if (a == N && res < 0) res = 0; return res; } if ((M & (1LL << (l - 1))) == 0) { return res = cnt_(a, l - 1); } res = max(cnt(a, N, l - 1), cnt_(a, l - 1) + sum(a, N)); for (int i = a; i < N; ++i) res = max(res, cnt(a, i, l - 1) + cnt_(i + 1, l - 1) + sum(i + 1, N)); return res; } #undef int int main() { #define int long long scanf("%lld%lld", &N, &M); if (M == 0) { puts("0"); return 0; } for (int i = 1; i <= N; ++i) { scanf("%lld", sums + i); sums[i] += sums[i - 1]; } while ((1LL << lg) <= M) lg++; printf("%lld\n", cnt_(1, lg)); } |