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
167
168
169
170
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <deque>
#include <iterator>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <cmath>
using namespace std;
#define HAT 128 * 1024

int n, hp;
int used[100001], indexof[100001];
int tree[2 * HAT];
vector<pair<int, pair<int, int> > > negative;
vector<pair<int, pair<int, int> > > even;
vector<pair<int, pair<int, int> > > positive;
vector<pair<int, pair<int, int> > > boxes;

deque<int> ret;
deque<int> ret2;

bool isBetter(int posA, int posB) {
	if (posB == -1) return true;
	if (posA == -1) return false;
	if (-boxes[posA].second.first == -boxes[posB].second.first)
		return boxes[posA].first > boxes[posB].first;
	return -boxes[posA].second.first < -boxes[posB].second.first;
}

void add(int pos) {
	int v = pos + HAT;
	while (v) {
		if (isBetter(pos, tree[v])) tree[v] = pos;
		v /= 2;
	}
}

void remove(int pos) {
	int v = pos + HAT;
	tree[v] = -1;
	v /= 2;
	while (v) {
		tree[v] = -1;
		if (isBetter(tree[2 * v + 1], tree[v])) tree[v] = tree[2 * v + 1];
		if (isBetter(tree[2 * v], tree[v])) tree[v] = tree[2 * v];
		v /= 2;
	}
}

int getMin(int pos) {
	int v = pos + HAT;
	int best = tree[v];
	while (v) {
		if (v/2 && v%2 && isBetter(tree[v/2], best)) best = tree[v/2];
		v /= 2;
	}
	return best;
}

bool go() {
	if (boxes.size() == 0) return false;
	memset(tree, -1, sizeof(tree));
	sort(boxes.begin(), boxes.end());
	int res = 1;
	int sum_wa = 0, last_wa = -boxes[0].second.first, last_wy = boxes[0].first;
	used[0] = 1;

	add(0);
	ret2.push_back(boxes[0].second.second);

	for (int i = 1; i < boxes.size(); i++) {
		int cand1 = boxes[i].first - sum_wa - last_wa;
		int cand2 = last_wy - sum_wa + boxes[i].second.first;
		if (cand1 >= cand2 && cand1 >= 0) {
			res++;
			sum_wa += last_wa;
			last_wa = -boxes[i].second.first;
			last_wy = boxes[i].first;
			used[i] = 1;

			add(i);
			ret2.push_back(boxes[i].second.second);
		}
		else if (cand2 > cand1 && cand2 >= 0) {
			res++;
			sum_wa -= boxes[i].second.first;
			used[i] = 1;

			add(i);
			ret2.push_front(boxes[i].second.second);
		}
		else {
			int best_i = 0, best_v = 0;
			//for (int j = i - 1; j >= 0; j--) {
			//	if (used[j] && best_v < -boxes[j].second.first) {
			//		best_i = j;
			//		best_v = -boxes[j].second.first;
			//	}
			//}
			best_i = getMin(i);
			if (best_i == -1) best_i = 0;
			best_v = -boxes[best_i].second.first;
			if (best_v >= -boxes[i].second.first) {
				used[best_i] = 0;
				sum_wa += last_wa + boxes[best_i].second.first;
				last_wa = -boxes[i].second.first;
				last_wy = boxes[i].first;
				used[i] = 1;

				add(i);
				remove(best_i);
				for (int k = 0; k < ret2.size(); k++) if (ret2[k] == boxes[best_i].second.second) ret2[k] = boxes[i].second.second;
			}
		}
	}

	return res != boxes.size();
}

int main() {
	scanf("%d %d", &n, &hp);
	for (int i = 0; i < n; i++) {
		int dmg, restore;
		scanf("%d %d", &dmg, &restore);
		if (dmg > restore) negative.push_back(make_pair(dmg, make_pair(restore, i + 1)));
		if (dmg == restore) even.push_back(make_pair(dmg, make_pair(restore, i + 1)));
		if (dmg < restore) positive.push_back(make_pair(dmg, make_pair(restore, i + 1)));
	}

	bool failed = false;

	sort(positive.begin(), positive.end());
	for (int i = 0; i < positive.size(); i++) {
		if (hp <= positive[i].first) failed = true;
		else {
			hp += positive[i].second.first - positive[i].first;
			ret.push_back(positive[i].second.second);
		}
	}

	for (int i = 0; i < even.size(); i++) {
		if (hp <= even[i].first) failed = true;
		else ret.push_back(even[i].second.second);
	}

	if (!failed) {
		
		for (int i = 0; i < negative.size(); i++) {
			boxes.push_back(make_pair(hp - negative[i].first - 1, make_pair(negative[i].second.first - negative[i].first, negative[i].second.second)));
			if (hp - negative[i].first - 1 < 0) failed = true;
		}
		 
		failed = failed || go();
	}

	if (failed) printf("NIE\n");
	else {
		printf("TAK\n");
		for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i]);
		for (int i = 0; i < ret2.size(); i++) printf("%d ", ret2[i]);
	}

	//cin >> n;

	return 0;
}