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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define SZ(x) ((int)(x.size()))
#define fi first
#define se second
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define DBG cerr << "debug here" << endl;
#define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl;

typedef long long num;
typedef double fl;

const int NINF = -1000000000;

typedef struct elem 
{
    int v, id; 
} elem;

bool mycmp(const elem& e1, const elem& e2)
{
    return e1.v < e2.v;
}
typedef struct node
{
    int v, d;
} node;

node mymax(node v, node u)
{
    if(v.v > u.v) return v;
    return u;
}
const int MAX_N = 100010;

vector<elem> b[MAX_N];

const int M = 131072;

node t[2 * M];

void insert(node nd) 
{
    int x = M + nd.d;
    t[x] = nd;
    while(x != 1)
    {
        x /= 2;
        t[x] = mymax(t[2 * x], t[2 * x + 1]);
    }
}
node query(int a, int b)
{
    int va = M + a;
    int vb = M + b;
    node res = mymax(t[va], t[vb]);
    while(va / 2 != vb / 2)
    {
        if(va % 2 == 0)
        {
            res = mymax(res, t[va + 1]);
        }
        if(vb % 2 == 1)
        {
            res = mymax(res, t[vb - 1]);
        }
        va /= 2;
        vb /= 2;
    }
    return res; 
}
int main()
{
    ios_base::sync_with_stdio(0);
    int n, z;
    cin >> n >> z;
    FOR(i, n)
    {
        int d, a;
        elem e;
        cin >> d >> a;
        e.v = a - d;
        e.id = i + 1;
        b[d].pb(e);
    }
    node nd;
    nd.v = NINF;
    FOR(i, M)
    {
        nd.d = i;
        insert(nd);
    }
    FOR(i, MAX_N) 
    {
        if(!b[i].empty())
        {
            sort(ALL(b[i]), mycmp);
            nd.v = b[i].back().v;
            nd.d = i; 
            insert(nd);
        }
    }
    int cnt = 0;
    vector<int> ans;
    int flag = 0;
    while(1)
    {        
        if(cnt == n) break;        
        nd = query(0, min(MAX_N - 1, z - 1));
        if(nd.v == NINF) 
        {
            break;
        }
        else if(nd.v < 0)
        {
            flag = 1;
            break;
        }
        z = z + nd.v;
        if(z == 0) break;
        cnt++;
        int d = nd.d;
        ans.pb(b[d].back().id);
        b[d].pop_back();
        if(!b[d].empty())
        {
            nd.v = b[d].back().v;
        }
        else
        {
            nd.v = NINF;
        }
        insert(nd);
    }
    if(flag)
    {
        int k = min(MAX_N - 1, z - 1);
        while(1)
        {
            for(; k >= 0; --k)
            {
                if(!b[k].empty())
                {
                    break;
                }
            }
            if(k < 0)
            {
                break;
            }
            elem n1 = b[k].back();
            node n2 = query(0, min(MAX_N - 1, z - 1));
            int d = n2.d;
            if(n2.v == NINF)
            {
                break;
            }
            if(z + n2.v >= k)
            {
                z += n2.v;
                ans.pb(b[d].back().id);
                b[d].pop_back();
                if(!b[d].empty())
                {
                    n2.v = b[d].back().v;
                }
                else
                {
                    n2.v = NINF;
                }
                insert(n2); 
            }
            else
            {
                z += n1.v;
                ans.pb(n1.id);
                b[k].pop_back();
                n2.d = k;
                if(!b[k].empty())
                {
                    n2.v = b[k].back().v;
                }
                else
                {
                    n2.v = NINF;
                }
                insert(n2);
            }
            cnt++;
        }
    }
    if(cnt == n)
    {
        cout << "TAK" << endl;
        FOR(i, n) cout << ans[i] << " ";
        cout << endl;
    }
    else
    {
        cout << "NIE" << endl;
    }
    return 0;
}