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
/*
 * boh.cpp
 *
 *  Created on: May 13, 2014
 *      Author: hanczar
 */
#include <vector>
#include <stdio.h>
#include <algorithm>

std::vector< std::pair<int,int> > monsters;
std::vector< int > goodmonsters;
std::vector< int > badmonsters;
typedef long long int lli;

bool play (lli live) {

  for (int i=0;i<goodmonsters.size();i++) {
      live -= monsters[goodmonsters[i]].first;
      if(live<=0) {
        return false;
      }
      live += monsters[goodmonsters[i]].second;
      //printf("after meeting good monster %d , %lld,  %d %d \n",i,live,monsters[goodmonsters[i]].first,monsters[goodmonsters[i]].second);
  }

  for (int i=badmonsters.size()-1;0<=i;--i) {
      live -= monsters[badmonsters[i]].first;
      if(live<=0) {
        return false;
      }
      live += monsters[badmonsters[i]].second;
      //printf("after meeting bad monster %d , %lld,  %d %d \n",i,live,monsters[badmonsters[i]].first,monsters[badmonsters[i]].second);
  }

  return true;
}

bool monstersort(int m1,int m2) {
  return (monsters[m1].first < monsters[m2].first);
}

bool eliksirsort(int m1,int m2) {
    if (monsters[m1].second == monsters[m2].second) {
      return (monsters[m1].first < monsters[m2].first);
    } else {
      return (monsters[m1].second < monsters[m2].second);
    }
}

int main(int argc,char *argv[]) {
  int n,live;
  scanf("%d %d",&n,&live);

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

    monsters.push_back(std::pair<int,int>(hit,potion));
    if (potion >= hit) {
      goodmonsters.push_back(i);
    } else {
      badmonsters.push_back(i);
    }
  }

  std::sort(goodmonsters.begin(),goodmonsters.end(), monstersort);

  std::sort(badmonsters.begin(),badmonsters.end(), eliksirsort);

  /*
  for (int i=0;i<goodmonsters.size();i++) {
    printf("%d: %d %d \n",i,monsters[goodmonsters[i]].first,monsters[goodmonsters[i]].second);
  }


  for (int i=0;i<badmonsters.size();i++) {
    printf("%d: %d %d \n",i,monsters[badmonsters[i]].first,monsters[badmonsters[i]].second);
  }
  */

  bool result = play(live);
  if (!result) {
    printf("NIE\n");
  } else {
    printf("TAK\n");
    for (int i=0;i<goodmonsters.size();i++) {
      printf("%d ",goodmonsters[i]+1);
    }
    for (int i=badmonsters.size()-1;0<=i;--i) {
      printf("%d ",badmonsters[i]+1);
    }
  }

  return 0;
}