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
#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int n, z;
  cin >> n >> z;
  vector< tuple<int,int,int> > good;
  vector< tuple<int,int,int> > bad;
  for (int i = 0; i < n; i++) {
    int d, a;
    cin >> d >> a;
    if (d <= a)
      good.push_back(make_tuple(d, a, i+1));
    else
      bad.push_back(make_tuple(a, d, i+1));
  }
  long long hp = z;
  sort(good.begin(), good.end());
  for (int i = 0; i < good.size(); i++) {
    int d = get<0>(good[i]);
    int a = get<1>(good[i]);
    if (hp <= d)
      goto no;
    hp += a - d;
  }
  sort(bad.begin(), bad.end(), greater< tuple<int,int,int> >());
  for (int i = 0; i < bad.size(); i++) {
    int a = get<0>(bad[i]);
    int d = get<1>(bad[i]);
    if (hp <= d)
      goto no;
    hp += a - d;
  }
  cout << "TAK" << endl;
  for (int i = 0; i < good.size(); i++)
    cout << get<2>(good[i]) << " ";
  for (int i = 0; i < bad.size(); i++)
    cout << get<2>(bad[i]) << " ";
  cout << endl;
  goto out;
 no:
  cout << "NIE" << endl;
 out:
  return 0;
}