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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void qsort1(int *aid, int *ad, int *aa, int st, int ed) {
   int mid = (st+ed) / 2;

   int m_id = aid[mid];
   int m_d  =  ad[mid];
   int m_a  =  aa[mid];

   int t_id, t_d, t_a;

   aid[mid] = aid[ed];
    ad[mid] =  ad[ed];
    aa[mid] =  aa[ed];

   int x, x2=0;

   for (x=0; x < ed; x++) {
      if (ad[x] < m_d) {
         t_id    = aid[x];
         t_d     =  ad[x];
         t_a     =  aa[x];

         aid[x]  = aid[x2];
          ad[x]  =  ad[x2];
          aa[x]  =  aa[x2];

         aid[x2] = t_id;
          ad[x2] = t_d;
          aa[x2] = t_a;

         x2++;
      }
   }
   mid = x2;

   aid[ed]  = aid[mid];
    ad[ed]  =  ad[mid];
    aa[ed]  =  aa[mid];

   aid[mid] = m_id;
    ad[mid] = m_d;
    aa[mid] = m_a;


   if (mid - st > 1) {
      qsort1 (aid, ad, aa, st, mid-1);
   }
   if (ed - mid > 1) {
      qsort1 (aid, ad, aa, mid+1, ed);
   }
}


void qsort2(int *aid, int *ad, int *aa, int st, int ed) {

   int mid = (st+ed) / 2;

   int m_id = aid[mid];
   int m_d  =  ad[mid];
   int m_a  =  aa[mid];

   int t_id, t_d, t_a;

   aid[mid] = aid[ed];
    ad[mid] =  ad[ed];
    aa[mid] =  aa[ed];

   int x, x2=st;

   for (x=st; x < ed; x++) {
      if ((ad[x] > m_d) || (ad[x] == m_d) && (aa[x] > m_a)) {
         t_id    = aid[x];
         t_d     =  ad[x];
         t_a     =  aa[x];

         aid[x]  = aid[x2];
          ad[x]  =  ad[x2];
          aa[x]  =  aa[x2];

         aid[x2] = t_id;
          ad[x2] = t_d;
          aa[x2] = t_a;

         x2++;
      }
   }
   mid = x2;

   aid[ed]  = aid[mid];
    ad[ed]  =  ad[mid];
    aa[ed]  =  aa[mid];

   aid[mid] = m_id;
    ad[mid] = m_d;
    aa[mid] = m_a;


   if (mid - st > 1) {
      qsort2 (aid, ad, aa, st, mid-1);
   }
   if (ed - mid > 1) {
      qsort2 (aid, ad, aa, mid+1, ed);
   }
}

int main() {
   int n, hp;
   int i, j, idx1, idx2;
   scanf("%d %d", &n, &hp);

   int *aid = malloc (sizeof(int) * n);
   int *ad = malloc (sizeof(int) * n);
   int *aa = malloc (sizeof(int) * n);


   int a, d;

   idx1 = 0;
   idx2 = n-1;

   for (i=0; i < n; i++) {
      scanf("%d %d", &d, &a);

      if (a < d) {
         aid[idx2] = i+1;
         ad[idx2] = d;
         aa[idx2] = a;
         idx2--;
      } else {
         aid[idx1] = i+1;
         ad[idx1] = d;
         aa[idx1] = a;
         idx1++;
      }

   }
   qsort1 (aid, ad, aa, 0, idx2);
   qsort2 (aid, ad, aa, idx1, n-1);

   for (i=0; i < n; i++) {
      hp -= ad[i];
      if (hp <= 0) {
         printf("NIE");
         return 0;
      }
      hp += aa[i];
   }
   printf("TAK\n");
   for (i=0; i < n; i++) {
      printf("%d ", aid[i]);
   }

   return 0;
}