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
#include <cstdio>
#include <algorithm>
#define MONSTER_COUNT 101000
// #define DEBUG 1
using namespace std;
typedef long long int lli;
class M
{
	public:
		M(){}
		M(int Id, int D, int H)
		{
			id = Id;
			d = D;
			h = H;
			r = H + D;
		}
		int id;
		int d;
		int h;
		int r;

		void operator=(const M& b)
		{
			this->id = b.id;
			this->d = b.d;
			this->h = b.h;
			this->r = b.r;
		}
		bool operator==(const M& b) const 
		{ 
			return this->id == b.id; 
		}
		bool operator!=(const M& b) const 
		{
			return !(*this == b);
		}
		bool operator< (const M& b) const
		{
			if(this->h >= 0 && b.h < 0) return true;
			if(this->h < 0 && b.h >= 0) return false;
			if(this->h < 0 && b.h < 0)
			{
				if(this->d < b.d) return false;
				if(this->d > b.d) return true;
				if(this->h > b.h) return true;
				if(this->h < b.h) return false;
				
			}
			else
			{
				if(this->h > b.h) return true;
				if(this->h < b.h) return false;
				if(this->d < b.d) return true;
				if(this->d > b.d) return false;
			}
			return this->id < b.id;
		}
		bool operator> (const M& b) const
		{
			return b < *this;
		}
		bool operator<=(const M& b) const
		{
			return !operator> (b);
		}
		bool operator>=(const M& b) const
		{
			return !operator< (b);
		}
};

lli h;
int nm, n, a, b, d, i;
M mm[MONSTER_COUNT];
int m[MONSTER_COUNT];
M *p, *q;
int *r, *s;
int main()
{
	p = mm;
	r = m;
	scanf("%d%lld", &n, &h);
	#ifdef DEBUG
		printf("n=%d h=%lld\n", n, h);
	#endif
	for(i = 1; i <= n; ++i)
	{
		scanf("%d%d", &a, &b);
		#ifdef DEBUG
			printf("%d: a=%d b=%d\n", i, a, b);
		#endif
		d = b - a;
		#ifdef DEBUG
			printf("%d: a=%d b=%d d=%d h=%lld\n", i, a, b, d, h);
		#endif
		if(d >= 0 && a < h)
		{
			*r = i;
			h += (int)d;
			++r;
			#ifdef DEBUG
				printf("(d = %d) > 0 && (a = %d) > (h = %lld) => adding to health\n", d, a, h);
			#endif
		}
		else
		{
			*p = M(i, a, d);
			++p;
		}
	}
	#ifdef DEBUG
		puts("done read");
	#endif
	sort(mm, p);
	#ifdef DEBUG
		puts("done sort");
		printf("h = %lld\n", h);
	#endif
	for(q = mm; q < p; ++q)
	{
		h -= q->d;
		#ifdef DEBUG
			printf("%d: (d = -%d) r = %d | %lld\n", q-mm, q->d, q->r, h);
		#endif
		if(h <= 0)
		{
			puts("NIE");
			return 0;
		}
		h += q->r;
		*r = q->id;
		++r;
	}
	puts("TAK");
	for(s = m; s < r; ++s) printf("%d ", *s);
	return 0;
}