#include <bits/stdc++.h> using namespace std; const int kN = 205; const int kB = 63; const long long kInf = static_cast<long long>(2e18); long long f[kB][kN][kN][2]; int n; long long m, a[kN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; a[i] += a[i - 1]; } for (int b = 0; b < 2; ++b) for (int l = 1; l <= n; ++l) for (int r = l; r <= n; ++r) for (int p = 0; p < 2; ++p) f[b][l][r][p] = (b == 0 && l == r) ? 0 : -kInf; for (int b = 0; b < kB; ++b) { int bit = (m >> b) & 1; for (int l = 1; l <= n; ++l) for (int r = l; r <= n; ++r) for (int p = 0; p < 2; ++p) for (int mid = l - 1; mid <= r; ++mid) { if (mid + 1 <= r && p && !bit) continue; long long& result = f[1][l][r][p]; long long cur_value = a[r] - a[mid]; cur_value += f[0][l][mid][mid == r ? (p && !bit) : 0]; cur_value += f[0][mid + 1][r][p]; result = max(result, cur_value); } for (int l = 1; l <= n; ++l) { for (int r = l; r <= n; ++r) { for (int p = 0; p < 2; ++p) { f[0][l][r][p] = f[1][l][r][p]; f[1][l][r][p] = -kInf; } } } } cout << f[0][1][n][1] << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; const int kN = 205; const int kB = 63; const long long kInf = static_cast<long long>(2e18); long long f[kB][kN][kN][2]; int n; long long m, a[kN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; a[i] += a[i - 1]; } for (int b = 0; b < 2; ++b) for (int l = 1; l <= n; ++l) for (int r = l; r <= n; ++r) for (int p = 0; p < 2; ++p) f[b][l][r][p] = (b == 0 && l == r) ? 0 : -kInf; for (int b = 0; b < kB; ++b) { int bit = (m >> b) & 1; for (int l = 1; l <= n; ++l) for (int r = l; r <= n; ++r) for (int p = 0; p < 2; ++p) for (int mid = l - 1; mid <= r; ++mid) { if (mid + 1 <= r && p && !bit) continue; long long& result = f[1][l][r][p]; long long cur_value = a[r] - a[mid]; cur_value += f[0][l][mid][mid == r ? (p && !bit) : 0]; cur_value += f[0][mid + 1][r][p]; result = max(result, cur_value); } for (int l = 1; l <= n; ++l) { for (int r = l; r <= n; ++r) { for (int p = 0; p < 2; ++p) { f[0][l][r][p] = f[1][l][r][p]; f[1][l][r][p] = -kInf; } } } } cout << f[0][1][n][1] << '\n'; } |