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
#include <iostream>
          using namespace std;
          int main ()
          {
ios_base::sync_with_stdio(0);
          int n, u[100000], p[100000], b[100000], i, k=0, s1[100000], s2[100000];
 long long z,suma;         
          cin>>n>>z;
         
          for (i=0; i<n; i++)
          	  {
		   	   cin>>u[i]>>p[i];
		   	   b[i]=p[i]-u[i];
		   	  }
		   	  int e=0;
  while (k==0)        
{    k=1;      for (i=0; i<n; i++)
          	  {
			   		if (b[i]>=0 && u[i]<z && u[i]>=0)
			   		{
			   		   z+=b[i];
                       b[i]=0; u[i]=-1; p[i]=-1; 
                       k=0;
                       s1[e]=i+1;
                       e++;
                    }
              }
        
}    
suma=z; k=0; 
int f=0;
for (i=0; i<n; i++) suma+=b[i];
if (suma>=0)
{
while (k==0)        
{    k=1;      for (i=0; i<n; i++)
          	  {
			   		if (suma>p[i] && p[i]>=0)
			   		{
			   		   suma-=b[i];
                       b[i]=0; u[i]=-1; p[i]=-1; 
                       k=0;
                       s2[f]=i+1;
                       f++;
                    }
                    
              }
        
}
 

int j=0;
for (i=0; i<n; i++) {if (u[i]!=-1){j=1; break;}}

if (j==0) {
          cout<<"TAK"<<endl; 
for (i=0; i<e; i++) cout<<s1[i]<<" ";
for (i=f-1; i>=0; i--) cout<<s2[i]<<" ";
}
else cout<<"NIE";
}
else cout<<"NIE";
      
          
          return 0;
          }