链接:
https://www.luogu.org/problemnew/show/P1541
【思路】:
用f[a][b][c][d]表示,第一张卡用a张,第二张卡用b张..........然后就盘就ok了
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<map> #include<string> #include<cstring> using namespace std; const int maxn=999999999; const int minn=-999999999; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n,m,a[355],f[41][41][41][41],s[305],js[5],b; int main() { cin>>n>>m; for(int i=1; i<=n; ++i) { a[i]=read(); } for(int i=1; i<=m; ++i) { cin>>b; js[b]++; } f[0][0][0][0]=a[1]; for(int i=0; i<=js[1]; ++i) { for(int j=0; j<=js[2]; ++j) { for(int k=0; k<=js[3]; ++k) { for(int l=0; l<=js[4]; ++l) { int place=1+i*1+j*2+k*3+l*4; if(i!=0) { f[i][j][k][l]=max(f[i][j][k][l],f[i-1][j][k][l]+a[place]); } if(j!=0) { f[i][j][k][l]=max(f[i][j][k][l],f[i][j-1][k][l]+a[place]); } if(k!=0) { f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k-1][l]+a[place]); } if(l!=0) { f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k][l-1]+a[place]); } } } } } cout<<f[js[1]][js[2]][js[3]][js[4]]; return 0; }