Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8. Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//============================================================================
// Name        : kug.cpp
// Author      : Grzegorz Gajoch
// Description : Potyczki 2014 - kuglarz
//============================================================================


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <list>

using namespace std;

#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(x, n) for(int x=0; x < (n); ++x)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define SIZE(n) (int)n.size()
#define ALL(c) c.begin(),c.end()
#define PB(n) push_back(n)
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<bool> VB;
#define MP make_pair
#define ST mon
#define ND second
const int INF = 1000000001;

#undef DBGGG

#ifdef DBGGG
	#define debug
	#define deb printf
#else
	#undef debug
	#define deb //printf
#endif

int N, Z;
struct monster
{
	int cost, gain, number;
	void print()
	{
		printf("(%d %d |%d)\n", cost, gain, number);
	}
};
bool operator<(monster a, monster b)
{
	if (a.cost == b.cost)
	{
		if (a.gain == b.gain)
		{
			return (a.number > b.number);
		}
		return (a.gain > b.gain);
	}
	return (a.cost > b.cost);
}
struct by_gain {
	bool operator() (const monster& a, const monster& b)
	{
		if (a.gain == b.gain)
		{
			if (a.cost == b.cost)
			{
				return (a.number > b.number);
			}
			return (a.cost > b.cost);
		}
		return (a.gain > b.gain);
	}
};
vector<int> res;
int main(int argc, char **argv)
{
	scanf("%d %d", &N, &Z);
	monster mon;
	int d, a;
	multiset<monster> more, less;
	multiset<monster, by_gain> less_by_gain;
	long long LAST_DEL = 0;
	REP(i, N)
	{
		scanf("%d %d", &d, &a);
		mon.cost = d;
		mon.gain = a-d;
		mon.number = i;
		if (a == 0)
		{
			LAST_DEL += d;
		}
		else if( mon.gain >= 0 ) 
			more.insert(mon);
		else
		{
			less.insert(mon);
			less_by_gain.insert(mon);
		}
	}
#ifdef debug
	printf("first phase: %d\n",Z);
	for (auto x : more)
	{
		x.print();
	}
#endif
	while (more.size() > 0)
	{
		deb("now: %d\n", Z);
		mon.cost = Z-1;
		mon.gain = INF;
		mon.number = 0;
		auto x = more.lower_bound(mon);
		if (x != more.end())
		{
			mon = *x;
#ifdef debug
			mon.print();
#endif
			if (Z <= mon.cost) goto fail;
			Z += mon.gain;
			res.push_back(mon.number);
			more.erase(x);
		}
		else
		{
			goto fail;
		}
		if (Z <= 0) goto fail;
	}










#ifdef debug
	printf("after first: %d\nLESS:\n", Z);
	for (auto x : less)
	{
		printf("(%d %d)\n", x.cost, x.gain);
	}
	printf("LESS BY GAIN:\n");
	for (auto x : less_by_gain)
	{
		printf("(%d %d)\n", x.cost, x.gain);
	}
#endif

	
	while (less_by_gain.size() > 0 || less.size() > 0)
	{
		auto less_top_itr = less_by_gain.begin(),
			biggest_val_itr = less.begin();

		if (Z + less_top_itr->gain > biggest_val_itr->cost) //mo�na ciachn��
		{
			if (Z <= less_top_itr->cost) goto fail;
			Z += less_top_itr->gain;
#ifdef debug
			printf("TAKING: Z = %d ",Z);
			monster x = *less_top_itr;
			x.print();
#endif
			res.push_back(less_top_itr->number);
			less.erase(*less_top_itr);
			less_by_gain.erase(less_top_itr);
		}
		else //trzeba du�ego
		{
			if (Z <= biggest_val_itr->cost) goto fail;
			Z += biggest_val_itr->gain;
#ifdef debug
			printf("TAKING: Z = %d ", Z);
			monster x = *biggest_val_itr;
			x.print();
#endif
			res.push_back(biggest_val_itr->number);
			less.erase(biggest_val_itr);
			less_by_gain.erase(*biggest_val_itr);
		}
		if (Z <= 0) goto fail;
	}

	Z -= LAST_DEL;
	if (Z > 0)
	{
		printf("TAK\n");
		for (auto x : res)
			printf("%d ", x+1);
		printf("\n");
	}
	else
	{
	fail:
		printf("NIE\n");
	}
	return 0;
}