#include<cstdio> typedef long long ll; const int N=67,M=205; const ll inf=3000000000000000000LL; int n,len,i,b[N];ll m,a[M],s[M],f[N][2][M][M];bool v[N][2][M][M]; inline void up(ll&a,ll b){a<b?(a=b):0;} ll dfs(int x,int y,int l,int r){ if(l>r)return 0; if(!x)return l==r?0:-inf; if(v[x][y][l][r])return f[x][y][l][r]; v[x][y][l][r]=1; ll tmp=-inf; int ny=y; if(y==1&&b[x]==1)ny=0; up(tmp,dfs(x-1,ny,l,r)); if(y==0||b[x]==1)for(int i=l;i<=r;i++)up(tmp,dfs(x-1,ny,l,i-1)+dfs(x-1,y,i,r)+s[r]-s[i-1]); return f[x][y][l][r]=tmp; } int main(){ scanf("%d%lld",&n,&m); if(!m)return puts("0"),0; while(m)b[++len]=m&1,m>>=1; for(i=1;i<=n;i++)scanf("%lld",&a[i]),s[i]=s[i-1]+a[i]; printf("%lld",dfs(len,1,1,n)); }
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 | #include<cstdio> typedef long long ll; const int N=67,M=205; const ll inf=3000000000000000000LL; int n,len,i,b[N];ll m,a[M],s[M],f[N][2][M][M];bool v[N][2][M][M]; inline void up(ll&a,ll b){a<b?(a=b):0;} ll dfs(int x,int y,int l,int r){ if(l>r)return 0; if(!x)return l==r?0:-inf; if(v[x][y][l][r])return f[x][y][l][r]; v[x][y][l][r]=1; ll tmp=-inf; int ny=y; if(y==1&&b[x]==1)ny=0; up(tmp,dfs(x-1,ny,l,r)); if(y==0||b[x]==1)for(int i=l;i<=r;i++)up(tmp,dfs(x-1,ny,l,i-1)+dfs(x-1,y,i,r)+s[r]-s[i-1]); return f[x][y][l][r]=tmp; } int main(){ scanf("%d%lld",&n,&m); if(!m)return puts("0"),0; while(m)b[++len]=m&1,m>>=1; for(i=1;i<=n;i++)scanf("%lld",&a[i]),s[i]=s[i-1]+a[i]; printf("%lld",dfs(len,1,1,n)); } |