var
i,j,m,n,x,l,k:longint;
b:array[1..4] of longint;
f:array[-1..40,-1..40,-1..40,-1..40] of longint;
a:array[0..350] of longint;
function qumax(a,b,c,d:longint):longint;
var
max:longint;
begin
max:=a;
if b>max then max:=b;
if c>max then max:=c;
if d>max then max:=d;
exit(max);
end;
begin
readln(n,m);
for i:=1 to n do read(a[i-1]);
readln;
for j:=1 to m do begin read(x);
case x of
1: inc(b[1]);
2: inc(b[2]);
3: inc(b[3]);
4: inc(b[4]);
end;
end;
for i:=0 to b[1] do
for j:= 0 to b[2] do
for k:=0 to b[3] do
for l:=0 to b[4] do
begin
x:=i+j*2+k*3+l*4;
f[i,j,k,l]:=qumax(f[i-1,j,k,l],f[i,j-1,k,l],f[i,j,k-1,l],f[i,j,k,l-1])+a[x];
end;
writeln(f[b[1],b[2],b[3],b[4]]);
end.
以前学pascal打的ac代码