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
// Grzegorz Bukowiec
// PA 2014 - Bohater

#include <iostream>
#include <algorithm>
using namespace std;

struct Monster
{
	int id;
	int attack;
	int elixir;
	int overall;
};

bool monsterCompare (const Monster &a, const Monster &b)
{
	if ((a.overall < 0 && b.overall > 0) || (a.overall > 0 && b.overall < 0) || (a.overall == 0 && b.overall != 0) || (a.overall != 0 && b.overall == 0)) {
		return a.overall > b.overall;
	}

	if (a.overall > 0 && b.overall > 0) {
		return a.attack < b.attack;
	}

	if (a.overall < 0 && b.overall < 0) {
		return a.elixir > b.elixir;
	}

	return a.attack < b.attack;
}

int main()
{
	ios_base::sync_with_stdio(0);
	int n;
	long long int z;
	bool managed = true;
	cin >> n >> z;
	Monster *monsters = new Monster[n];

	for (int i = 0; i < n; ++i) {
		monsters[i].id = i + 1;
		cin >> monsters[i].attack;
		cin >> monsters[i].elixir;
		monsters[i].overall = monsters[i].elixir - monsters[i].attack;
	}

	sort(monsters, monsters + n, monsterCompare);

	for (int i = 0; i < n; ++i) {
		z -= monsters[i].attack;
		if (z <= 0) {
			managed = false;
			break;
		}
		z += monsters[i].elixir;
	}

	if (managed) {
		cout << "TAK" << endl;
		for (int i = 0; i < n; ++i) {
			cout << monsters[i].id << " ";
		}
	} else {
		cout << "NIE";
	}
	delete [] monsters;

	return 0;
}