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
/*
 * boh.c
 *
 */
#include <stdio.h>
#include <stdlib.h>

int * obr, *el;
int cmp(const void * a, const void * b) {
	int ai = *(int*) a, bi = *(int*) b, asign, bsign;
	if (el[ai] == el[bi] && obr[ai] == obr[bi])
		return 0;
	if ((-obr[ai] + el[ai]) > 0)
		asign = 0;
	else if ((-obr[ai] + el[ai]) == 0)
		asign = 1;
	else
		asign = 3;

	if ((-obr[bi] + el[bi]) > 0)
		bsign = 0;
	else if ((-obr[bi] + el[bi]) == 0)
		bsign = 1;
	else
		bsign = 3;

	if (asign != bsign)
		return asign - bsign;

	if (asign == 0)
		return obr[ai] - obr[bi];
	if (asign == 3)
		return el[bi] - el[ai];
	/*if (asign==1)
	 return*/
	return 0;

}

int main() {
	int n, i, *index;
	long long int z;

	scanf("%i %lli", &n, &z);
	obr = (int *) malloc(n * sizeof(int));
	el = (int *) malloc(n * sizeof(int));
	index = (int *) malloc(n * sizeof(int));
	for (i = 0; i < n; i++) {
		index[i] = i;
		scanf("%i %i", &obr[i], &el[i]);
	}
	qsort(index, n, sizeof(int), cmp);
	for (i = 0; i < n; i++) {
		if (z <= obr[index[i]]) {
			printf("NIE\n");
			//for (i = 0; i < n; i++)
			 //printf("%i ", index[i] + 1);
			return 0;
		}
		//printf ("Z: %lli",z);
		z += (-obr[index[i]] + el[index[i]]);
	}
	printf("TAK\n");
	for (i = 0; i < n; i++)
		printf("%i ", index[i] + 1);
	printf("\n");

	return 0;
}