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
#include <cstdio>
#include <algorithm>
using namespace std;

struct Monstah {
    int id, dmg, hlth, diff;

    Monstah() {}
    Monstah(int i, int d, int a) {
        id = i;
        dmg = d;
        hlth = a;
        diff = hlth - dmg;
    }
};

int sign(int x) {
    if(x >= 0) return 1;
    else return -1;
}

bool operator<(const Monstah &a, const Monstah &b) {
    int sa = sign(a.diff), sb = sign(b.diff);
    if(sa > sb)
        return true;
    else if(sa < sb)
        return false;

    if(sa > 0) {
        // when diff >= 0 we prefer less dmg first
        if(a.dmg <= b.dmg)
            return true;
    } else {
        // when diff < 0 we prefer more dmg first
        if(a.dmg > b.dmg && 1 + a.hlth > b.dmg)
            return true;
    }

    return false;
}

int main() {
    int n, z, d, a;
    Monstah ar[100000];
    int ans[100000];
    scanf("%d %d", &n, &z);
    for(int i = 0; i < n; ++i) {
        scanf("%d %d", &d, &a);
        ar[i] = Monstah(i + 1, d, a);
    }

    sort(ar, ar + n);
    //for(int i = 0; i < n; ++i)
        //printf("id=%d dmg=%d hlth=%d diff=%d\n", ar[i].id, ar[i].dmg, ar[i].hlth, ar[i].diff);

    for(int i = 0; i < n; ++i) {
        z-= ar[i].dmg;
        if(z <= 0)
            break;
        z += ar[i].hlth;
        ans[i] = ar[i].id;
    }

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

    return 0;
}