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
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <deque>
using namespace std;

deque <pair <pair <int, int>, int > > vVec;
deque <pair <pair <int, int>, int > > vVec2;
stack <pair <pair <int, int>, int > > sTempStack;
vector <int> vID;

int main()
{
	ios_base::sync_with_stdio(0);
	int n, a, b;
	long long m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> a >> b;
		vVec.push_back(make_pair(make_pair(b, a), i));
	}
	sort(vVec.begin(), vVec.end());
	while (vVec.size() != 0)
	{
//		cout << "vec: " << vVec.size() << endl;
//		cout << m << " - " << vVec.back().first.second << endl;
		if (m - vVec.back().first.second > 0)
		{
//			cout << vVec.back().first.first << " " << vVec.back().first.second << " " << vVec.back().second << endl;
			m -= vVec.back().first.second;
			m += vVec.back().first.first;
			vID.push_back(vVec.back().second);
			vVec.pop_back();
			while (vVec2.size() != 0 && m - vVec2.back().first.second > 0)
			{
				sTempStack.push(vVec2.back());
				vVec2.pop_back();
			}
			while (sTempStack.size() != 0)
			{
				vVec.push_back(sTempStack.top());
				sTempStack.pop();
			}
		}
		else
		{
			while (vVec.size() != 0 && m - vVec.back().first.second <= 0)
			{
				vVec2.push_front(vVec.back());
				vVec.pop_back();
			}
		}
	}
	if (vVec.size() == 0 && vVec2.size() == 0)
	{
		cout << "TAK" << endl;
		for (int i = 0; i < vID.size(); i++)
		{
			cout << vID[i] << " ";
		}
	}
	else
	{
//		cout << vVec.size() << " " << vVec2.size() << endl;
		cout << "NIE" << endl;
	}
	
	return 0;

}