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
#include <stdio.h>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
typedef int I;
typedef tuple<I,I,I> E;
typedef vector<E> V;

inline bool check(I z, const V& add, const V& sub) {
  for (const E& e : add) {
    if (get<0>(e) >= z) {
      return false;
    }
    z += (get<2>(e) - get<0>(e));
  }
  for (const E& e : sub) {
    if (z <= get<2>(e)) {
      return false;
    }
    z -= (get<0>(e) + get<2>(e));
  }
  return true;
}

inline void print(const V& v, bool& ns) {
    for (const E& e : v) {
      if (ns)
        ns = false;
      else
        printf(" ");
      printf("%d", get<1>(e));
    }
}

int main() {
  I nn, z;
  scanf("%d %d\n", &nn, &z);
  V add, sub;
  add.reserve(nn);
  sub.reserve(nn);
  for (I n=0; n<nn; ++n) {
    I d, a;
    scanf("%d %d\n", &d, &a);
    if (a >= d) 
      add.push_back(E(d, n+1, a));
    else
      sub.push_back(E(-a, n+1, d));
  }
  sort(add.begin(), add.end());
  sort(sub.begin(), sub.end());
  if (check(z, add, sub)) {
    printf("TAK\n");
    bool ns = true;
    print(add, ns);
    print(sub, ns);
    printf("\n");
  } else {
    printf("NIE\n");
  }
  return 0;
}