【题解】
很容易想到这是一个DP,f[i][j][k][l]表示4种卡片分别用了多少张,那么转移方程就是f[i][j][k][l]=Max(f[i-1][j][k][l],f[i][j-1][k][l],f[i][j][k-1][l],f[i][j][k][l-1])+a[i+j*2+k*3+l*4+1].

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define LL long long 5 #define rg register 6 #define N 400 7 using namespace std; 8 int n,m,cnt[10],f[41][41][41][41],a[N]; 9 inline int read(){ 10 int k=0,f=1; char c=getchar(); 11 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 12 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 13 return k*f; 14 } 15 int main(){ 16 n=read(); m=read(); 17 for(rg int i=1;i<=n;i++) a[i]=read(); 18 for(rg int i=1;i<=m;i++) cnt[read()]++; 19 for(rg int i=0;i<=cnt[1];i++) 20 for(rg int j=0;j<=cnt[2];j++) 21 for(rg int k=0;k<=cnt[3];k++) 22 for(rg int l=0;l<=cnt[4];l++){ 23 int pos=i+(j<<1)+(k*3)+(l<<2)+1; 24 if(i>=1)f[i][j][k][l]=max(f[i][j][k][l],f[i-1][j][k][l]); 25 if(j>=1)f[i][j][k][l]=max(f[i][j][k][l],f[i][j-1][k][l]); 26 if(k>=1)f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k-1][l]); 27 if(l>=1)f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k][l-1]); 28 f[i][j][k][l]+=a[pos]; 29 } 30 printf("%d ",f[cnt[1]][cnt[2]][cnt[3]][cnt[4]]); 31 return 0; 32 }