#include <bits/stdc++.h> using namespace std; const int MAX_N = 200; const long long INF = 1e18; int n; long long m, a[MAX_N + 5], sumPref[MAX_N + 5], dp[2][MAX_N + 5][MAX_N + 5][2]; int main() { scanf("%d %lld", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); sumPref[i] = sumPref[i - 1] + a[i]; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { for (int k = 0; k < 2; k++) { dp[1][i][j][k] = -INF; } } } for (int bit = 0; bit < 60; bit++) { int bitM2 = bit % 2; for (int from = n; from >= 1; from--) { for (int to = from; to <= n; to++) { for (int equalM = 0; equalM < 2; equalM++) { if (equalM == 1 && ((1LL << bit) & m) == 0) { dp[bitM2][from][to][equalM] = dp[1 - bitM2][from][to][equalM]; continue; } dp[bitM2][from][to][equalM] = dp[1 - bitM2][from][to][0]; for (int split = from; split <= to; split++) { dp[bitM2][from][to][equalM] = max(dp[bitM2][from][to][equalM], dp[1 - bitM2][from][split - 1][0] + dp[1 - bitM2][split][to][equalM] + sumPref[to] - sumPref[split - 1]); } } } } } printf("%lld\n", dp[1][1][n][1]); 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 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 200; const long long INF = 1e18; int n; long long m, a[MAX_N + 5], sumPref[MAX_N + 5], dp[2][MAX_N + 5][MAX_N + 5][2]; int main() { scanf("%d %lld", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); sumPref[i] = sumPref[i - 1] + a[i]; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { for (int k = 0; k < 2; k++) { dp[1][i][j][k] = -INF; } } } for (int bit = 0; bit < 60; bit++) { int bitM2 = bit % 2; for (int from = n; from >= 1; from--) { for (int to = from; to <= n; to++) { for (int equalM = 0; equalM < 2; equalM++) { if (equalM == 1 && ((1LL << bit) & m) == 0) { dp[bitM2][from][to][equalM] = dp[1 - bitM2][from][to][equalM]; continue; } dp[bitM2][from][to][equalM] = dp[1 - bitM2][from][to][0]; for (int split = from; split <= to; split++) { dp[bitM2][from][to][equalM] = max(dp[bitM2][from][to][equalM], dp[1 - bitM2][from][split - 1][0] + dp[1 - bitM2][split][to][equalM] + sumPref[to] - sumPref[split - 1]); } } } } } printf("%lld\n", dp[1][1][n][1]); return 0; } |