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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
program Boha;

{$mode objfpc}{$H+}

uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes
   { you can add units after this };
Type TMonster=Record
     Case Boolean of
          False : (
	            Numb:Longint;
		    Hits:Longint;
		    Elic:Longint;
		    Suma:Longint;
                   );
          True  : (A:Array[0..3] of Longint);
      End;
     TMonsters=Array[0..100000] of TMonster;

Var Bitor:Int64;
    D, Plus, Minus:TMonsters;
    CountP,CountM:Longint;
    Test1,Test2:Boolean;

Function Fight(var Dat:TMonsters;Const Count:Longint):Boolean;
Var Loop:Longint;
Begin
   Result:=True;
   For Loop:=Count downto 1 do Begin
      Bitor:=Bitor-Dat[Loop].Hits;
      If Bitor<1 Then Begin
         Result:=False;
         Break;
      End;
      Bitor:=Bitor+Dat[Loop].Elic;
   End;
End;

procedure MergeSort(var Dat:TMonsters;const  Start, Stop, Col: Longint);
var
  Med,Rock1,Rock2,Loop : integer;
begin
  Med := (Start + Stop + 1) div 2;
  if Med-Start>1 Then MergeSort( Dat, Start, Med - 1, Col);
  if Stop-Med>0  Then MergeSort( Dat, Med, Stop, Col);
  Rock1:=Start; Rock2:=Med;
  for Loop:=Start to Stop do
    if (Rock1=Med) or ((Rock2<=Stop) and (Dat[Rock1].A[Col]>Dat[Rock2].A[Col])) then
    Begin
      d[Loop]:= Dat[Rock2]; inc(Rock2);
    End Else Begin
      d[Loop]:= Dat[Rock1]; inc(Rock1);
    End;
  For Loop:=Start to Stop do Dat[Loop]:=d[Loop];
End;

Procedure PrintMonster;
var Loop:Longint;
Begin
   For Loop:=1 to CountP do
     With Plus[Loop] do
        WriteLn( Numb,' ',Hits,' ',Elic,' ',Suma);
   WriteLn('-----------');
   For Loop:=1 to CountM do
     With Minus[Loop] do
        WriteLn( Numb,' ',Hits,' ',Elic,' ',Suma);

   ReadLn;
End;

Procedure PrintResult;
Var Loop:Longint;
Begin
   If Test1 ANd Test2 Then Begin
      WriteLn('TAK');
      For Loop:=CountP downto 1 do Write(Plus[Loop].Numb,' ');
      For Loop:=CountM downto 1 do Write(Minus[Loop].Numb,' ');
      WriteLn;
   End Else WriteLn('NIE');
End;

Procedure Reverse(var Dat:TMonsters;const Count:Longint);
Var Loop:Longint;
    T:TMonster;
Begin
   For Loop:=1 to Count div 2 do Begin
      T:=Dat[Count-Loop+1];
      Dat[Count-Loop+1]:=Dat[Loop];
      Dat[Loop]:=T;
   End;
End;

Function ReadLine:TMonster;
Begin
   ReadLn(Result.Hits,Result.Elic);
   Result.Suma:=Result.Elic-Result.Hits;
End;

Procedure ReadData;
Var Count, Loop:Longint;
    T:TMonster;
Begin
   Test1:=False;   Test2:=False;
   CountP:=0;      CountM:=0;
   ReadLn(Count,Bitor);
   For Loop:=1 to Count do Begin
      T:=ReadLine;
      If T.Suma>=0 Then Begin
         CountP:=CountP+1;
         Plus[CountP]:=T;
         Plus[CountP].Numb:=Loop;
      End Else Begin
         CountM:=CountM+1;
         Minus[CountM]:=T;
         Minus[CountM].Numb:=Loop;
      End;
   End;
   MergeSort(Plus, 1,CountP,3);
   MergeSort(Plus, 1,CountP,1);
   Reverse(Plus,CountP);
   Test1:=Fight(Plus,CountP);
   MergeSort(Minus, 1, CountM,3);
   MergeSort(Minus, 1, CountM,1);
   Test2:=Fight(Minus,CountM);
End;

begin
   ReadData;
   PrintResult;
//   ReadLn;
end.

{
3 5
3 1
4 8
8 3

5 5
1 0
1 0
1 0
1 0
1 1

2 5
4 19
6 1

10 5
1 10
3 4
3 5
3 6
3 7
1 1
1 2
1 3
1 10
5 1

15 5
1 10
3 4
1 2
7 1
3 5
1 0
3 1
3 6
3 7
1 1
3 0
1 2
1 3
1 10
5 1

9 5
1 1
4 8
0 0
1 0
8 1
0 5
4 2
2 1
3 1
TAK
3 6 1 2 5 7 9 8 4

9 4
1 1
4 8
0 0
1 0
8 1
0 5
4 2
2 1
3 1
NIE

}