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

using namespace std;

const int MAXN = 100007;

pair <pair <long long int, long long int>, int> positive[MAXN], negative[MAXN];
int sequence[MAXN];
long long int suma, life;
int n, am_pos, am_neg, counter;

int main() {
  scanf("%d %lld", &n, &life);
  
  for (int i = 0; i < n; ++i) {
    long long int dmg, hp, x;
    scanf("%lld %lld", &dmg, &hp);
    x = hp - dmg;
    if (x >= 0) {
      positive[am_pos].first.first = dmg;
      positive[am_pos].first.second = x;
      positive[am_pos].second = i + 1;
      ++am_pos;
    }
    else {
      negative[am_neg].first.first = hp;
      negative[am_neg].first.second = -x;
      negative[am_neg].second = i + 1;
      ++am_neg;
    }
    suma += x;
  }
  
  suma += life;
  
  if (suma <= 0) {
    printf("NIE\n");
    return 0;
  }

  sort(positive,positive+am_pos);
  sort(negative,negative+am_neg);
  
  for (int i = 0; i < am_pos; ++i) {
    if (positive[i].first.first < life) {
      sequence[counter] = positive[i].second;
      ++counter;
      life += positive[i].first.second;
    }
    else {
      printf("NIE\n");
      return 0;
    }
  }

  life = suma;
  
  for (int i = 0; i < am_neg; ++i) {
    if (negative[i].first.first < life ) {
      sequence[counter] = negative[i].second;
      ++counter;
      life += negative[i].first.second;
    }
    else {
      printf("NIE\n");
      return 0;
    }
  }
  
  
  printf("TAK\n");
  for (int i = 0; i < am_pos; ++i) {
    printf("%d ", sequence[i]);
  }
  for (int i = counter - 1; i >= am_pos; --i) {
    printf("%d ", sequence[i]);
  }
  
  return 0;
}

//LONG LONGI!!!