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
82
83
84
#include <cstdio>
#include <queue>
#include <utility>
#include <algorithm>
#include <iostream>
using namespace std;


struct Dragon {
    int id, d, a;
    Dragon(int id, int d, int a) : id(id), d(d), a(a) {}
};

struct CmpDragon {
    bool operator()(Dragon *a, Dragon *b)
    {
        if (a->d == b->d)
            return a->a < b->a;
        return a->d < b->d;
    }
};

priority_queue<Dragon*, vector<Dragon*>, CmpDragon> for_worse;
vector<Dragon*> for_better;
vector<int> result;

bool alg(int z)
{
    for (int i = 0; i < for_better.size(); i++) {
        //cout << "z : " << z << endl;
        if (for_better[i]->d >= z)
            return false;
        result.push_back(for_better[i]->id);
        //cout << "push1 " << for_better[i]->id << endl;
        z += -for_better[i]->d + for_better[i]->a;
    }

    while (!for_worse.empty()) {
        //cout << "z: " << z << endl;
        Dragon &dragon = *(for_worse.top());
        for_worse.pop();

        result.push_back(dragon.id);
        //cout << "push2 " << dragon.id << endl;

        if (dragon.d >= z)
            return false;
        
        z += -dragon.d + dragon.a;
    }

    return true;
}

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

    for_better.reserve(n);

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

        if (a >= d) {
            for_better.push_back(new Dragon(i + 1, d, a));
        } else {
            for_worse.push(new Dragon(i + 1, d, a));
        }
    }

    sort(for_better.begin(), for_better.end(), CmpDragon());

    if (alg(z)) {
        printf("TAK\n");
        for (int i = 0; i < result.size(); i++) {
            printf("%d ", result[i]);
        }
        printf("\n");
    } else {
        printf("NIE\n");
    }
}