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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>

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


int cmp(const void * a, const void * b)
{
	return (( *(Monster*)a).d - (*(Monster*)b).d );
}

int test_hero(int n, int p, Monster* m, int *answer)
{
	int result=1;

	// Alloc memory
	Monster *a_gt = (Monster*)malloc(n*sizeof(Monster));
	Monster *a_lt = (Monster*)malloc(n*sizeof(Monster));

	// Prepare counters
	int gt_c = 0, lt_c = 0, ans_c = 0;

	// Distribute challenges
	for(int i=0; i<n; i++)
	{
		if(m[i].a >= m[i].d)
		{
			a_gt[gt_c] = m[i];
			gt_c++;
		}
		else
		{
			a_lt[lt_c] = m[i];
			lt_c++;
		}
	}

	// Start with monsters where a>=d
	qsort(a_gt, gt_c, sizeof(Monster), cmp);
	// Try killing them
	for(int i=0; i<gt_c; i++)
	{
		if(p > a_gt[i].d)
		{
			p += a_gt[i].a - a_gt[i].d;
			// Record answer
			answer[ans_c] = a_gt[i].id;
			ans_c++;
		}
		else
		{
			result = 0;
			break;
		}
	}


	// Monsters with a<d
	if(result == 1)
	{
		qsort(a_lt, lt_c, sizeof(Monster), cmp);

		for(int i=lt_c-1; i>=0; i--)
		{
			// Check if killing monster won't be dead end
			if(p - a_lt[i].d > 0 && (i == 0 || p - a_lt[i].d + a_lt[i].a > a_lt[i-1].d))
			{
				// Kill monster
				p += a_lt[i].a - a_lt[i].d;
				// Record answer
				answer[ans_c] = a_lt[i].id;
				ans_c++;
			}
			else if(i != 0 && p - a_lt[i-1].d > 0 && p - a_lt[i-1].d + a_lt[i-1].a > a_lt[i].d)
			{
				// Kill monster
				p += a_lt[i-1].a - a_lt[i-1].d;
				// Record answer
				answer[ans_c] = a_lt[i-1].id;
				ans_c++;
				// Kill almost strongest monster
				a_lt[i-1] = a_lt[i];
			}
			else
			{
				// There is no way to kill all monsters
				result = 0;
				break;
			}

		}
	}

	// Free memory
	free(a_gt);
	free(a_lt);

	return result;
}


int main()
{
	int n, p;
	int *answer;
	Monster* m;

	scanf("%d %d", &n, &p);

	m = (Monster*)malloc(n*sizeof(Monster));
	answer = (int*)malloc(n*sizeof(int));

	// Read input
	for(int i=0; i<n; i++)
	{
		scanf("%d %d", &(m[i].d), &(m[i].a));
		m[i].id = i+1;
	}

	if(test_hero(n, p, m, answer))
	{
		printf("TAK\n");
		for(int i=0; i<n; i++)
		{
			printf("%d", answer[i]);
			if(i < n-1)
				printf(" ");
			else
				printf("\n");
		}
	}
	else
	{
		printf("NIE\n");
	}

	// Free memory
	free(m);
	free(answer);

	return 0;
}