设状态函数f(i,j,x,y)表示选i个1,j个2,x个3,y个4牌得到的最大分
边界f(0,0,0,0)=a[1];
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<vector>
#define oo 100000000
#define maxm 45
#define maxn 1005
#define for(i,t,x) for(int i=t;i<=x;i++)
#define f(i,j,x,y) d[i][j][x][y]
using namespace std;
int n,m;
int a[maxn],b[5]={0};
int d[maxm][maxm][maxm][maxm]={0};
void read(int &x)
{
x=0;
bool ok=0;
char ch=getchar();
while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();
while((ch>='0'&&ch<='9')||ch=='-')
{
if(ch=='-') ok=1;
else x=x*10+ch-'0';
ch=getchar();
}
if(ok) x=-x;
}
void in()
{
read(n);read(m);
for(i,1,n) read(a[i]);
for(i,1,m)
{
int x;
read(x);
b[x]++;
}
}
//f(i,j,x,y)表示选i个1,j个2,x个3,y个4牌得到的最大分
void task()
{
f(0,0,0,0)=a[1];
for(i,0,b[1])
for(j,0,b[2])
for(x,0,b[3])
for(y,0,b[4])
{
if(i==0&&j==0&&x==0&&y==0) continue;
int t1=-oo,t2=-oo,t3=-oo,t4=-oo;
int k=i+j*2+x*3+y*4+1;
if(i-1>=0) t1=f(i-1,j,x,y)+a[k];
if(j-1>=0) t2=f(i,j-1,x,y)+a[k];
if(x-1>=0) t3=f(i,j,x-1,y)+a[k];
if(y-1>=0) t4=f(i,j,x,y-1)+a[k];
f(i,j,x,y)=max(max(t1,t2),max(t3,t4));
}
printf("%d",f(b[1],b[2],b[3],b[4]));
}
int main()
{
freopen("in.txt","r",stdin);
in();
task();
return 0;
}