乌龟棋
![](https://img2018.cnblogs.com/blog/1660983/201905/1660983-20190528171521345-2004023492.png)
#include<bits/stdc++.h>
using namespace std;
int x,n,m,f[41][41][41][41],a[351],b[121],g[5];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
{
cin>>x;
g[x]++;
}
f[0][0][0][0]=a[1];
for(int i=0;i<=g[1];i++)
for(int j=0;j<=g[2];j++)
for(int k=0;k<=g[3];k++)
for(int l=0;l<=g[4];l++)
{
int r=1+i+2*j+3*k+4*l;
if(i!=0)
f[i][j][k][l]=max(f[i][j][k][l],f[i-1][j][k][l]+a[r]);
if(j!=0)
f[i][j][k][l]=max(f[i][j][k][l],f[i][j-1][k][l]+a[r]);
if(k!=0)
f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k-1][l]+a[r]);
if(l!=0)
f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k][l-1]+a[r]);
}
cout<<f[g[1]][g[2]][g[3]][g[4]];