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
#define debug if(1)
// Grzegorz Guspiel
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i,n) for(int i=0;i<int(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second

template<typename T> void maxE(T& a, const T& b) { a = max(a, b); }
template<typename T> void minE(T& a, const T& b) { a = min(a, b); }

struct Monster {
    int req, gain, id;
};

vector<Monster> good, bad;
vector<int> result;
long long hp;

int goodKey(const Monster& m) { return m.req; }
int goodCmp(const Monster& a, const Monster& b) { return goodKey(a) < goodKey(b); }
int badKey(const Monster& m) { return -(m.req + m.gain); }
int badCmp(const Monster& a, const Monster& b) { return badKey(a) < badKey(b); }

bool apply(const Monster& m) {
    if (hp < m.req) return 0;
    hp += m.gain;
    assert(hp >= 0);
    result.pb(m.id);
    return 1;
}

bool simulate() {
    sort(ALL(good), goodCmp);
    sort(ALL(bad), badCmp);

    FOREACH (i, good) if (!apply(*i)) return 0;
    FOREACH (i, bad) if (!apply(*i)) return 0;

    return 1;
}

int main() {
    ios_base::sync_with_stdio(0);

    int n; cin >> n >> hp;
    hp--;
    REP (i, n) {
        int dmg, repair;
        cin >> dmg >> repair;
        Monster monster;
        monster.gain = repair - dmg;
        monster.req = dmg;
        monster.id = i + 1;
        if (monster.gain >= 0) good.pb(monster);
        else bad.pb(monster);
    }

    if (simulate()) {
        cout << "TAK" << endl;
        FOREACH (i, result) cout << *i << " ";
        cout << endl;
    } else cout << "NIE" << endl;
	return 0;
}