program bohater; type walka=record id,dam,lEl : longint; end; tab=array [1..100050] of walka; var hp,n,i,dam,lEl,pos,neg : longint; walkiPos,walkiNeg : tab; koniec : boolean; procedure quickPos(od,do_ : longint); var i,j,x : longint; temp : walka; begin i:=od; j:=do_; x:=walkiPos[(od+do_) div 2].dam; repeat while walkiPos[i].dam<x do inc(i); while x<walkiPos[j].dam do dec(j); if i<=j then begin temp:=walkiPos[i]; walkiPos[i]:=walkiPos[j]; walkiPos[j]:=temp; inc(i); dec(j); end; until i>j; if od<j then quickPos(od,j); if i<do_ then quickPos(i,do_); end; procedure quickNeg(od,do_ : longint); var i,j,x : longint; temp : walka; begin i:=od; j:=do_; x:=walkiNeg[(od+do_) div 2].dam; repeat while walkiNeg[i].dam>x do inc(i); while x>walkiNeg[j].dam do dec(j); if i<=j then begin temp:=walkiNeg[i]; walkiNeg[i]:=walkiNeg[j]; walkiNeg[j]:=temp; inc(i); dec(j); end; until i>j; if od<j then quickNeg(od,j); if i<do_ then quickNeg(i,do_); end; begin read(n); readln(hp); pos:=0; neg:=0; for i:=1 to n do begin read(dam); readln(lEl); if dam<lEl then begin inc(pos); walkiPos[pos].id:=i; walkiPos[pos].dam:=dam; walkiPos[pos].lEl:=lEl; end else begin inc(neg); walkiNeg[neg].id:=i; walkiNeg[neg].dam:=dam; walkiNeg[neg].lEl:=lEl; end; end; if pos>0 then quickPos(1,pos); if neg>0 then quickNeg(1,neg); koniec:=false; i:=1; while (not koniec)and(i<=pos) do begin hp:=hp-walkiPos[i].dam; if hp<=0 then koniec:=true else begin hp:=hp+walkiPos[i].lEl; inc(i); end; end; i:=1; while (not koniec)and(i<=neg) do begin hp:=hp-walkiNeg[i].dam; if hp<=0 then koniec:=true else begin hp:=hp+walkiNeg[i].lEl; inc(i); end; end; if koniec then write('NIE') else begin writeln('TAK'); for i:=1 to pos-1 do write(walkiPos[i].id,' '); if neg=0 then write(walkiPos[pos].id) else begin if pos>0 then write(walkiPos[pos].id,' '); for i:=1 to neg-1 do write(walkiNeg[i].id,' '); write(walkiNeg[neg].id); end; end; end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | program bohater; type walka=record id,dam,lEl : longint; end; tab=array [1..100050] of walka; var hp,n,i,dam,lEl,pos,neg : longint; walkiPos,walkiNeg : tab; koniec : boolean; procedure quickPos(od,do_ : longint); var i,j,x : longint; temp : walka; begin i:=od; j:=do_; x:=walkiPos[(od+do_) div 2].dam; repeat while walkiPos[i].dam<x do inc(i); while x<walkiPos[j].dam do dec(j); if i<=j then begin temp:=walkiPos[i]; walkiPos[i]:=walkiPos[j]; walkiPos[j]:=temp; inc(i); dec(j); end; until i>j; if od<j then quickPos(od,j); if i<do_ then quickPos(i,do_); end; procedure quickNeg(od,do_ : longint); var i,j,x : longint; temp : walka; begin i:=od; j:=do_; x:=walkiNeg[(od+do_) div 2].dam; repeat while walkiNeg[i].dam>x do inc(i); while x>walkiNeg[j].dam do dec(j); if i<=j then begin temp:=walkiNeg[i]; walkiNeg[i]:=walkiNeg[j]; walkiNeg[j]:=temp; inc(i); dec(j); end; until i>j; if od<j then quickNeg(od,j); if i<do_ then quickNeg(i,do_); end; begin read(n); readln(hp); pos:=0; neg:=0; for i:=1 to n do begin read(dam); readln(lEl); if dam<lEl then begin inc(pos); walkiPos[pos].id:=i; walkiPos[pos].dam:=dam; walkiPos[pos].lEl:=lEl; end else begin inc(neg); walkiNeg[neg].id:=i; walkiNeg[neg].dam:=dam; walkiNeg[neg].lEl:=lEl; end; end; if pos>0 then quickPos(1,pos); if neg>0 then quickNeg(1,neg); koniec:=false; i:=1; while (not koniec)and(i<=pos) do begin hp:=hp-walkiPos[i].dam; if hp<=0 then koniec:=true else begin hp:=hp+walkiPos[i].lEl; inc(i); end; end; i:=1; while (not koniec)and(i<=neg) do begin hp:=hp-walkiNeg[i].dam; if hp<=0 then koniec:=true else begin hp:=hp+walkiNeg[i].lEl; inc(i); end; end; if koniec then write('NIE') else begin writeln('TAK'); for i:=1 to pos-1 do write(walkiPos[i].id,' '); if neg=0 then write(walkiPos[pos].id) else begin if pos>0 then write(walkiPos[pos].id,' '); for i:=1 to neg-1 do write(walkiNeg[i].id,' '); write(walkiNeg[neg].id); end; end; end. |