|
|
back to boardMy alg is 0 1 knapsack, BUT #4 Crash(Access_Violation)! My alg is 0 1 knapsack, BUT #4 Crash(Access_Violation)! Need help! var a:array[1..100] of longint; b:array[1..10000] of longint; max,n,nn,i,s,s2:longint; procedure readdata; begin readln(n); s:=0; for i:=1 to n do begin read(a[i]); s:=s+a[i]; end; s2:=s div 2; end; procedure writedata; begin writeln(abs( abs(s-max)-abs(max) )); end; function check(l:longint):boolean; var i:integer; bb:boolean; begin bb:=false; for i:=1 to nn do if b[i]=l then begin bb:=true; exit; end; check:=false; end; procedure sum(l:longint); var i,nnn:longint; begin nnn:=nn; for i:=1 to nnn-1 do begin if ((b[i]+l)<=s2) and (not check(b[i]+l)) then begin if (b[nn]<=s2) and (b[nn]>max) then max:=b[nn]; inc(nn); b[nn]:=b[i]+l; if (b[nn]<=s2) and (b[nn]>max) then max:=b[nn]; end; end; end; begin readdata; nn:=0; max:=a[1]; for i:=1 to n do begin if not check(a[i]) then begin inc(nn); b[nn]:=a[i]; if (b[nn]<=s2) and (b[nn]>max) then max:=b[nn]; sum(a[i]); end; end; writedata; end. AC! Thanks to Allah! At least AC! Thanks to Allah! AC!!! 0.001 105 КБ |
|
|