#include <bits/stdc++.h> using namespace std; long long dp[207][207][60]; bool obl_dp[207][207][60]; long long dp2[207][207][60]; bool obl_dp2[207][207][60]; long long tab[207]; int policz(long long m) { return 63 - __builtin_clzll(m); } bool kk(int a, int b, long long m) { return b - a + 1 <= m; } long long rozp(int a, int b, long long m) { if (b < a || m == 1) return 0LL; int bit = policz(m); long long war = 1LL << bit; long long drugi; bool pot = war == m; if (!pot) { drugi = m - war; if (obl_dp2[a][b][bit]) return dp2[a][b][bit]; } else { drugi = war = war / 2; if (obl_dp[a][b][bit]) return dp[a][b][bit]; } long long wyn = LLONG_MIN; for (int sr = a; sr <= b + 1; sr++) if (kk(a, sr - 1, war) && kk(sr, b, m - war)) wyn = max(wyn, rozp(a, sr - 1, war) + tab[b] - tab[sr - 1] + rozp(sr, b, drugi)); if (!pot) { obl_dp2[a][b][bit] = true; return dp2[a][b][bit] = wyn; } else { obl_dp[a][b][bit] = true; return dp[a][b][bit] = wyn; } } int main() { int N; long long M; scanf("%d%lld", &N, &M); for (int i = 1; i <= N; i++) { scanf("%lld", &tab[i]); tab[i] += tab[i - 1]; } printf("%lld\n", rozp(1, N, M + 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 53 54 55 56 57 | #include <bits/stdc++.h> using namespace std; long long dp[207][207][60]; bool obl_dp[207][207][60]; long long dp2[207][207][60]; bool obl_dp2[207][207][60]; long long tab[207]; int policz(long long m) { return 63 - __builtin_clzll(m); } bool kk(int a, int b, long long m) { return b - a + 1 <= m; } long long rozp(int a, int b, long long m) { if (b < a || m == 1) return 0LL; int bit = policz(m); long long war = 1LL << bit; long long drugi; bool pot = war == m; if (!pot) { drugi = m - war; if (obl_dp2[a][b][bit]) return dp2[a][b][bit]; } else { drugi = war = war / 2; if (obl_dp[a][b][bit]) return dp[a][b][bit]; } long long wyn = LLONG_MIN; for (int sr = a; sr <= b + 1; sr++) if (kk(a, sr - 1, war) && kk(sr, b, m - war)) wyn = max(wyn, rozp(a, sr - 1, war) + tab[b] - tab[sr - 1] + rozp(sr, b, drugi)); if (!pot) { obl_dp2[a][b][bit] = true; return dp2[a][b][bit] = wyn; } else { obl_dp[a][b][bit] = true; return dp[a][b][bit] = wyn; } } int main() { int N; long long M; scanf("%d%lld", &N, &M); for (int i = 1; i <= N; i++) { scanf("%lld", &tab[i]); tab[i] += tab[i - 1]; } printf("%lld\n", rozp(1, N, M + 1)); return 0; } |