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
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <algorithm>

using namespace std;

long long int n,z;
long long int n2;
bool check = true;
bool dalej = false;

struct p 
{
	int d,a,it;
	bool used;

};

bool operator<(p a,p b){
	if(a.d >  b.d)return true;
	else if(a.d == a.d){
		if(a.a >  b.a)return true;
		else return false;
	}
	else return false;
}

int main(){
	ios_base::sync_with_stdio(0);

	cin >> n >> z;

	p tab[n];
	int kol[n];

	n2 = n;

	for (int i = 0; i < n; ++i)
	{
		cin >> tab[i].d >> tab[i].a;
		tab[i].it = i + 1;
		tab[i].used = false;
	}

	sort(tab,tab + n);

	while(n2 > 0){
		dalej = false;

		for (int i = 0; i < n; ++i)
		{
			if(tab[i].d < z && tab[i].used == false )
			{	
				z -= tab[i].d;
				z += tab[i].a;

				kol[n - n2] = tab[i].it;
				tab[i].used = true;
				n2--;

				dalej = true;
			}
			if(dalej)break;
		}
		if(!dalej)
		{
			cout << "NIE";
			break;
		}
	}
	if(n2 == 0){

		cout << "TAK\n";

		for (int i = 0; i < n; ++i)
		{
			cout << kol[i] << " ";
		}
	}
		cout << "\n";	
	return 0;
}