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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class boh {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        long h = sc.nextInt(), sum = h;
        StringBuilder result = new StringBuilder(2 * n);

        ArrayList<Enemy> friends = new ArrayList<Enemy>(n);
        ArrayList<Enemy> enemies = new ArrayList<Enemy>(n);

        for (int i = 1; i <= n; ++i) {
            Enemy e = new Enemy(sc.nextInt(), sc.nextInt(), i);
            if (e.delta >= 0) {
                friends.add(e);
            } else {
                enemies.add(e);
            }
            sum += e.delta;
        }

        if (sum <= 0) {
            System.out.println("NIE"); // No way you can defeat them..
            return;
        }

        friends.sort(new Comparator<Enemy>() {
            @Override
            public int compare(Enemy o1, Enemy o2) {
                return o1.damage - o2.damage;
            }
        });

        for (Enemy friend : friends) {
            if (h - friend.damage > 0) {
                result.append(friend.idx).append(' ');
                h += friend.delta;
            } else {
                System.out.println("NIE"); // You can't pass this one :(
                return;
            }
        }

        enemies.sort(new Comparator<Enemy>() {
            @Override
            public int compare(Enemy o1, Enemy o2) {
                if (o2.health == o1.health) {
                    return o2.damage - o1.damage;
                }
                return o2.health - o1.health;
            }
        });

        for (Enemy enemy : enemies) {
            if (h - enemy.damage > 0) {
                result.append(enemy.idx).append(' ');
                h += enemy.delta;
            } else {
                System.out.println("NIE"); // You can't pass this one :(
                return;
            }
        }

        System.out.println("TAK");
        System.out.println(result.delete(result.length() - 1, result.length()));
    }
}

class Enemy {
    int damage;
    int health;
    int delta;
    int idx;

    public Enemy(int d, int h, int i) {
        damage = d;
        health = h;
        delta = h - d;
        idx = i;
    }
}