传送门
思路:经典DP题,我来水一水,看到数据范围后应该可以知道这是一个多维DP,我们可以设
代码:
#include<cstdio>
inline int max(int a,int b){return a>b?a:b;}
int n, m;
int a[355], card[5];
int f[41][41][41][41];
int main()
{
int i,k,j,l;
scanf("%d%d",&n,&m);
for(i = 1; i <= n; i ++) scanf("%d",&a[i]);
for(i = 1; i <= m; i ++)
{
scanf("%d",&j);
card[j]++;
}
for(i=0; i<=card[1]; i ++)
for(j=0; j<=card[2]; j ++)
for(k=0; k<=card[3]; k ++)
for(l=0; l<=card[4]; l ++)
{
int a1 = i==0?0:f[i-1][j][k][l];
int a2 = j==0?0:f[i][j-1][k][l];
int a3 = k==0?0:f[i][j][k-1][l];
int a4 = l==0?0:f[i][j][k][l-1];
int t = max(max(a1,a2),max(a3,a4));
f[i][j][k][l] = t+a[i+2*j+3*k+4*l+1];
}
printf("%d",f[card[1]][card[2]][card[3]][card[4]]);
}
版权声明:请随意转载O(∩_∩)O
原文地址:http://blog.csdn.net/gengmingrui/article/details/47723807