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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <queue>
using namespace std;
typedef pair<pair<int,int>,int > fight;
bool read(priority_queue<fight> &p,priority_queue<fight> &m,int N,long long &points2){
for(int i=0;i<N;++i){
	int d,a;
	cin>>d>>a;
	long long s=a-d; points2+=s;
	if(s<0) m.push(make_pair(make_pair(-a,d),i));
	else p.push(make_pair(make_pair(-d,a),i));

}

return points2>0;}

bool dequeue_p(priority_queue<fight> &x,long long &points,vector<int>& v){
	bool b=true;
	while(b && !x.empty()){
	fight d=x.top();
	if(-d.first.first>=points) b=false;
	else {v.push_back(d.second);
	points+=d.first.second+d.first.first;
	x.pop();
	}
	//cout<<"pop"<<d.second<<endl;
	}
	return (x.empty());
}

int main()
{
	ios_base::sync_with_stdio(false);
	int N;
	cin>>N;
	long long points,points2;
	cin>>points;
	points2=points;
	priority_queue<fight> p,m;
	vector<int> v1,v2;

	if(!read(p,m,N,points2)) cout<<"NIE"<<endl;
	else {
		if(!dequeue_p(p,points,v1) || !dequeue_p(m,points2,v2)) cout<<"NIE"<<endl;

		else {
			cout<<"TAK"<<endl;
		for (auto i:v1) cout<<i+1<<" ";
		for(auto it=v2.rbegin();it!=v2.rend();++it)
			cout<<(*it)+1<<" ";
			cout<<endl;
		}
	}
	return 0;
}