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

struct Monster
{
	unsigned d, a, p;

	Monster(unsigned d, unsigned a, unsigned p): d(d), a(a - d), p(p) {}

	bool operator<(const Monster &m) const
	{
		return d < m.d;
	}
};

bool check(unsigned z, const std::vector<Monster> &plus, const std::vector<Monster> &minus)
{
	for(std::vector<Monster>::const_iterator it = plus.cbegin(); it != plus.cend(); ++it)
	{
		if(z <= it->d)
			return false;
		z += it->a;
	}

	for(std::vector<Monster>::const_iterator it = minus.cbegin(); it != minus.cend(); ++it)
		z -= it->a;

	if(static_cast<int>(z) < 0)
		return false;

	for(std::vector<Monster>::const_iterator it = minus.cbegin(); it != minus.cend(); ++it)
	{
		if(z <= it->d)
			return false;
		z += it->a;
	}

	return true;
}

int main()
{
	std::ios_base::sync_with_stdio(false);

	unsigned n, z;
	std::cin >> n >> z;

	std::vector<Monster> plus, minus;

	for(unsigned i = 1; i <= n; ++i)
	{
		unsigned d, a;
		std::cin >> d >> a;

		if(d <= a)
			plus.emplace_back(d, a, i);
		else
			minus.emplace_back(a, d, i);
	}

	std::sort(plus.begin(), plus.end());
	std::sort(minus.begin(), minus.end());

	if(check(z, plus, minus))
	{
		std::cout << "TAK" << std::endl;
		for(std::vector<Monster>::const_iterator it = plus.cbegin(); it != plus.cend(); ++it)
			std::cout << it->p << ' ';
		for(std::vector<Monster>::const_reverse_iterator it = minus.crbegin(); it != minus.crend(); ++it)
			std::cout << it->p << ' ';
		std::cout << std::endl;
	}
	else
		std::cout << "NIE" << std::endl;

	return 0;
}