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
// Karol Kosinski
#include <cstdio>
#include <queue>
#include <vector>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
//#define DEBUG(args...) printf(args)
#define DEBUG(args...)
using namespace std;

typedef long long LG;

struct monster {
        int d, a, no;
};

struct sort_plus {
        bool operator () (const monster &x, const monster &y) {
                return x.d - y.d > 0;
        }
};

struct sort_minus {
        bool operator () (const monster &x, const monster &y) {
                return x.a - y.a < 0;
        }
};

int main() {
        int n; LG z;
        scanf("%d%lld", &n, &z);
        priority_queue<monster, vector<monster>, sort_plus> P;
        priority_queue<monster, vector<monster>, sort_minus> Q;
        FOR(i,0,n) {
                monster m;
                scanf("%d%d", &m.d, &m.a);
                m.no = i + 1;
                if(m.a - m.d >= 0) P.push(m);
                else Q.push(m);
                DEBUG("stop\n");
        }
        vector<int> res;
        while(!P.empty()) {
                monster cm = P.top(); P.pop();
                z -= cm.d;
                if(z <= 0) {
                        printf("NIE\n");
                        return 0;
                }
                z += cm.a;
                res.push_back(cm.no);
                DEBUG("[%d]: -%d +%d", cm.no, cm.d, cm.a);
                DEBUG("\t z = %lld\n", z);
        }
        while(!Q.empty()) {
                monster cm = Q.top(); Q.pop();
                z -= cm.d;
                if(z <= 0) {
                        printf("NIE\n");
                        return 0;
                }
                z += cm.a;
                res.push_back(cm.no);
                DEBUG("(%d): -%d +%d", cm.no, cm.d, cm.a);
                DEBUG("\t z = %lld\n", z);
        }
        printf("TAK\n");
        FOR(i,0,n) printf("%d ", res[i]);
        printf("\n");
        return 0;
}