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
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <list>
using namespace std;

int main()
{
	int n, z;
	scanf("%d %d", &n, &z);

	map<int,set<pair<int, int> > > positive, negative;

	for (int i = 1; i <= n; i++)
	{
		int d, a;
		scanf("%d %d", &d, &a);

		pair<int,int> pair (a, i);
		if (a - d >= 0)
			positive[d].insert(pair);
		else
			negative[d].insert(pair);
	}

	list<int> track;
	for (map<int,set<pair<int, int> > >::iterator it = positive.begin(); it != positive.end(); it++)
	{
		int d = it->first;
		for (set<pair<int, int> >::reverse_iterator it2 = it->second.rbegin(); it2 != it->second.rend(); it2++)
		{
			z -= d;
			if (z <= 0)
			{
				puts("NIE");
				return 0;
			}
			z += it2->first;
			track.push_back(it2->second);
		}
	}

        for (map<int,set<pair<int, int> > >::reverse_iterator it = negative.rbegin(); it != negative.rend(); it++)
        {
                int d = it->first;
                for (set<pair<int, int> >::reverse_iterator it2 = it->second.rbegin(); it2 != it->second.rend(); it2++)
                {
                        z -= d;
                        if (z <= 0)
                        {
                                puts("NIE");
                                return 0;
                        }
                        z += it2->first;
			track.push_back(it2->second);
                }
        }

	puts("TAK");
	for (list<int>::iterator it = track.begin(); it != track.end(); it++)
		printf((it == track.begin() ? "%d" : " %d"), *it);
}