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
#include<fstream>
#include<vector>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<list>
using namespace std;

struct monster{
    int taken;
    int given;
    int diff;
    int order;
};

int compare (const void * a, const void * b)
{
      return ( (*(monster*)a).taken - (*(monster*)b).taken );
}


monster mons[1000000];
long long int sum;

int main(int argc, char**argv)
{
 int ncases;
 int numwords;
 bool pos = false;
 int num;
 int inithp;
 int tmp = scanf("%d %d", &num, &inithp);
 for(int j = 0; j < num; j++){
     tmp = scanf("%d %d", &mons[j].taken, &mons[j].given);
     mons[j].diff = mons[j].given - mons[j].taken;
     mons[j].order = j;
     sum += mons[j].diff;
 }
 if(sum + inithp <= 0){
     printf("NIE\n");
 }
 else{
     list<int> up;
     list<int> down;
     qsort(mons, num, sizeof(monster), compare);
     long long int cursum = inithp;
     if(mons[0].taken > inithp){
             printf("NIE\n");
             return 0;
         }
     for(int j = 0; j < num; j++){
         if(mons[j].diff >= 0){
             cursum += mons[j].diff;
             up.push_back(mons[j].order);
         }
         else
         {
             down.push_front(mons[j].order);
         }
         if(cursum < mons[j+1].taken && j != num - 1){
             printf("NIE\n");
             return 0;
         }
     }
     printf("TAK\n");
     for (std::list<int>::iterator it = up.begin() ; it != up.end(); ++it)
              std::cout << *it + 1 << ' ';
     for (std::list<int>::iterator it = down.begin() ; it != down.end(); ++it)
              std::cout << *it + 1 << ' ';
     printf("\n");
 }
 return 0;
}