#include<bits/stdc++.h> using namespace std; #define int long long int dp[210][210][70][2]; bool visited[210][210][70][2]; int qual[210]; int pre[210]; int m,inf=1e18; int sums(int left, int right){ return pre[right]-pre[left-1]; } int solve(int left,int right,int bitsLeft,int finish){ if(visited[left][right][bitsLeft][finish])return dp[left][right][bitsLeft][finish]; visited[left][right][bitsLeft][finish]=1; if(bitsLeft==0){ if(left<right){ dp[left][right][bitsLeft][finish]=-inf; return -inf; } dp[left][right][bitsLeft][finish]=0; return 0; } if(left>right){ dp[left][right][bitsLeft][finish]=0; return 0; } if(!finish){ int ans=-inf; for(int i=left;i<=right+1;i++){ ans=max(ans,sums(i,right)+solve(left,i-1,bitsLeft-1,0)+solve(i,right,bitsLeft-1,0)); } dp[left][right][bitsLeft][finish]=ans; return ans; } if((1LL<<(bitsLeft-1))&m){ int ans=-inf; for(int i=left;i<=right+1;i++){ ans=max(ans,sums(i,right)+solve(left,i-1,bitsLeft-1,0)+solve(i,right,bitsLeft-1,1)); } dp[left][right][bitsLeft][finish]=ans; return ans; } else{ int ans=solve(left,right,bitsLeft-1,finish); dp[left][right][bitsLeft][finish]=ans; return ans; } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >>n >>m; for(int i=1;i<=n;i++){ cin >>pre[i]; pre[i]+=pre[i-1]; } pre[n+1]=n; cout <<solve(1,n,63,1); }
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 | #include<bits/stdc++.h> using namespace std; #define int long long int dp[210][210][70][2]; bool visited[210][210][70][2]; int qual[210]; int pre[210]; int m,inf=1e18; int sums(int left, int right){ return pre[right]-pre[left-1]; } int solve(int left,int right,int bitsLeft,int finish){ if(visited[left][right][bitsLeft][finish])return dp[left][right][bitsLeft][finish]; visited[left][right][bitsLeft][finish]=1; if(bitsLeft==0){ if(left<right){ dp[left][right][bitsLeft][finish]=-inf; return -inf; } dp[left][right][bitsLeft][finish]=0; return 0; } if(left>right){ dp[left][right][bitsLeft][finish]=0; return 0; } if(!finish){ int ans=-inf; for(int i=left;i<=right+1;i++){ ans=max(ans,sums(i,right)+solve(left,i-1,bitsLeft-1,0)+solve(i,right,bitsLeft-1,0)); } dp[left][right][bitsLeft][finish]=ans; return ans; } if((1LL<<(bitsLeft-1))&m){ int ans=-inf; for(int i=left;i<=right+1;i++){ ans=max(ans,sums(i,right)+solve(left,i-1,bitsLeft-1,0)+solve(i,right,bitsLeft-1,1)); } dp[left][right][bitsLeft][finish]=ans; return ans; } else{ int ans=solve(left,right,bitsLeft-1,finish); dp[left][right][bitsLeft][finish]=ans; return ans; } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >>n >>m; for(int i=1;i<=n;i++){ cin >>pre[i]; pre[i]+=pre[i-1]; } pre[n+1]=n; cout <<solve(1,n,63,1); } |