1068
#include<stdio.h> int tos[41][41][41][41]={0};int card[5]={0};int n,m,step[356]={0};main(){ int tor(int a,int b,int c,int d); int i,j,x; scanf("%d%d",&n,&m); for(i=0;i<n;i++)scanf("%d",&step[i]); for(i=0;i<m;i++){scanf("%d",&x);card[x]++;} printf("%d",tor(card[1],card[2],card[3],card[4])); return(0);} int tor(int a,int b,int c,int d){ int i,j,x=0,y=0,z=0,w=0,stp=0,temp=0; if(tos[a][b][c][d]==0) { stp=a+b*2+c*3+d*4; if(stp==-1) { return(step[0]); } else { if(a-1>=0)x=tor(a-1,b,c,d); if(b-1>=0)y=tor(a,b-1,c,d); if(c-1>=0)z=tor(a,b,c-1,d); if(d-1>=0)w=tor(a,b,c,d-1); if(x>temp)temp=x; if(y>temp)temp=y; if(z>temp)temp=z; if(w>temp)temp=w; tos[a][b][c][d]=temp+step[stp]; return(tos[a][b][c][d]); } } else return(tos[a][b][c][d]);} 运行耗费