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
157
158
159
160
161
162
163
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class boh {

	public InputStream getInputStream() {
		return System.in;
		// return new FileInputStream(new File("boh.txt"));
	}

	public PrintStream getOutputStream() {
		return System.out;
	}

	long hitpoints = 0;
	LinkedList<Fight> fights = new LinkedList<boh.Fight>();

	LinkedList<Fight> fightsOrder = new LinkedList<boh.Fight>();

	class Fight {
		int damage;
		int heal;
		int index;

		public Fight(int damage, int heal, int index) {
			this.damage = damage;
			this.heal = heal;
			this.index = index;
		}

		public String toString() {
			return "Fight [damage=" + damage + ", heal=" + heal + ", index="
					+ index + "]";
		}

	}

	public void process() {
		BufferedReader reader = null;
		try {
			InputStream in = getInputStream();
			PrintStream out = getOutputStream();
			reader = new BufferedReader(new InputStreamReader(in));
			String line = reader.readLine();
			StringTokenizer tokenizer = new StringTokenizer(line);
			int fights = Integer.valueOf(tokenizer.nextToken());
			hitpoints = Integer.valueOf(tokenizer.nextToken());
			for (int i = 0; i < fights; i++) {
				readFight(reader, i + 1);
			}

			boolean fight = fight();
			if (fight) {
				out.println("TAK");
				for (Iterator<Fight> iterator = fightsOrder.iterator(); iterator
						.hasNext();) {
					Fight next = iterator.next();
					out.print(next.index);
					if (iterator.hasNext()) {
						out.print(" ");
					}
				}
				out.print("\n");
			} else {
				out.println("NIE");
			}

		} catch (Throwable t) {
			t.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}
	}

	private boolean fight() {
		sortByLowestDamageHighestHealFights();

		// firstly fight only when you gain more than loose
		int sizeBefore = fights.size();
		int sizeAfter = fights.size();
		do {
			sizeBefore = fights.size();
			for (Iterator<Fight> iterator = fights.iterator(); iterator
					.hasNext();) {
				Fight fight = iterator.next();
				if (hitpoints > fight.damage && fight.damage <= fight.heal) {
					hitpoints = (hitpoints - fight.damage) + fight.heal;
					iterator.remove();
					fightsOrder.add(fight);
				}
			}
			sizeAfter = fights.size();
		} while (sizeAfter != sizeBefore);

		sortHighestHealFights();

		do {
			sizeBefore = fights.size();
			for (Iterator<Fight> iterator = fights.iterator(); iterator
					.hasNext();) {
				Fight fight = iterator.next();
				if (hitpoints > fight.damage) {
					hitpoints = (hitpoints - fight.damage) + fight.heal;
					iterator.remove();
					fightsOrder.add(fight);
				}
			}
			sizeAfter = fights.size();
		} while (sizeAfter != sizeBefore);

		return fights.isEmpty();
	}

	private void readFight(BufferedReader reader, int index) throws IOException {
		String line = reader.readLine();
		StringTokenizer tokenizer = new StringTokenizer(line);
		int damage = Integer.valueOf(tokenizer.nextToken());
		int heal = Integer.valueOf(tokenizer.nextToken());
		fights.add(new Fight(damage, heal, index));
	}

	private void sortByLowestDamageHighestHealFights() {
		Collections.sort(fights, new Comparator<Fight>() {
			public int compare(Fight o1, Fight o2) {
				if (o1.damage == o2.damage) {
					if (o1.heal == o2.heal) {
						return 0;
					} else {
						return o2.heal - o1.heal;
					}
				}
				return o1.damage - o2.damage;
			}
		});
	}

	private void sortHighestHealFights() {
		Collections.sort(fights, new Comparator<Fight>() {
			public int compare(Fight o1, Fight o2) {
				return o2.heal - o1.heal;
			}
		});
	}

	public static void main(String[] args) {
		boh boh = new boh();
		boh.process();
	}
}