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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// :D
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <utility>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <set>

using namespace std;
typedef pair <int, int> P;
typedef pair <int, P> PP;
const int MAXN = 100000;

int n, z, a, b, hp;
P t[25];
vector <int> res;
vector <int> f;
bool killed[MAXN + 5];
vector <PP> monsters;
priority_queue <PP, vector <PP>, greater <PP> > QPP;
priority_queue <PP> QPPF;

bool check();

int main() {
    scanf("%d %d", &n, &hp);
    if(n < 11) {
        for(int i = 1; i <= n; ++i) {
            f.push_back(i);
            scanf("%d %d", &t[i].first, &t[i].second);            
        }
        do {
            if(check()) {
                return 0;
            }
        } while(next_permutation(f.begin(), f.end()));
        printf("NIE\n");
        return 0;
    }
    for(int i = 1; i <= n; ++i) {
        scanf("%d %d", &a, &b);
        if(a <= b) {
            QPP.push(make_pair(a,make_pair(b - a, i)));
            killed[i] = true;
        } else {
            monsters.push_back(make_pair(a, make_pair(b - a, i)));
            QPPF.push(make_pair(b - a,make_pair(a, i))); // a?
        }
    }
    sort(monsters.begin(), monsters.end());
    while(!QPP.empty()) {
        PP act = QPP.top(); QPP.pop();
        if(act.first >= hp) {
            printf("NIE\n");
            return 0;
        } else {
            res.push_back(act.second.second);
            killed[act.second.second] = true;
            hp += act.second.first;
        }
    }
    while(!QPPF.empty()) {
        PP act = QPPF.top();
        if(killed[act.second.second]) {
            QPPF.pop();
            continue;
        }
        if(hp + act.first <= monsters.back().first && act.second.second != monsters.back().second.second) {
            while(monsters.size() && killed[monsters.back().second.second]) {
                monsters.pop_back();
            }
            if(!killed[monsters.back().second.second] && monsters.back().first >= hp) {
                printf("NIE\n");
                return 0;
            }
            if(monsters.size()) {
                hp += monsters.back().second.first;
                res.push_back(monsters.back().second.second);
                killed[monsters.back().second.second] = true;
                monsters.pop_back();
            }
            continue;
        }
        if(act.second.first >= hp) {
            printf("NIE\n");
            return 0;
        } else {
            if(!killed[act.second.second]) {
                hp += act.first;
                killed[act.second.second] = true;
                res.push_back(act.second.second);
            }
            QPPF.pop();
        }
    }
    printf("TAK\n");
    for(int i = 0; i < res.size(); ++i) {
        printf("%d ", res[i]);
    }
    printf("\n");
    return 0;
}

bool check() {
    res.clear();
    int HP = hp;
    for(int i = 0; i < n; ++i) {
        int act = f[i];
        hp -= t[act].first;
        if(hp <= 0) {
            hp = HP;
            return false;
        }
        hp += t[act].second;
        res.push_back(act);
    }
    printf("TAK\n");
    for(int i = 0; i < n; ++i) {
        printf("%d ", res[i]);
    }
    printf("\n");
    return true;
}