#include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; const long long inf=1e18+7; long long tab[210]; long long pref[210]; long long w[70][210][210]; long long wyn[70][210]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,i,j,k,l; long long pot,siz=0,m; cin>>n>>m; m++; for(i=1;i<=n;i++) { cin>>tab[i]; pref[i]=pref[i-1]+tab[i]; } for(i=1;i<=n;i++) w[0][i][i]=0; pot=2; for(k=1;pot<=m;k++,pot*=2) { for(i=1;i<=n;i++) { for(j=i;(j<=n)&&(j-i+1<=pot);j++) { w[k][i][j]=-inf; for(l=max((long long)i-1,(long long)j-(pot/2));l<=min((long long)j,(pot/2)+i-1);l++) w[k][i][j]=max(w[k][i][j],w[k-1][i][l]+w[k-1][l+1][j]+pref[j]-pref[l]); //cerr<<k<<" "<<i<<" "<<j<<" "<<w[k][i][j]<<"\n"; } } } i=1; while(m>0) { while(pot>m) { pot/=2; k--; } for(j=1;j<=min((long long)n,siz+pot);j++) { wyn[i][j]=-inf; for(l=max(0LL,(long long)j-pot);l<=min((long long)j,siz);l++) wyn[i][j]=max(wyn[i][j],wyn[i-1][l]+w[k][l+1][j]+(pref[j]-pref[l])*(i-1)); //cerr<<siz+pot<<" "<<j<<" "<<wyn[i][j]<<"\n"; } m-=pot; siz+=pot; i++; } cout<<wyn[i-1][n]<<"\n"; 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 | #include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; const long long inf=1e18+7; long long tab[210]; long long pref[210]; long long w[70][210][210]; long long wyn[70][210]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,i,j,k,l; long long pot,siz=0,m; cin>>n>>m; m++; for(i=1;i<=n;i++) { cin>>tab[i]; pref[i]=pref[i-1]+tab[i]; } for(i=1;i<=n;i++) w[0][i][i]=0; pot=2; for(k=1;pot<=m;k++,pot*=2) { for(i=1;i<=n;i++) { for(j=i;(j<=n)&&(j-i+1<=pot);j++) { w[k][i][j]=-inf; for(l=max((long long)i-1,(long long)j-(pot/2));l<=min((long long)j,(pot/2)+i-1);l++) w[k][i][j]=max(w[k][i][j],w[k-1][i][l]+w[k-1][l+1][j]+pref[j]-pref[l]); //cerr<<k<<" "<<i<<" "<<j<<" "<<w[k][i][j]<<"\n"; } } } i=1; while(m>0) { while(pot>m) { pot/=2; k--; } for(j=1;j<=min((long long)n,siz+pot);j++) { wyn[i][j]=-inf; for(l=max(0LL,(long long)j-pot);l<=min((long long)j,siz);l++) wyn[i][j]=max(wyn[i][j],wyn[i-1][l]+w[k][l+1][j]+(pref[j]-pref[l])*(i-1)); //cerr<<siz+pot<<" "<<j<<" "<<wyn[i][j]<<"\n"; } m-=pot; siz+=pot; i++; } cout<<wyn[i-1][n]<<"\n"; return 0; } |