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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static java.lang.Integer.valueOf;

public class boh {
    private static class Monster {
        int d;
        int a;

        int number;

        private Monster(int number, int d, int a) {
            this.number = number;
            this.d = d;
            this.a = a;
        }

        @Override
        public String toString() {
            return "Monster{" +
                    "d=" + d +
                    ", a=" + a +
                    ", number=" + number +
                    '}';
        }
    }

    private static class PositiveMonsterComparator implements Comparator<Monster> {
        @Override
        public int compare(Monster o1, Monster o2) {
            return Integer.compare(o1.d, o2.d);
        }
    }
    private static class NegativeMonsterComparator implements Comparator<Monster> {
        @Override
        public int compare(Monster o1, Monster o2) {
            return Integer.compare(o2.a, o1.a);
        }
    }

    public static void main(String[] args) throws IOException {
        List<Monster> positiveMonsters = new ArrayList<Monster>();
        List<Monster> negativeMonsters = new ArrayList<Monster>();
        List<Monster> allMonsters = new ArrayList<Monster>();

        long z = readMonsters(positiveMonsters, negativeMonsters);

        Collections.sort(positiveMonsters, new PositiveMonsterComparator());
        Collections.sort(negativeMonsters, new NegativeMonsterComparator());

        allMonsters.addAll(positiveMonsters);
        allMonsters.addAll(negativeMonsters);

        StringBuilder result = new StringBuilder();
        for (Monster m : allMonsters) {
            if (z <= m.d) {
                System.out.println("NIE");
                return;
            }
            if (result.length() != 0) {
                result.append(" ");
            }
            result.append(m.number);
            z = z - m.d + m.a;
        }
        if (z > 0) {
            System.out.println("TAK");
            System.out.println(result.toString());
        } else {
            System.out.println("NIE");
        }
    }
    private static int readMonsters(List<Monster> positiveMonsters, List<Monster> negativeMonsters) throws IOException {
        InputStream input = System.in;
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String[] nzLine = reader.readLine().split(" ");
        int n = valueOf(nzLine[0]);
        int z = valueOf(nzLine[1]);
        for (int i = 0; i < n; i++) {
            String[] daLine = reader.readLine().split(" ");
            int d = valueOf(daLine[0]); // damage
            int a = valueOf(daLine[1]); // health
            Monster monster = new Monster(i+1, d, a);
            if (a > d) {
                positiveMonsters.add(monster);
            } else {
                negativeMonsters.add(monster);
            }
        }
        return z;
    }
}