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
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.Iterator;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author michalsk
 */
public class boh {

    public static void main(String[] args) throws IOException {
        doAll(System.in);
    }

    private static void doAll(InputStream stream) throws IOException {
        Reader.init(stream);
        long cases = Reader.nextInt();
        long initHp = Reader.nextInt();
        long currentHp = initHp;
        List<DmgHealCaseno> possitiveDeltas = new ArrayList<DmgHealCaseno>();
        List<DmgHealCaseno> negativeDeltas = new ArrayList<DmgHealCaseno>();
        List<Integer> indicesOfConsumed = new ArrayList<Integer>();

        int caseNo = 1;
        while (caseNo < cases + 1) {
            long dmg = Reader.nextInt();
            long heal = Reader.nextInt();
            long delta = heal - dmg;
            if (delta >= 0) {
                if (currentHp > dmg) {
                    currentHp += delta;
                    indicesOfConsumed.add(caseNo);
                } else {
                    possitiveDeltas.add(new DmgHealCaseno(dmg, heal, caseNo));
                }

            } else {
                negativeDeltas.add(new DmgHealCaseno(dmg, heal, caseNo));
            }
            caseNo++;
        }

        Collections.sort(possitiveDeltas, new Comparator<DmgHealCaseno>() {

            @Override
            public int compare(DmgHealCaseno o1, DmgHealCaseno o2) {
                return (int) (o2.dmg - o1.dmg);
            }
        });

        Iterator<DmgHealCaseno> iterator = possitiveDeltas.iterator();
        while (iterator.hasNext()) {
            DmgHealCaseno dmgDeltaCaseno = iterator.next();
            if (currentHp > dmgDeltaCaseno.dmg) {
                currentHp += dmgDeltaCaseno.getDelta();
                indicesOfConsumed.add(dmgDeltaCaseno.caseNo);
                iterator.remove();
            }
        }

        if (possitiveDeltas.size() > 0) {
            System.out.println("NIE");
            return;
        }

        Collections.sort(negativeDeltas, new Comparator<DmgHealCaseno>() {

            @Override
            public int compare(DmgHealCaseno o1, DmgHealCaseno o2) {
                return (int) (o2.heal - o1.heal);
            }
        });

        iterator = negativeDeltas.iterator();
        while (iterator.hasNext()) {
            DmgHealCaseno dmgDeltaCaseno = iterator.next();
            if (currentHp > dmgDeltaCaseno.dmg) {
                currentHp += dmgDeltaCaseno.getDelta();
                indicesOfConsumed.add(dmgDeltaCaseno.caseNo);
                iterator.remove();
            }
        }

        if (negativeDeltas.size() == 0) {
            System.out.println("TAK");
            for (Integer integer : indicesOfConsumed) {
                System.out.print(integer + " ");
            }
        } else {
            System.out.println("NIE");
        }
    }

    /** Class taken from http://www.cpe.ku.ac.th/~jim/java-io.html */
    static class Reader {

        static BufferedReader reader;
        static StringTokenizer tokenizer;

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() throws IOException {
            while (!tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        static long nextInt() throws IOException {
            return Integer.parseInt(next());
        }

    }

    static class DmgHealCaseno {

        final long dmg;
        final long heal;
        final int caseNo;

        public DmgHealCaseno(long dmg, long heal, int caseNo) {
            this.dmg = dmg;
            this.heal = heal;
            this.caseNo = caseNo;
        }

        public long getDelta() {
            return heal - dmg;
        }

        @Override
        public String toString() {
            return "DmgAndDelta [dmg=" + dmg + ", heal=" + heal + "]";
        }

    }

}