import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class boh { public static void main(String[] args) throws Exception { new boh().solve(new BufferedReader(new InputStreamReader(System.in)), new BufferedWriter(new OutputStreamWriter(System.out))); } public void solve(BufferedReader reader, BufferedWriter writer) throws Exception { StringTokenizer st = new StringTokenizer(reader.readLine()); StringBuilder monsterKillingOrder = new StringBuilder(); int n = Integer.parseInt(st.nextToken()); int heroLifePoints = Integer.parseInt(st.nextToken()); List<Monster> monsters = new ArrayList<Monster>(n); for (int i = 1; i <= n; i++) { st = new StringTokenizer(reader.readLine()); int d = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); monsters.add(new Monster(i, d, a - d)); } Collections.sort(monsters); boolean success = true; for (Monster m : monsters) { if (heroLifePoints > m.cost) { monsterKillingOrder.append(m.idx).append(" "); heroLifePoints += m.life; } else { success = false; break; } } if (success) { writer.append("TAK"); writer.newLine(); writer.append(monsterKillingOrder); } else { writer.append("NIE"); } writer.newLine(); writer.flush(); } private static class Monster implements Comparable<Monster> { int idx, cost, life; public Monster(int idx, int cost, int life) { this.idx = idx; this.cost = cost; this.life = life; } public boolean isBad() { return life < 0; } @Override public int compareTo(Monster that) { if (this.isBad() && !that.isBad()) { return 1; } else if (!this.isBad() && that.isBad()) { return -1; } else if (this.isBad() && that.isBad()) { return this.cost > that.cost ? -1 : that.cost > this.cost ? 1 : 0; } else { return this.cost < that.cost ? -1 : that.cost < this.cost ? 1 : 0; } } } }
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 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class boh { public static void main(String[] args) throws Exception { new boh().solve(new BufferedReader(new InputStreamReader(System.in)), new BufferedWriter(new OutputStreamWriter(System.out))); } public void solve(BufferedReader reader, BufferedWriter writer) throws Exception { StringTokenizer st = new StringTokenizer(reader.readLine()); StringBuilder monsterKillingOrder = new StringBuilder(); int n = Integer.parseInt(st.nextToken()); int heroLifePoints = Integer.parseInt(st.nextToken()); List<Monster> monsters = new ArrayList<Monster>(n); for (int i = 1; i <= n; i++) { st = new StringTokenizer(reader.readLine()); int d = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); monsters.add(new Monster(i, d, a - d)); } Collections.sort(monsters); boolean success = true; for (Monster m : monsters) { if (heroLifePoints > m.cost) { monsterKillingOrder.append(m.idx).append(" "); heroLifePoints += m.life; } else { success = false; break; } } if (success) { writer.append("TAK"); writer.newLine(); writer.append(monsterKillingOrder); } else { writer.append("NIE"); } writer.newLine(); writer.flush(); } private static class Monster implements Comparable<Monster> { int idx, cost, life; public Monster(int idx, int cost, int life) { this.idx = idx; this.cost = cost; this.life = life; } public boolean isBad() { return life < 0; } @Override public int compareTo(Monster that) { if (this.isBad() && !that.isBad()) { return 1; } else if (!this.isBad() && that.isBad()) { return -1; } else if (this.isBad() && that.isBad()) { return this.cost > that.cost ? -1 : that.cost > this.cost ? 1 : 0; } else { return this.cost < that.cost ? -1 : that.cost < this.cost ? 1 : 0; } } } } |