#include <bits/stdc++.h> using namespace std; typedef long long int LL; const int N = 207; const int M = 61; const LL MX = 1000LL * 1000 * 1000; const LL INF = 2LL * MX * MX; int n; LL in[N]; LL pref[N]; LL dp[M][N][N][2]; LL m; bool bit[M]; int main(){ scanf("%d %lld", &n, &m); for(int i = 1; i <= n; ++i) scanf("%lld", &in[i]), pref[i] = pref[i - 1] + in[i]; for(int i = M - 2; i >= 0; --i) if(m & (1LL << i)) bit[i + 1] = true; for(int i = 1; i <= n; ++i) for(int j = i + 1; j <= n; ++j) dp[0][i][j][0] = dp[0][i][j][1] = -INF; for(int t = 1; t < M; ++t){ for(int i = 1; i <= n; ++i) for(int j = i; j <= n; ++j){ { dp[t][i][j][0] = -INF; for(int k = i - 1; k <= j; ++k) dp[t][i][j][0] = max(dp[t][i][j][0], dp[t - 1][i][k][0] + dp[t - 1][k + 1][j][0] + pref[j] - pref[k]); } { dp[t][i][j][1] = dp[t - 1][i][j][1]; if(bit[t]) for(int k = i - 1; k <= j; ++k) dp[t][i][j][1] = max(dp[t][i][j][1], dp[t - 1][i][k][0] + dp[t - 1][k + 1][j][1] + pref[j] - pref[k]); } // printf("dp[%d][%d][%d][0] = %lld\n", t, i, j, dp[t][i][j][0]); // printf("dp[%d][%d][%d][1] = %lld\n", t, i, j, dp[t][i][j][1]); } } printf("%lld\n", dp[M - 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 53 54 55 56 57 58 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; const int N = 207; const int M = 61; const LL MX = 1000LL * 1000 * 1000; const LL INF = 2LL * MX * MX; int n; LL in[N]; LL pref[N]; LL dp[M][N][N][2]; LL m; bool bit[M]; int main(){ scanf("%d %lld", &n, &m); for(int i = 1; i <= n; ++i) scanf("%lld", &in[i]), pref[i] = pref[i - 1] + in[i]; for(int i = M - 2; i >= 0; --i) if(m & (1LL << i)) bit[i + 1] = true; for(int i = 1; i <= n; ++i) for(int j = i + 1; j <= n; ++j) dp[0][i][j][0] = dp[0][i][j][1] = -INF; for(int t = 1; t < M; ++t){ for(int i = 1; i <= n; ++i) for(int j = i; j <= n; ++j){ { dp[t][i][j][0] = -INF; for(int k = i - 1; k <= j; ++k) dp[t][i][j][0] = max(dp[t][i][j][0], dp[t - 1][i][k][0] + dp[t - 1][k + 1][j][0] + pref[j] - pref[k]); } { dp[t][i][j][1] = dp[t - 1][i][j][1]; if(bit[t]) for(int k = i - 1; k <= j; ++k) dp[t][i][j][1] = max(dp[t][i][j][1], dp[t - 1][i][k][0] + dp[t - 1][k + 1][j][1] + pref[j] - pref[k]); } // printf("dp[%d][%d][%d][0] = %lld\n", t, i, j, dp[t][i][j][0]); // printf("dp[%d][%d][%d][1] = %lld\n", t, i, j, dp[t][i][j][1]); } } printf("%lld\n", dp[M - 1][1][n][1]); return 0; } |