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
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>

typedef long long int64;

using namespace std;

int main () {
	int n, i, d, a;
	int64 z;
	scanf("%d %lld", &n, &z);
	vector<pair<int, pair<int, int> > > plus, minus;
	vector<int> order;
	for (i = 0; i < n; ++i) {
		scanf("%d %d", &d, &a);
		if (d <= a) {
			plus.push_back(make_pair(d, make_pair(a - d, i)));
		}
		else {
			minus.push_back(make_pair(a, make_pair(d, i)));
		}
	}
	sort(plus.begin(), plus.end());
	for (i = 0; i < plus.size(); ++i) {
		if (z > plus[i].first) {
			z += plus[i].second.first;
			order.push_back(plus[i].second.second + 1);
		}
		else {
			printf("NIE\n");
			return 0;
		}
	}
	int64 s = 0;
	sort(minus.begin(), minus.end());
	for (i = 0; i < minus.size(); ++i) {
		s += minus[i].second.first - minus[i].first;
	}
	if (z <= s) {
		printf("NIE\n");
		return 0;
	}
	z -= s;
	sort(minus.begin(), minus.end());
	for (i = 0; i < minus.size(); ++i) {
		if (z <= minus[i].first) {
			printf("NIE\n");
			return 0;
		}
		z += minus[i].second.first - minus[i].first;
	}
	for (i = minus.size() - 1; i >= 0; --i) {
		order.push_back(minus[i].second.second + 1);
	}
	printf("TAK\n");
	for (i = 0; i < order.size(); ++i) {
		printf("%d", order[i]);
		if (i == order.size() - 1) {
			break;
		}
		printf(" ");
	}
	printf("\n");
	return 0;
}