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
#include <iostream>
#include <vector>
#include <algorithm>
#include <ostream>

using namespace std;

struct Potwor {
	int i;
	int d;
	int a;
	Potwor(int i, int d, int a): i(i), d(d), a(a) {}
	bool operator<(const Potwor& p) const{ return this->a < p.a; }
	bool operator>(const Potwor& p) const{ return this->a > p.a; }
	friend ostream& operator<<(ostream &out, const Potwor& p);
};

ostream& operator<<(ostream& out, const Potwor& p){
	return out << "Potwor(" << p.i << ", " << p.d << ", " << p.a << ")";
}


int main(){

	int n, z;
	vector<Potwor> pot;

	cin >> n >> z;

	int d, a;
	for(int i=0; i<n; ++i){
		cin >> d >> a;
		pot.push_back(Potwor(i+1, d, a));
	}

	sort(pot.begin(), pot.end(), greater<Potwor>());
//	for(auto& p: pot)
//		cout << p << endl;

	vector<int> ret;
	while(pot.size() > 0){
		if((pot.size()%1000) == 0)
			cerr << pot.size() << endl;
		bool ok = false;
		for(int i=0; i<pot.size(); ++i){
			if(pot[i].d<z){
				z-=pot[i].d;
				z+=pot[i].a;
				ok=true;
				ret.push_back(pot[i].i);
				pot.erase(pot.begin() + i);
				break;
			}
		}
		if(!ok){
			cout << "NIE" << endl;
			return 0;
		}
	}

	cout << "TAK" << endl;
	for(auto i: ret){
		cout << i << " ";
	}
	cout << endl;

	return 0;
}