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
#include <iostream>

using namespace std;

struct m
{
	unsigned int n, d, a;	
};

long int z;

bool mcomp(m i, m j) { return i.a > j.a && z > i.d; }

int main(void)
{
	cin.sync_with_stdio(false);
	unsigned int n;
	cin >> n; cin >> z;
	m* da = (m*)malloc(sizeof(m) * (n + 1));
	for (unsigned int i = 1; i <= n; ++i)
	{
		da[i].n = i;
		cin >> da[i].d;
		cin >> da[i].a;
	}
	for (unsigned int j = 1; j <= n; ++j)
	{
		unsigned int k = j;
		for (unsigned int i = k + 1; i <= n; ++i)
			if (mcomp(da[i], da[k])) k = i;
		m x = da[k];
		da[k] = da[j];
		da[j] = x;
		z -= x.d;
		if (z < 1)
		{
			cout << "NIE" << endl;
			return 0;
		}
		z += x.a;
	}
	cout << "TAK" << endl;
	for (unsigned int i = 1; i <= n; ++i) cout << da[i].n << " ";
}