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
#include <stdlib.h>    
#include <iostream>
using namespace std;

long d[100000];
long a[100000];
long p[100000];
long seq[100000];

int compare (const void* e1, const void* e2)
{
	long i1= *((long*)e1);
	long i2= *((long*)e2);
	if (p[i1]>=0)
	{
		if (p[i2]<=0)
			return -1;
		return d[i1]-d[i2];
	}
	if (p[i2]>=0)
		return 1;
	return a[i2]-a[i1];
}

int main(int argc, char* argv[])
{
	long n;
	long long int z; 
	cin>>n>>z;
	for (long i=0;i<n;i++){
		cin>>d[i]>>a[i];
		seq[i]=i;
		p[i]=a[i]-d[i];
	}
	qsort (seq, n, sizeof(long), compare);

	for (long i=0;i<n;i++){
		long nextSeq = seq[i];
		z-=d[nextSeq];
		if (z<=0){
			cout<<"NIE"<<endl;
			return 0;
		}
		z+=a[nextSeq];
	}
	cout<<"TAK"<<endl;
	for (long i=0;i<n;i++)
		cout<<seq[i]+1<<" ";
	return 0;
}