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
#include <cstdio>
#include <cstdlib>

typedef struct {
	int id;
	int d;
	int a;
} battle;

battle high[100000];
battle low[100000];
int t[100000];

int comp_high(const void *a, const void *b)
{
	battle x = *(battle *)a;
	battle y = *(battle *)b;

	return x.d - y.d;
}

int comp_low(const void *a, const void *b)
{
	battle x = *(battle *)a;
	battle y = *(battle *)b;

	return y.a - x.a;
}

int main()
{
	int n, hptr = 0, lptr = 0;
	long long z;

	scanf("%d %lld", &n, &z);
	for (int i = 0; i < n; i++) {
		int d, a;
		scanf("%d %d", &d, &a);
		if (a >= d) {
			high[hptr].id = i + 1;
			high[hptr].d = d;
			high[hptr].a = a;
			hptr++;
		} else {
			low[lptr].id = i + 1;
			low[lptr].d = d;
			low[lptr].a = a;
			lptr++;
		}
	}

	qsort(high, hptr, sizeof(battle), comp_high);
	qsort(low, lptr, sizeof(battle), comp_low);

	int tptr = 0;
	bool b = true;
	for (int i = 0; i < hptr && b; i++) {
		if (high[i].d < z) {
			z -= high[i].d;
			z += high[i].a;
			t[tptr] = high[i].id;
			tptr++;
		} else {
			b = false;
		}
	}

	for (int i = 0; i < lptr && b; i++) {
		if (low[i].d < z) {
			z -= low[i].d;
			z += low[i].a;
			t[tptr] = low[i].id;
			tptr++;
		} else {
			b = false;
		}
	}

	if (b) {
		printf("TAK\n");
		for (int i = 0; i < tptr; i++) {
			printf("%d ", t[i]);
		}
		printf("\n");
	} else {
		printf("NIE\n");
	}
}