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

struct TripleLong {
  long d;
  long a;
  long i;
  TripleLong(long dt, long at, long it):d(dt),a(at),i(it) {};  
};

bool compare(const TripleLong& first, const TripleLong& second) {
  if (first.d < second.d)
    return true;
  else
    return false;
}

bool compare2(const TripleLong& first, const TripleLong& second) {
  if (first.a < second.a)
    return false;
  else
    return true;
}

int main() {
	long long z;
	long n;
	bool sur = true;
	scanf("%ld %lld\n", &n, &z);
	long long trace_suma = 0;
	vector<TripleLong> trace;
	vector<TripleLong> zyskuje;
	vector<long> res;

	for (long i=1;i<=n;i++) {
		long d,a;
		scanf("%ld %ld\n", &d, &a);
		if (d>a) {
			trace.push_back(TripleLong(d,a,i));
			trace_suma += d-a;
		} else {
			zyskuje.push_back(TripleLong(d,a,i));
		}
	}

 	sort(zyskuje.begin(),zyskuje.end(),compare);
	vector<TripleLong>::const_iterator itr = zyskuje.begin();
	while(itr != zyskuje.end()){
		if (itr->d >= z) {
			sur = false;
			break;
		} else {
			z += (-itr->d+itr->a);
			res.push_back(itr->i);
		}
		++itr;
	}

	if (sur) {
		if (trace_suma >= z) {
			sur = false;
		} else {
			sort(trace.begin(),trace.end(),compare2);
			vector<TripleLong>::const_iterator itr = trace.begin();
			while(itr != trace.end()){
			//	printf("%ld %ld %ld\n", itr->d,itr->a,itr->i);
				if (itr->d >= z) {
					sur = false;
					break;
				} else {
					z += (-itr->d+itr->a);
					res.push_back(itr->i);
				}
				++itr;
			} 
		}
	}

	if(sur) {
		printf("TAK\n");
		for (int i=0; i<res.size(); i++) {
			if (i==res.size()-1) {
				printf("%ld\n", res[i]);
			} else {
				printf("%ld ", res[i]);
			}
		}
	} else {
		printf("NIE\n");
	}

	return 0;
}