#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#define I inline
#define R register
using namespace std;
int n,m;
int num[400]={0};
int sum[5]={0};
int f[41][41][41][41]={0};
I int read()
{
int x=0;
char ch=0;
bool w=true;
while(!isdigit(ch)){if(ch=='-')w=false;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return w?x:-x;
}
I int MAIN()
{
freopen ("tortoise.in","r",stdin);
freopen ("tortoise.out","w",stdout);
n=read(),m=read();
for(R int i=1;i<=n;++i)
num[i]=read();
for(R int i=1;i<=m;++i)
++sum[read()];
f[0][0][0][0]=num[1];
for(R int i=0;i<=sum[1];++i)
for(R int j=0;j<=sum[2];++j)
for(R int k=0;k<=sum[3];++k)
for(R int l=0;l<=sum[4];++l)
{
int s=i+2*j+3*k+4*l+1;//由于从第一格开始,所以要加 1
if(i!=0)
f[i][j][k][l]=max(f[i-1][j][k][l]+num[s],f[i][j][k][l]);
if(j!=0)
f[i][j][k][l]=max(f[i][j-1][k][l]+num[s],f[i][j][k][l]);
if(k!=0)
f[i][j][k][l]=max(f[i][j][k-1][l]+num[s],f[i][j][k][l]);
if(l!=0)
f[i][j][k][l]=max(f[i][j][k][l-1]+num[s],f[i][j][k][l]);
}
cout<<f[sum[1]][sum[2]][sum[3]][sum[4]];
return 0;
}
int lglj=MAIN();
int main(){;}