显示代码纯文本
#include<cstdio>
using namespace std;
int n,m,w[351],p1,p2,p3,p4,f[41][41][41][41],i,j,k,l;
int max(int a,int b)
{
return a>b?a:b;
}
int Main()
{
freopen("tortoise.in","r",stdin);freopen("tortoise.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%d",&w[i]);
for(i=1;i<=m;i++)
{
int x;
scanf("%d",&x);
if(x==1)p1++;
else if(x==2)p2++;
else if(x==3)p3++;
else p4++;
}
f[0][0][0][0]=w[1];
for(i=0;i<=p1;i++)for(j=0;j<=p2;j++)
for(k=0;k<=p3;k++)for(l=0;l<=p4;l++)
{
int maxx=0;
if(i)maxx=max(maxx,f[i-1][j][k][l]);
if(j)maxx=max(maxx,f[i][j-1][k][l]);
if(k)maxx=max(maxx,f[i][j][k-1][l]);
if(l)maxx=max(maxx,f[i][j][k][l-1]);
f[i][j][k][l]=maxx+w[i+j*2+k*3+l*4+1];
}
printf("%d\n",f[p1][p2][p3][p4]);
return 0;
}
int main(){;}
int syy=Main();