1 #pragma GCC optimize("Ofast") 2 #include <bits/stdc++.h> 3 #define maxn 13003 4 #define _for(i,a,b) for(int i = (a);i < b;i ++) 5 typedef long long ll; 6 using namespace std; 7 8 inline ll read() 9 { 10 ll ans = 0; 11 char ch = getchar(), last = ' '; 12 while(!isdigit(ch)) last = ch, ch = getchar(); 13 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 14 if(last == '-') ans = -ans; 15 return ans; 16 } 17 inline void write(ll x) 18 { 19 if(x < 0) x = -x, putchar('-'); 20 if(x >= 10) write(x / 10); 21 putchar(x % 10 + '0'); 22 } 23 24 int N,M; 25 int ip[400]; 26 int dp[41][41][41][41]; 27 int num[5]; 28 int main() 29 { 30 N = read(),M = read(); 31 _for(i,1,N+1) 32 ip[i] = read(); 33 34 _for(i,1,M+1) 35 num[read()] ++; 36 37 dp[0][0][0][0] = ip[1]; 38 _for(a,0,num[1]+1) 39 _for(b,0,num[2]+1) 40 _for(c,0,num[3]+1) 41 _for(d,0,num[4]+1) 42 { 43 int r = 1+a+2*b+3*c+4*d; 44 if(a) dp[a][b][c][d] = max(dp[a][b][c][d],dp[a-1][b][c][d]+ip[r]); 45 if(b) dp[a][b][c][d] = max(dp[a][b][c][d],dp[a][b-1][c][d]+ip[r]); 46 if(c) dp[a][b][c][d] = max(dp[a][b][c][d],dp[a][b][c-1][d]+ip[r]); 47 if(d) dp[a][b][c][d] = max(dp[a][b][c][d],dp[a][b][c][d-1]+ip[r]); 48 } 49 write(dp[num[1]][num[2]][num[3]][num[4]]); 50 return 0; 51 }