#include <bits/stdc++.h> using namespace std; const int mx = 203; const long long inf = 1e18+4e3; long long ciag[mx], sum[mx], dp[mx][mx][2]; void prep(int n){ for(int i=1; i<=n; i++){ for(int j=i+1; j<=n; j++){ dp[i][j][0] = -2*inf; dp[i][j][1] = -2*inf; } } } int main(){ int n; long long m; scanf("%d %lld",&n, &m); for(int i=1; i<=n; i++){ scanf("%lld",&ciag[i]); sum[i] = sum[i-1] + ciag[i]; } prep(n); //printf("%d\n",__builtin_clz(m)); for(int b=0; b < 32 - __builtin_clz(m); b++){ long long res1, res2; for(int i=1; i<=n; i++){ for(int j=i; j<=n; j++){ res1 = dp[i][j][0]; res2 = dp[i][j][1]; for(int k = i; k <= j; k++){ if(dp[i][k-1][1] + dp[k][j][1] + sum[j] - sum[k-1] > -inf) res2 = max(res2, dp[i][k-1][1] + dp[k][j][1] + sum[j] - sum[k-1]); } if(((m >> b) & 1LL) < 0) continue; res1 = dp[i][j][1]; for(int k = i; k <= j; k++){ if(dp[i][k-1][1] + dp[k][j][0] + sum[j] - sum[k-1] > -inf) res1 = max(res2, dp[i][k-1][1] + dp[k][j][0] + sum[j] - sum[k-1]); } dp[i][j][0] = res1; } } for(int i=1; i<=n; i++){ for(int j=i; j<=n; j++){ dp[i][j][1] = res2; dp[i][j][0] = res1; } } } printf("%lld\n",dp[1][n][0]); 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 | #include <bits/stdc++.h> using namespace std; const int mx = 203; const long long inf = 1e18+4e3; long long ciag[mx], sum[mx], dp[mx][mx][2]; void prep(int n){ for(int i=1; i<=n; i++){ for(int j=i+1; j<=n; j++){ dp[i][j][0] = -2*inf; dp[i][j][1] = -2*inf; } } } int main(){ int n; long long m; scanf("%d %lld",&n, &m); for(int i=1; i<=n; i++){ scanf("%lld",&ciag[i]); sum[i] = sum[i-1] + ciag[i]; } prep(n); //printf("%d\n",__builtin_clz(m)); for(int b=0; b < 32 - __builtin_clz(m); b++){ long long res1, res2; for(int i=1; i<=n; i++){ for(int j=i; j<=n; j++){ res1 = dp[i][j][0]; res2 = dp[i][j][1]; for(int k = i; k <= j; k++){ if(dp[i][k-1][1] + dp[k][j][1] + sum[j] - sum[k-1] > -inf) res2 = max(res2, dp[i][k-1][1] + dp[k][j][1] + sum[j] - sum[k-1]); } if(((m >> b) & 1LL) < 0) continue; res1 = dp[i][j][1]; for(int k = i; k <= j; k++){ if(dp[i][k-1][1] + dp[k][j][0] + sum[j] - sum[k-1] > -inf) res1 = max(res2, dp[i][k-1][1] + dp[k][j][0] + sum[j] - sum[k-1]); } dp[i][j][0] = res1; } } for(int i=1; i<=n; i++){ for(int j=i; j<=n; j++){ dp[i][j][1] = res2; dp[i][j][0] = res1; } } } printf("%lld\n",dp[1][n][0]); return 0; } |