#include <bits/stdc++.h>
using namespace std;
int n,m;
int val[360],cnt[5];
int f[45][45][45][45];
int main(){
ios::sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> val[i];
}
for(int i = 1; i <= m; i++){
int x;cin >> x;
cnt[x]++;
}
f[0][0][0][0] = val[1];
for(int a = 0; a <= cnt[1];a++){
for(int b = 0; b <= cnt[2];b++){
for(int c = 0; c <= cnt[3];c++){
for(int d = 0; d <= cnt[4];d++){
int res = a + b * 2 + c * 3 + d * 4 + 1;
if(a) f[a][b][c][d] = max(f[a][b][c][d],f[a - 1][b][c][d] + val[res]);
if(b) f[a][b][c][d] = max(f[a][b][c][d],f[a][b - 1][c][d] + val[res]);
if(c) f[a][b][c][d] = max(f[a][b][c][d],f[a][b][c - 1][d] + val[res]);
if(d) f[a][b][c][d] = max(f[a][b][c][d],f[a][b][c][d - 1] + val[res]);
}
}
}
}
cout << f[cnt[1]][cnt[2]][cnt[3]][cnt[4]];
return 0;
}
10-28
54