constint N = 355; int a[N], n, m; int num[5]; intread() { int f = 1, x = 0; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } int f[42][42][42][42];
intmain() { memset(f, -0x3f, sizeof f); n = read(), m = read(); for(int i = 1;i <= n;i++) { a[i] = read(); }
int t;
for(int i = 1;i <= m;i++) { t = read(); num[t]++; } f[0][0][0][0] = a[1]; int ans = -0x7f7f7f7f; for(int i = 0;i <= num[1];i++) { for(int j = 0;j <= num[2];j++) { for(int k = 0;k <= num[3];k++) { for(int l = 0;l <= num[4];l++) { if(i == j && j == k && k == l && i == 0) continue; int x = 1 + i + 2 * j + 3 * k + 4 * l; if(x > n) continue; if(i - 1 >= 0) f[i][j][k][l] = max(f[i][j][k][l], f[i - 1][j][k][l] + a[x]); if(j - 1 >= 0) f[i][j][k][l] = max(f[i][j][k][l], f[i][j - 1][k][l] + a[x]); if(k - 1 >= 0) f[i][j][k][l] = max(f[i][j][k][l], f[i][j][k - 1][l] + a[x]); if(l - 1 >= 0) f[i][j][k][l] = max(f[i][j][k][l], f[i][j][k][l - 1] + a[x]); ans = max(ans, f[i][j][k][l]); } } } }