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
#include <cstdio>
#include <algorithm>

typedef long long ll;

struct Monster {
    int dmg;
    int heal;
    int id;
};

bool monsterCmp(const Monster &a, const Monster &b) {
    int adiff = a.heal - a.dmg;
    int bdiff = b.heal - b.dmg;

    if (adiff > 0) {
        if (bdiff > 0) {
            return a.dmg < b.dmg;
        } else {
            return true;
        }
    } else if (adiff == 0) {
        if (bdiff < 0) {
            return true;
        } else {
            return false;
        }
    } else /* adiff < 0 */ {
        return a.heal > b.heal;
    }
}

int main() {
    int n;
    ll hp;
    scanf("%d %lld", &n, &hp);

    Monster *monsters = new Monster[n];
    for (ll i = 0; i < n; ++i) {
        scanf("%d %d", &monsters[i].dmg, &monsters[i].heal);
        monsters[i].id = i + 1;
    }

    std::sort(monsters, monsters + n, monsterCmp);

    bool dead = false;
    for (int i = 0; i < n; ++i) {
        hp -= monsters[i].dmg;
        if (hp <= 0) {
            dead = true;
            break;
        }

        hp += monsters[i].heal;

        //printf("kill %d (%d/%d), hp: %d\n", monsters[i].id, monsters[i].dmg, monsters[i].heal - monsters[i].dmg, hp);
    }

    if (dead) {
        printf("NIE\n");
    } else {
        printf("TAK\n");
        for (int i = 0; i < n; ++i) {
            printf("%d ", monsters[i].id);
        }
        printf("\n");
    }

    return 0;
}