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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
/**
 *
 * @author Marcin Lewandowski
 */
public class boh {

    public static void main(String[] args) throws IOException {
        int n = readInt(System.in);
        int z = readInt(System.in);
        LinkedList<Moster> positve = new LinkedList<Moster>();
        LinkedList<Moster> negative = new LinkedList<Moster>();
        for (int i = 0; i < n; i++) {
            int damage = readInt(System.in);
            int health = readInt(System.in);
            Moster moster = new Moster(damage, health, i + 1);
            if (health > damage) {
                positve.add(moster);
            } else {
                negative.add(moster);
            }
        }
        //check positive monsters
        Collections.sort(positve, new Comparator<Moster>() {
            @Override
            public int compare(Moster o1, Moster o2) {
                return o1.damage - o2.damage;
            }
        });
        for (Moster m : positve) {
            int diff = z - m.damage;
            if (diff < 1) {
                System.out.println("NIE");
                return;
            } else {
                z = diff + m.health;
            }
        }
        //check negative monsters
        Comparator[] comparators = new Comparator[3];
        comparators[1]= new Comparator<Moster>() {
            @Override
            public int compare(Moster o1, Moster o2) {
                if (o1.damage != o2.damage) {
                    return o2.damage - o1.damage;
                } else {
                    return o2.health - o1.health;
                }
            }
        };
        comparators[0]= new Comparator<Moster>() {
            @Override
            public int compare(Moster o1, Moster o2) {
                    return o2.health - o1.health;
            }
        };
        comparators[2]= new Comparator<Moster>() {
            @Override
            public int compare(Moster o1, Moster o2){
                    return (o2.health-o2.damage) - (o1.health- o1.damage);

            }
        };
        boolean isSuccess =true;
        for(Comparator<Moster> comparator : comparators){
            Collections.sort(negative,comparator);
            isSuccess =true;
            int health = z;
            for (Moster m : negative) {
                int diff = health - m.damage;
                if (diff < 1) {
                    isSuccess=false;
                    break;
                } else {
                    health = diff + m.health;
                }
            }
            if(isSuccess){
                break;
            }
        }
        if(!isSuccess){
            System.out.println("NIE");
        }
        StringBuilder builder = new StringBuilder(n);
        for (Moster m : positve) {
            builder.append(m.index);
            builder.append(" ");
        }
        for (Moster m : negative) {
            builder.append(m.index);
            builder.append(" ");
        }
        String order = builder.toString().trim();
        System.out.println("TAK");
        System.out.println(order);
    }

    private static int readInt(InputStream in) throws IOException {
        int result = 0;
        boolean isDigit = false;
        int c = 0;
        while ((c = in.read()) != -1) {
            if (c >= '0' && c <= '9') {
                isDigit = true;
                result = result * 10 + c - '0';
            } else if (isDigit) {
                break;
            }
        }
        return result;
    }

    private static class Moster {
        int damage;
        int health;
        int index;
        public Moster(int damage, int health, int index) {
            this.damage = damage;
            this.health = health;
            this.index = index;
        }
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Moster other = (Moster) obj;
            if (this.damage != other.damage) {
                return false;
            }
            if (this.health != other.health) {
                return false;
            }
            if (this.index != other.index) {
                return false;
            }
            return true;
        }
        @Override
        public int hashCode() {
            int hash = 3;
            hash = 97 * hash + this.damage;
            hash = 97 * hash + this.health;
            hash = 97 * hash + this.index;
            return hash;
        }
    }
}