#include <bits/stdc++.h> using namespace std; const int N = 200; const int BITS = 60; const long long INF = 2e18; long long dp[BITS + 1][2][N][N]; void maxify(long long &x, long long val) { x = max(x, val); } long long solve(int n, long long m, const vector <long long> &a) { for (int bits = 0; bits <= BITS; bits++) for (int ub = 0; ub < 2; ub++) { for (int l = 0; l < n; l++) for (int r = l; r < n; r++) { dp[bits][ub][l][r] = -INF; } } for (int ub = 0; ub < 2; ub++) for (int l = 0; l < n; l++) { dp[0][ub][l][l] = 0; } vector <bool> mBit(BITS); for (int bit = 0; bit < BITS; bit++) { mBit[bit] = m & (1LL << bit); } for (int bits = 0; bits < BITS; bits++) for (int ub = 0; ub < 2; ub++) { bool onlyZeros = ub && !mBit[bits]; for (int l = 0; l < n; l++) for (int r = l; r < n; r++) { long long sumRight = 0; for (int mid = r; mid >= l - 1; mid--) { if (onlyZeros && mid < r) break; long long here = sumRight; if (mid >= l) { sumRight += a[mid]; } if (mid >= l) { int ubLeft = ub && !mBit[bits]; long long hereLeft = dp[bits][ubLeft][l][mid]; if (hereLeft == -INF) { continue; } here += hereLeft; } if (mid < r) { int ubRight = ub; long long hereRight = dp[bits][ubRight][mid + 1][r]; if (hereRight == -INF) { continue; } here += hereRight; } maxify(dp[bits + 1][ub][l][r], here); } } } return dp[BITS][1][0][n-1]; } int main() { ios_base::sync_with_stdio(false); int n; long long m; cin >> n >> m; vector <long long> a(n); for (auto &ai : a) { cin >> ai; } cout << solve(n, m, a); return 0; }
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; const int N = 200; const int BITS = 60; const long long INF = 2e18; long long dp[BITS + 1][2][N][N]; void maxify(long long &x, long long val) { x = max(x, val); } long long solve(int n, long long m, const vector <long long> &a) { for (int bits = 0; bits <= BITS; bits++) for (int ub = 0; ub < 2; ub++) { for (int l = 0; l < n; l++) for (int r = l; r < n; r++) { dp[bits][ub][l][r] = -INF; } } for (int ub = 0; ub < 2; ub++) for (int l = 0; l < n; l++) { dp[0][ub][l][l] = 0; } vector <bool> mBit(BITS); for (int bit = 0; bit < BITS; bit++) { mBit[bit] = m & (1LL << bit); } for (int bits = 0; bits < BITS; bits++) for (int ub = 0; ub < 2; ub++) { bool onlyZeros = ub && !mBit[bits]; for (int l = 0; l < n; l++) for (int r = l; r < n; r++) { long long sumRight = 0; for (int mid = r; mid >= l - 1; mid--) { if (onlyZeros && mid < r) break; long long here = sumRight; if (mid >= l) { sumRight += a[mid]; } if (mid >= l) { int ubLeft = ub && !mBit[bits]; long long hereLeft = dp[bits][ubLeft][l][mid]; if (hereLeft == -INF) { continue; } here += hereLeft; } if (mid < r) { int ubRight = ub; long long hereRight = dp[bits][ubRight][mid + 1][r]; if (hereRight == -INF) { continue; } here += hereRight; } maxify(dp[bits + 1][ub][l][r], here); } } } return dp[BITS][1][0][n-1]; } int main() { ios_base::sync_with_stdio(false); int n; long long m; cin >> n >> m; vector <long long> a(n); for (auto &ai : a) { cin >> ai; } cout << solve(n, m, a); return 0; } |