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
//Aleksander "kaalex" Kramarz

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

vector<pair<pair<int,int>,int> > A, B;
vector<int> res1, res2;
int n, a, d;
long long z, sum;

int main()
{
	scanf("%d%lld", &n, &z);
	sum = z;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d%d", &d, &a);
		sum += a-d;
		if(d <= a)
			A.push_back(make_pair(make_pair(d, a-d), i));
		else
			B.push_back(make_pair(make_pair(a, d-a), i));
	}
	if(!A.empty())
		sort(A.begin(), A.end());
	if(!B.empty())
		sort(B.begin(), B.end());
	for(int i = 0; i < A.size(); i++)
	{
		if(A[i].first.first >= z)
		{
			printf("NIE\n");
			return 0;
		}
		z += A[i].first.second;
		res1.push_back(A[i].second);
	}
	for(int i = 0; i < B.size(); i++)
	{
		if(B[i].first.first >= sum)
		{
			printf("NIE\n");
			return 0;
		}
		sum += B[i].first.second;
		res2.push_back(B[i].second);
	}
	printf("TAK\n");
	for(int i = 0; i < res1.size(); i++)
		printf("%d ", res1[i]);
	for(int i = res2.size()-1; i >= 0; i--)
		printf("%d ", res2[i]);
	printf("\n");
}