#define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; const int N = 205, S = 60; const long long INF = -2e18; int n; long long m[S], dp1[N][N][S], dp2[N][N][S], pref[N]; int main() { scanf("%d%lld", &n, &m[S-1]); for(long long i=S-2;i>=0;i--) if(m[i+1] & (1LL<<(i+1LL))) m[i] = m[i+1] - (1LL<<(i+1LL)); else m[i] = m[i+1]; for(int i=1;i<=n;i++) { scanf("%lld", &pref[i]); pref[i] += pref[i-1]; } for(int i=1;i<=n;i++) for(int j=i+2;j<=n;j++) dp1[i][j][0] = INF; for(int i=1;i<=n;i++) { dp1[i][i][0] = max(0LL, pref[i] - pref[i-1]); dp1[i][i+1][0] = pref[i+1] - pref[i]; } for(int bit=1;bit<S;bit++) for(int d=0;d<n;d++) for(int p=1;p<=n-d;p++) { int k = p + d; dp1[p][k][bit] = INF; for(int l=p;l<=k+1;l++) dp1[p][k][bit] = max(dp1[p][k][bit], dp1[p][l-1][bit-1] + dp1[l][k][bit-1] + pref[k] - pref[l-1]); //cout<<p<<" "<<k<<" "<<bit<<" "<<dp1[p][k][bit]<<endl; } for(int i=1;i<=n;i++) for(int j=i+2;j<=n;j++) dp2[i][j][0] = INF; for(int i=1;i<=n;i++) { dp2[i][i][0] = 0; if(m[0] % 2) dp2[i][i][0] = max(0LL, pref[i] - pref[i-1]); if(i != n) { if(m[0] % 2) dp2[i][i+1][0] = pref[i+1] - pref[i]; else dp2[i][i+1][0] = INF; } } for(int bit=1;bit<S;bit++) for(int d=0;d<n;d++) for(int p=1;p<=n-d;p++) { int k = p + d; dp2[p][k][bit] = INF; long long xx = m[bit] & (1LL<<(long long)bit); if(!xx) { dp2[p][k][bit] = dp2[p][k][bit-1]; //cout<<p<<" "<<k<<" "<<bit<<" "<<dp2[p][k][bit]<<endl; continue; } for(int l=p;l<=k+1;l++) dp2[p][k][bit] = max(dp2[p][k][bit], dp1[p][l-1][bit-1] + dp2[l][k][bit-1] + pref[k] - pref[l-1]); //cout<<p<<" "<<k<<" "<<bit<<" "<<dp2[p][k][bit]<<endl; } printf("%lld\n", dp2[1][n][S-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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; const int N = 205, S = 60; const long long INF = -2e18; int n; long long m[S], dp1[N][N][S], dp2[N][N][S], pref[N]; int main() { scanf("%d%lld", &n, &m[S-1]); for(long long i=S-2;i>=0;i--) if(m[i+1] & (1LL<<(i+1LL))) m[i] = m[i+1] - (1LL<<(i+1LL)); else m[i] = m[i+1]; for(int i=1;i<=n;i++) { scanf("%lld", &pref[i]); pref[i] += pref[i-1]; } for(int i=1;i<=n;i++) for(int j=i+2;j<=n;j++) dp1[i][j][0] = INF; for(int i=1;i<=n;i++) { dp1[i][i][0] = max(0LL, pref[i] - pref[i-1]); dp1[i][i+1][0] = pref[i+1] - pref[i]; } for(int bit=1;bit<S;bit++) for(int d=0;d<n;d++) for(int p=1;p<=n-d;p++) { int k = p + d; dp1[p][k][bit] = INF; for(int l=p;l<=k+1;l++) dp1[p][k][bit] = max(dp1[p][k][bit], dp1[p][l-1][bit-1] + dp1[l][k][bit-1] + pref[k] - pref[l-1]); //cout<<p<<" "<<k<<" "<<bit<<" "<<dp1[p][k][bit]<<endl; } for(int i=1;i<=n;i++) for(int j=i+2;j<=n;j++) dp2[i][j][0] = INF; for(int i=1;i<=n;i++) { dp2[i][i][0] = 0; if(m[0] % 2) dp2[i][i][0] = max(0LL, pref[i] - pref[i-1]); if(i != n) { if(m[0] % 2) dp2[i][i+1][0] = pref[i+1] - pref[i]; else dp2[i][i+1][0] = INF; } } for(int bit=1;bit<S;bit++) for(int d=0;d<n;d++) for(int p=1;p<=n-d;p++) { int k = p + d; dp2[p][k][bit] = INF; long long xx = m[bit] & (1LL<<(long long)bit); if(!xx) { dp2[p][k][bit] = dp2[p][k][bit-1]; //cout<<p<<" "<<k<<" "<<bit<<" "<<dp2[p][k][bit]<<endl; continue; } for(int l=p;l<=k+1;l++) dp2[p][k][bit] = max(dp2[p][k][bit], dp1[p][l-1][bit-1] + dp2[l][k][bit-1] + pref[k] - pref[l-1]); //cout<<p<<" "<<k<<" "<<bit<<" "<<dp2[p][k][bit]<<endl; } printf("%lld\n", dp2[1][n][S-1]); return 0; } |