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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <tuple>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define MP make_pair
#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

bool slay[100001];
int sila[100001];
int mix[100001];
int main()
{
	ios_base::sync_with_stdio(0);
	int n; int hp;
	cin>>n>>hp;
	vector<pair<int, int> > a;
	vector<pair<int, int> > b;
	vector<pair<pair<int, int>, int> > c;
	for(int i = 0; i < n; i++){
		int t1,t2;
		cin>>t1>>t2;
		sila[i] = t1;
		mix[i] = t2;
		if(t2 >= t1){
			a.PB(make_pair(t1, i));
		} else{
			b.PB(make_pair(t1, i));
			c.PB(make_pair(make_pair(t1-t2,-t1), i));
		}
	}
	vector<int> result;
	sort(a.begin(), a.end());
	sort(ALL(b));
	sort(ALL(c));
	reverse(ALL(c));

	for(int i = 0; i < (int)a.size(); i++){
		int nr = a[i].ND;
		if(hp <= sila[nr]){
			cout<<"NIE\n";
			return 0;
		} else{
			result.PB(nr + 1);
			hp += mix[nr] - sila[nr];
			slay[nr] = true;
		}
	}

	while(!b.empty()){

		while(!b.empty() && slay[b.back().ND]){
			b.pop_back();
		}
		while(!c.empty() && slay[c.back().ND]){
			c.pop_back();
		}
		if(b.empty())
			break;
		int nrC = b.back().ND;
		int nrB = c.back().ND;
		if(sila[nrB] >= hp){
			cout<<"NIE\n";
			return 0;
		}
		if(hp - sila[nrC] + mix[nrC] <= sila[nrB]){
			hp -= sila[nrB] - mix[nrB];
			slay[nrB] = true;
			result.PB(nrB + 1);
		} else{
			hp -= sila[nrC] - mix[nrC];
			slay[nrC] = true;
			result.PB(nrC + 1);
		}
		if(b.empty())
			break;
	}
	cout<<"TAK\n";
	REP(i, result.size()){
		cout<<result[i]<<" ";
	}
	cout<<'\n';
	return 0;
}