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>
#include <vector>
#define scanf(args...) (scanf(args) ? : 0)
typedef long long int LL;
const int MAXN = 1e5+5;

std::pair<int, int> T[MAXN];
int res[MAXN];

bool cmp1(int a, int b) {
    return T[a].first < T[b].first;
}

bool cmp2(int a, int b) {
    return T[a].second < T[b].second;
}

int main() {
    int n;
    LL hp;
    scanf("%d%Ld", &n, &hp);
    
    LL sum = 0;
    std::vector<int> v1, v2;
    for (int i=0; i<n; i++) {
        scanf("%d%d", &T[i].first, &T[i].second);
        if (T[i].first < T[i].second)
            v1.push_back(i);
        else {
            v2.push_back(i);
            sum += T[i].second-T[i].first;
        }
    }
    
    bool success = true;

    std::sort(v1.begin(), v1.end(), cmp1);
    int it1 = 0;
    for (int p: v1) {
        if (hp > T[p].first) {
            hp += T[p].second-T[p].first;
            res[it1++] = p;
        }
        else
            success = false;
    }
    
    hp += sum;

    std::sort(v2.begin(), v2.end(), cmp2);
    int it2 = n-1;
    for (int p: v2) {
        if (hp > T[p].second) {
            hp += T[p].first-T[p].second;
            res[it2--] = p;
        }
        else
            success = false;
    }

    if (!success)
        printf("NIE\n");
    else {
        printf("TAK\n");
        for (int i=0; i<n; i++)
            printf("%d ", res[i]+1);
        printf("\n");
    }
}