要注意读题,他已经在第一个格子,所以每走一步取得是再+1的格子中的数
program tt; var n,m,i,b,j,k,l:longint; a:array[0..350]of longint; f:array[-1..40,-1..40,-1..40,-1..40]of longint; c:array[1..4]of longint; function max(a,b:longint):longint; begin if a<b then max:=b else max:=a; end; begin read(n,m); for i:=1 to n do read(a[i]); for i:=1 to m do begin read(b); c[b]:=c[b]+1; end; for i:=0 to c[1] do for j:=0 to c[2] do for k:=0 to c[3] do for l:=0 to c[4] do f[i,j,k,l]:=max(f[i-1,j,k,l],max(f[i,j-1,k,l],max(f[i,j,k-1,l],f[i,j,k,l-1])))+a[i+j*2+k*3+l*4+1]; writeln(f[c[1],c[2],c[3],c[4]]); end.