P1541 乌龟棋 题目
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<ctime>
#define ll long long
#define rg register
#define N 400
#define M 50
using namespace std;
int n,m;
ll ans;
int a[N],card[5];
long long f[M][M][M][M];
inline int read()
{
int s=0,m=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')m=-1,ch=getchar();
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
return s*m;
}
int main()
{
n=read();m=read();
for(rg int i=1;i<=n;++i)
a[i]=read();
for(rg int i=1;i<=m;++i)
{
int o=read();
card[o]++;
}
for(rg int i=0;i<=card[4];++i)
for(rg int j=0;j<=card[3];++j)
for(rg int k=0;k<=card[2];++k)
for(rg int l=0;l<=card[1];++l)
{
int now=i*4+j*3+k*2+l+1;
if(i>0)
{
f[i][j][k][l]=max(f[i-1][j][k][l]+a[now],f[i][j][k][l]);
}
if(j>0)
{
f[i][j][k][l]=max(f[i][j-1][k][l]+a[now],f[i][j][k][l]);
}
if(k>0)
{
f[i][j][k][l]=max(f[i][j][k-1][l]+a[now],f[i][j][k][l]);
}
if(l>0)
{
f[i][j][k][l]=max(f[i][j][k][l-1]+a[now],f[i][j][k][l]);
}
if(now==n)
{
ans=max(ans,f[i][j][k][l]);
}
}
printf("%lld
",ans+a[1]);
return 0;
}