/* Notice: - This account submits solutions that result from the collaboration of two (non-Polish) individuals. - We acknowledge that our participation is completely unofficial. - We just want to be able to submit our solutions until the end of the week. Thank you :)! */ #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long int64; const int kMaxN = 210; const int kMaxB = 63; const int64 kInf = (1LL<<61); int64 dp[kMaxB + 1][kMaxN][kMaxN][2]; int64 a[kMaxN], pre[kMaxN]; void Reset() { } bool Solve() { Reset(); int n; long long m; if (!(cin >> n >> m)) { return false; } for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { pre[i] = pre[i - 1] + a[i]; } // Valid base cases are segments of 1 element, using 0 bits. for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { dp[0][i][j][false] = dp[0][i][j][true] = -kInf; } dp[0][i][i][false] = dp[0][i][i][true] = 0; } for (int bit = 1; bit <= kMaxB; ++bit) { for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { // Put all zeros or all ones. dp[bit][i][j][false] = max( dp[bit - 1][i][j][false], dp[bit - 1][i][j][false] + pre[j] - pre[i - 1]); if ((m >> (bit - 1))&1) { dp[bit][i][j][true] = max(dp[bit - 1][i][j][false], dp[bit - 1][i][j][true] + pre[j] - pre[i - 1]); } else { dp[bit][i][j][true] = dp[bit - 1][i][j][true]; } // Split into 2 sections. for (int k = i; k < j; ++k) { // Split into [i, k], [k + 1, j]. dp[bit][i][j][false] = max(dp[bit][i][j][false], dp[bit - 1][i][k][false] + dp[bit - 1][k + 1][j][false] + pre[j] - pre[k]); if ((m >> (bit - 1))&1) { dp[bit][i][j][true] = max(dp[bit][i][j][true], dp[bit - 1][i][k][false] + dp[bit - 1][k + 1][j][true] + pre[j] - pre[k]); } } } } } cout << dp[kMaxB][1][n][true] << "\n"; return true; } int main() { while(Solve()); }
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 | /* Notice: - This account submits solutions that result from the collaboration of two (non-Polish) individuals. - We acknowledge that our participation is completely unofficial. - We just want to be able to submit our solutions until the end of the week. Thank you :)! */ #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long int64; const int kMaxN = 210; const int kMaxB = 63; const int64 kInf = (1LL<<61); int64 dp[kMaxB + 1][kMaxN][kMaxN][2]; int64 a[kMaxN], pre[kMaxN]; void Reset() { } bool Solve() { Reset(); int n; long long m; if (!(cin >> n >> m)) { return false; } for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { pre[i] = pre[i - 1] + a[i]; } // Valid base cases are segments of 1 element, using 0 bits. for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { dp[0][i][j][false] = dp[0][i][j][true] = -kInf; } dp[0][i][i][false] = dp[0][i][i][true] = 0; } for (int bit = 1; bit <= kMaxB; ++bit) { for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { // Put all zeros or all ones. dp[bit][i][j][false] = max( dp[bit - 1][i][j][false], dp[bit - 1][i][j][false] + pre[j] - pre[i - 1]); if ((m >> (bit - 1))&1) { dp[bit][i][j][true] = max(dp[bit - 1][i][j][false], dp[bit - 1][i][j][true] + pre[j] - pre[i - 1]); } else { dp[bit][i][j][true] = dp[bit - 1][i][j][true]; } // Split into 2 sections. for (int k = i; k < j; ++k) { // Split into [i, k], [k + 1, j]. dp[bit][i][j][false] = max(dp[bit][i][j][false], dp[bit - 1][i][k][false] + dp[bit - 1][k + 1][j][false] + pre[j] - pre[k]); if ((m >> (bit - 1))&1) { dp[bit][i][j][true] = max(dp[bit][i][j][true], dp[bit - 1][i][k][false] + dp[bit - 1][k + 1][j][true] + pre[j] - pre[k]); } } } } } cout << dp[kMaxB][1][n][true] << "\n"; return true; } int main() { while(Solve()); } |