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
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;

struct STree
{
    int *Nodes;
    int *Leafs;
    int N;
    void printL()
    {
        for(int i=0;i<N; i++)
            printf("%d ", Leafs[i]);
        printf("\n");
    }
    void init(int cnode, int a, int b)
    {
        if(a==b) {
            Nodes[cnode]=a;
            return;
        }
        init(2*cnode,a,(a+b)/2);
        init(2*cnode+1,(a+b)/2+1,b);
        if(Leafs[Nodes[2*cnode]]>Leafs[Nodes[2*cnode+1]])
            Nodes[cnode]=Nodes[2*cnode];
        else
            Nodes[cnode]=Nodes[2*cnode+1];
    }
    int _query(int cnode, int ca, int cb, int &a, int &b)
    {
        if(cb<a||ca>b)
            return -1;
        if(ca>=a&&cb<=b)
            return Nodes[cnode];

        int left=_query(cnode*2, ca, (ca+cb)/2, a, b);
        int right=_query(cnode*2+1, (ca+cb)/2+1, cb, a, b);

        if(left==-1)
            return right;
        if(right==-1)
            return left;

        return Leafs[left]>Leafs[right] ? left : right;
    }
    int query(int a, int b)
    {
        return _query(1,0,N-1,a,b);
    }
    STree(int n, int *t)
    {
        N=n;
        n=2*pow(2,log2(n)+1);
        Nodes=new int[n];
        Leafs=t;
        init(1, 0, N-1);
    }
    void _update(int cnode, int ca, int cb, int &p)
    {
        if(ca==cb)
            return;
            
        if(p<=(ca+cb)/2)
            _update(cnode*2,ca,(ca+cb)/2,p);
        else
            _update(cnode*2+1,(ca+cb)/2+1,cb,p);
        
        if(Leafs[Nodes[2*cnode]]>Leafs[Nodes[2*cnode+1]])
            Nodes[cnode]=Nodes[2*cnode];
        else
            Nodes[cnode]=Nodes[2*cnode+1];
    }
    void update(int p, int v)
    {
        Leafs[p]=v;
        _update(1,0,N-1,p);
    }
    ~STree()
    {
        delete [] Nodes;
    }
};
int getin()
{
    int t;
    scanf("%d", &t);
    return t;
}
struct mob {
	int diff;
	int dmg;
	int elix;
	mob(int dmg, int elix) {
		this->diff=elix-dmg;
		this->dmg=dmg;
		this->elix=elix;
	}
	mob(){}
};

bool dmgsort(mob i,mob j) { return (i.dmg<j.dmg); }

#include <vector>

int main()
{
    int N,SHP;
    vector <int> result;
    scanf("%d %d",&N, &SHP);
    mob *moby;
    int *tab;
    tab=new int[N];
    int *tabDMG;
    tabDMG=new int[N];
    moby = new mob[N];
    for(int i=0; i<N; i++) {
    	int dmg, hp;
    	scanf("%d %d", &dmg, &hp);
    	moby[i] = mob(dmg, hp);
    }
    sort(moby, moby+N, dmgsort);
    for(int i=0; i<N; i++)
        tab[i]=moby[i].diff, tabDMG[i]=moby[i].dmg;
    STree drzewo(N, tab);
    int mobsleft=N;
    while(mobsleft) {
    	int* where = lower_bound(tabDMG, tabDMG+N, SHP);
    	int gdzie = drzewo.query(0,(int) (where-tabDMG));
    	if(tab[gdzie]<0)
    		break;
    	SHP -= moby[gdzie].dmg;
    	if(SHP<=0)
    		break;
    	SHP += moby[gdzie].elix;
    	drzewo.update(gdzie, -2000000000);
    	result.push_back(gdzie+1);
    	mobsleft--;
    }
    for(int i=N-1; i>=0; i--, mobsleft--) {
    	if(tab[i] != -2000000000) {
    		SHP -= moby[i].dmg;
    		if(SHP <= 0)
    			break;
    		SHP += moby[i].elix;
    		result.push_back(i+1);
    	}
    }
    if(SHP>0) {
    	puts("TAK");
    	for(int i=0; i<result.size(); i++)
    		printf("%d ", result[i]);
    }
    else
    	puts("NIE");
}