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>
using namespace std;

struct monster
{
	int id, d, a;

	monster(int i=0, int d=0, int a=0)
	{
		id = i;
		this->d = d;
		this->a = a;
	}
};

void answerNo()
{
	printf("NIE\n");
	exit(0);
}

const int MAX = 100004;
monster monPlus[MAX], monMinus[MAX];

int main()
{
	int n, d, a, p = 0, m = 0;
	long long z;
	scanf("%d%lld", &n, &z);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &d, &a);
		if(d <= a)
			monPlus[p++] = monster(i, d, a);
		else
			monMinus[m++] = monster(i, d, a);
	}
	sort(monPlus, monPlus + p, [](const monster& a, const monster& b){return a.d < b.d;});
	sort(monMinus, monMinus + m, [](const monster& a, const monster& b){return a.a > b.a;});
	
	for(int i = 0; i < p; ++i)
	{
		if(monPlus[i].d >= z)
			answerNo();
		z += monPlus[i].a - monPlus[i].d;
	}
	for(int i = 0; i < m; ++i)
		z += monMinus[i].a - monMinus[i].d;
	for(int i = m-1; i >= 0; --i)
	{
		if(monMinus[i].a >= z)
			answerNo();
		z -= monMinus[i].a - monMinus[i].d;
	}
	printf("TAK\n");
	for(int i = 0; i < p; ++i)
		printf("%d ", monPlus[i].id);
	for(int i = 0; i < m; ++i)
		printf("%d ", monMinus[i].id);
	printf("\n");
	return 0;
}