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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <cctype>
#include <vector>
#include <algorithm>
#include <functional>

#define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x)))

#ifndef _MSC_VER
#define __int64 long long
#endif

typedef unsigned int uint;
typedef unsigned __int64 uint64;

uint get_uint()
{
	int c;
	while (isspace(c = std::cin.get()));

	uint value = (c - '0');

	while (isdigit(c = std::cin.get()))
	{
		value = (10 * value) + (c - '0');
	}

	return value;
}

static char put_uint_buffer[16];

void put_uint(uint value)
{
	if (value == 0)
	{
		std::cout.put('0');

		return;
	}

	int i;
	for (i = SIZE_OF_ARRAY(put_uint_buffer); value != 0; value /= 10)
	{
		put_uint_buffer[--i] = '0' + (value % 10);
	}

	std::cout.write(put_uint_buffer + i, SIZE_OF_ARRAY(put_uint_buffer) - i);
}

struct data
{
	uint id;

	uint dmg;
	uint heal;

	data(uint _id, uint _dmg, uint _heal) : id(_id), dmg(_dmg), heal(_heal) {}
};

inline bool operator<(const data &a, const data &b)
{
	return a.dmg < b.dmg;
}

inline bool operator>(const data &a, const data &b)
{
	return a.dmg == b.dmg ? a.heal < b.heal : a.dmg > b.dmg;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);

	uint n = get_uint();
	uint z = get_uint();

	std::vector<data> easy, hard;

	easy.reserve(n + 2);
	hard.reserve(n + 2);

	for (uint i = 0; i < n; ++i)
	{
		uint dmg = get_uint();
		uint heal = get_uint();

		if (heal >= dmg)
		{
			easy.push_back(data(i + 1, dmg, heal));
		}
		else
		{
			hard.push_back(data(i + 1, dmg, heal));
		}
	}

	std::sort(easy.begin(), easy.end());

	std::sort(hard.begin(), hard.end(), std::greater<data>());

	uint64 hp = z;

	std::vector<uint> result;

	result.reserve(n + 2);

	for (const auto &monster : easy)
	{
		if (monster.dmg >= hp)
		{
			std::cout << "NIE\n"; return 0;
		}

		hp += monster.heal - monster.dmg; // >= 0 for "easy"

		result.push_back(monster.id);
	}

	for (uint i = 0; i < hard.size();)
	{
		const auto &hard_monster = hard[i];

		if (hard_monster.dmg >= hp)
		{
			std::cout << "NIE\n"; return 0;
		}

		uint hard_hp_lose = hard_monster.dmg - hard_monster.heal; // > 0 for "hard"

		for (++i; i < hard.size(); ++i)
		{
			const auto &safe_monster = hard[i]; // safe_monster.dmg <= hard_monster.dmg

			if (hard_monster.heal >= safe_monster.heal && hp - hard_hp_lose > safe_monster.dmg)
			{
				break;
			}

			uint safe_hp_lose = safe_monster.dmg - safe_monster.heal; // > 0 for "hard"

			if (hp - safe_hp_lose <= hard_monster.dmg)
			{
				break;
			}

			hp -= safe_hp_lose;

			result.push_back(safe_monster.id);
		}

		hp -= hard_hp_lose;

		result.push_back(hard_monster.id);
	}

	std::cout << "TAK\n";

	for (uint id : result)
	{
		put_uint(id); std::cout.put(' ');
	}

	return 0;
}