#include <bits/stdc++.h> using namespace std; #define PII pair<int,int> long long a[205]; long long dp[205][205][70]; long long pref[205]; long long DP[205][70]; long long M[70]; int main() { ios_base::sync_with_stdio(0); int n; long long m; cin>>n>>m; m++; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)pref[i]=pref[i-1]+a[i]; for(int k=1;k<=65;k++) { for(int d=0;d<n;d++) { for(int x=1;x<=n-d;x++) { dp[x][x+d][k]=-2*1e18; if((long long)d+1>((long long)1<<k))continue; if((long long)d+1<=((long long)1<<(k-1)))dp[x][x+d][k]=max(dp[x][x+d][k-1],dp[x][x+d][k-1]+pref[x+d]-pref[x-1]); for(int i=x-1;i<=x+d;i++) { if((long long)i-x+1>((long long)1<<(k-1))||(long long)x+d-i>((long long)1<<(k-1)))continue; dp[x][x+d][k]=max(dp[x][x+d][k],dp[x][i][k-1]+dp[i+1][x+d][k-1]+pref[x+d]-pref[i]); } } } } /* for(int k=0;k<3;k++) { cout<<k<<endl; for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++)cout<<dp[i][j][k]<<" "; cout<<endl; }cout<<endl; }*/ int cnt=0; // long long M=0; for(int k=60;k>=0;k--) { M[k]=M[k+1]; if((m&((long long)1<<k))!=0) { M[k]+=((long long)1<<k); for(int i=1;i<=n;i++) { DP[i][k]=-2*1e18; if((long long)i>M[k])continue; if((long long)i<=M[k+1])DP[i][k]=max(DP[i][k],DP[i][k+1]); // if((long long)i<=((long long)1<<k))DP[i][k]=max(DP[i][k],dp[1][i]) for(int j=0;j<=i;j++) { if((long long)i-j>((long long)1<<k)||(long long)j>M[k+1])continue; DP[i][k]=max(DP[i][k],DP[j][k+1]+dp[j+1][i][k]+cnt*(pref[i]-pref[j])); } } cnt++; } else for(int i=1;i<=n;i++)DP[i][k]=DP[i][k+1]; /* if(k<3) { cout<<k<<" "<<cnt<<endl; for(int i=0;i<=n;i++)cout<<DP[i][k]<<" "; cout<<endl<<endl; }*/ } cout<<DP[n][0]<<endl; }
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 74 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; #define PII pair<int,int> long long a[205]; long long dp[205][205][70]; long long pref[205]; long long DP[205][70]; long long M[70]; int main() { ios_base::sync_with_stdio(0); int n; long long m; cin>>n>>m; m++; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)pref[i]=pref[i-1]+a[i]; for(int k=1;k<=65;k++) { for(int d=0;d<n;d++) { for(int x=1;x<=n-d;x++) { dp[x][x+d][k]=-2*1e18; if((long long)d+1>((long long)1<<k))continue; if((long long)d+1<=((long long)1<<(k-1)))dp[x][x+d][k]=max(dp[x][x+d][k-1],dp[x][x+d][k-1]+pref[x+d]-pref[x-1]); for(int i=x-1;i<=x+d;i++) { if((long long)i-x+1>((long long)1<<(k-1))||(long long)x+d-i>((long long)1<<(k-1)))continue; dp[x][x+d][k]=max(dp[x][x+d][k],dp[x][i][k-1]+dp[i+1][x+d][k-1]+pref[x+d]-pref[i]); } } } } /* for(int k=0;k<3;k++) { cout<<k<<endl; for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++)cout<<dp[i][j][k]<<" "; cout<<endl; }cout<<endl; }*/ int cnt=0; // long long M=0; for(int k=60;k>=0;k--) { M[k]=M[k+1]; if((m&((long long)1<<k))!=0) { M[k]+=((long long)1<<k); for(int i=1;i<=n;i++) { DP[i][k]=-2*1e18; if((long long)i>M[k])continue; if((long long)i<=M[k+1])DP[i][k]=max(DP[i][k],DP[i][k+1]); // if((long long)i<=((long long)1<<k))DP[i][k]=max(DP[i][k],dp[1][i]) for(int j=0;j<=i;j++) { if((long long)i-j>((long long)1<<k)||(long long)j>M[k+1])continue; DP[i][k]=max(DP[i][k],DP[j][k+1]+dp[j+1][i][k]+cnt*(pref[i]-pref[j])); } } cnt++; } else for(int i=1;i<=n;i++)DP[i][k]=DP[i][k+1]; /* if(k<3) { cout<<k<<" "<<cnt<<endl; for(int i=0;i<=n;i++)cout<<DP[i][k]<<" "; cout<<endl<<endl; }*/ } cout<<DP[n][0]<<endl; } |