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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

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

	private void go() {
		Scanner in = new Scanner(System.in);
		
		
		int n = in.nextInt();
		int z = in.nextInt();
		Integer maxHealthBad = 0;
		ArrayList<Monster> monstersGood = new ArrayList<Monster>();
		ArrayList<Monster> monstersBad = new ArrayList<Monster>();
		
		
		for(int i=1;i<n+1;++i) {
			int health = in.nextInt();
			int potion = in.nextInt();
			if(potion>health) {
				monstersGood.add(new Monster(health, potion, i));
			} else {
				if(maxHealthBad < health) {
					maxHealthBad = health;
				}
				monstersBad.add(new Monster(health, potion, i));
			}
		}
		Knight k = new Knight(z, maxHealthBad);
		Collections.sort(monstersGood);
		Collections.reverse(monstersGood);
		
		Collections.sort(monstersBad, new Comparator<Monster>() {

			@Override
			public int compare(Monster o1, Monster o2) {
				if(o1.diff < o2.diff) return 1;
				if(o1.diff > o2.diff) return -1;					
				return 0;
			}
		});
		
		
		
		while(k.killGoodMonster(monstersGood)) {
			
		}

		while(k.killBadMonster(monstersBad)) {
			
		}		
		
		if(monstersGood.isEmpty() && monstersBad.isEmpty()) {
			System.out.println("TAK");
			System.out.println(k.getKilled());
		} else {
			System.out.println("NIE");
		}
		
//		System.out.println(monstersGood);
//		System.out.println(monstersBad);
//		
	}
	
	
	class Knight {
		StringBuilder result;
		
		long health;
		int maxHealthBad;

		public Knight(int health, int maxHealthBad) {
			super();
			this.health = health;
			this.maxHealthBad = maxHealthBad;
			result = new StringBuilder();
		}
		
		public boolean killGoodMonster(ArrayList<Monster> monsters) {
			for(int i=0;i<monsters.size();++i) {
				if(monsters.get(i).health < health) {					
					health -= monsters.get(i).health;
					health += monsters.get(i).potion;
//					System.out.println("Kill: " + monsters.get(i) +"; healt left: " + health);
					result.append(monsters.get(i).position+" ");
					monsters.remove(i);
					return true;
				}				
			}
			return false;
		}
		
		public boolean killBadMonster(ArrayList<Monster> monsters) {
			if(health < maxHealthBad) {
				return false;
			}
			
			for(int i=0;i<monsters.size();) {
				while(i < monsters.size()-1 && health - monsters.get(i).health + monsters.get(i).potion < maxHealthBad) {
					i++;
				}
								
				if(monsters.get(i).health == maxHealthBad) {
					health -= monsters.get(i).health;
					health += monsters.get(i).potion;
					result.append(monsters.get(i).position+" ");
//					System.out.println("Kill: " + monsters.get(i) +"; healt left: " + health + "; max = " + maxHealthBad);
					monsters.remove(i);													
					maxHealthBad = findMaxHealth(monsters);
//					System.out.println(maxHealthBad);
					
					return true;
				} else {
					health -= monsters.get(i).health;
					health += monsters.get(i).potion;
					result.append(monsters.get(i).position+" ");
//					System.out.println("Kill: " + monsters.get(i) +"; healt left: " + health + "; max = " + maxHealthBad);
					monsters.remove(i);	
					return true;
				}
					
					
			}				
			return false;
		}
		
		private Integer findMaxHealth(ArrayList<Monster> monsters) {
			Integer max = 0;
			for(int i=0;i<monsters.size();++i) {
				if(monsters.get(i).health > max) {
					max = monsters.get(i).health;
				}
			}
			return max;
		}
				
		public String getKilled() {
			return result.toString().trim();
		}
		
	}
		
	class Monster implements Comparable<Monster>{
		int health;
		int potion;
		public int getHealth() {
			return health;
		}
		public int getPotion() {
			return potion;
		}
		public int getPosition() {
			return position;
		}
		public int getDiff() {
			return diff;
		}
		int position;
		int diff;
		public Monster(int health, int potion, int position) {
			super();
			this.health = health;
			this.potion = potion;
			this.position = position;
			diff = potion - health;
		}
		@Override
		public int compareTo(Monster o) {
			if(this.health > o.health)
				return -1;
			else if(this.health < o.health)
				return 1;
			else return 0;
		}
		@Override
		public String toString() {
			return "Monster [health=" + health + ", potion=" + potion
					+ ", position=" + position + ", diff=" + diff + "]";
		}
						
		
		
	}
	
}