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
#include<cstdio>
#include<algorithm>
#include<vector>
#include<tuple>
using namespace std;

typedef long long i64;

int n;
i64 z,zm;

int main(){
	vector<tuple<i64, i64,int> > A, B;

	scanf("%d%lld",&n,&z);
	for(int i=0;i<n;i++){
		i64 a,b;
		scanf("%lld%lld",&a,&b);
		if(a>b){
			B.push_back(make_tuple(b,a,i+1));
			zm+=b-a;
		}else
			A.push_back(make_tuple(a,b,i+1));
	}
	sort(A.begin(), A.end());

	sort(B.begin(), B.end());
	
	bool ok = true;
	for(int i=0;i<A.size();i++){
		if(get<0>(A[i])>=z){
			ok = false;
			break;
		}
		z += get<1>(A[i])-get<0>(A[i]);
	}

	if(ok){
		z+=zm;//koncowy wynik
		if(z <= 0)
			ok = false;
		else{	
			for(int i=0;i<B.size();i++){
				if(get<0>(B[i])>=z){
					ok = false;
					break;
				}
				z += get<1>(B[i])-get<0>(B[i]);
			}	
		}
	}

	if(!ok)
		printf("NIE\n");
	else{
		printf("TAK\n");
		for(int i=0;i<A.size();i++)printf("%d ",get<2>(A[i]));
		for(int i=B.size()-1;i>=0;i--)printf("%d ",get<2>(B[i]));
		printf("\n");
	}

	return 0;
}