import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; public class boh { public static void main(String[] args) throws IOException { SystemIO io = new SystemIO(); new boh().resolve(io); io.out.flush(); } void resolve(IO io) throws IOException { int mcnt = io.readInt(); Monster[] m = new Monster[mcnt]; long life = io.readInt(); for (int mi = 0; mi < mcnt; mi++) { m[mi] = new Monster(mi + 1, io.readInt(), io.readInt()); } Arrays.sort(m); for (int mi = 0; mi < mcnt; mi++) { life += m[mi].getLeft(); if (life < 0) { io.writeString("NIE\n"); return; } } io.writeString("TAK\n"); for (int mi = 0; mi < mcnt; mi++) { io.writeString(m[mi].idx + " "); } } class Monster implements Comparable<Monster> { int idx; int dmg; int life; int diff; Monster(int idx, int dmg, int life) { this.idx =idx; this.dmg = dmg; this.life = life; diff = life - dmg; } int getLeft() { return diff; } @Override public int compareTo(Monster o) { int d = o.diff - diff; if (d == 0) { d = dmg - o.dmg; if (d == 0) { d = o.life - life; } } return d; } } /* ------------------------------- In / Out ------------------------------------------- */ void dbg(Object o) { if (false) System.err.println(o); } interface IO { int readInt() throws IOException; long readLong() throws IOException; String readLine() throws IOException; void writeString(String str) throws IOException; } static class SystemIO implements IO { StringBuilder b = new StringBuilder(); BufferedInputStream in = new BufferedInputStream(System.in); PrintStream out = new PrintStream(new BufferedOutputStream(System.out)); int skipWhitespace() throws IOException { int r = -1; do { r = in.read(); if (r < 0) break; } while (Character.isWhitespace(r)); return r; } public void readDigit() throws IOException { b.setLength(0); int r = skipWhitespace(); while (Character.isDigit(r)) { b.append((char) r); r = in.read(); } } @Override public int readInt() throws IOException { readDigit(); return Integer.parseInt(b.toString()); } @Override public long readLong() throws IOException { readDigit(); return Long.parseLong(b.toString()); } @Override public String readLine() throws IOException { b.setLength(0); int r = skipWhitespace(); while (r != 10) { if (r == -1) break; b.append((char) r); r = in.read(); } return b.toString(); } @Override public void writeString(String str) throws IOException { out.write(str.getBytes()); } } }
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 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; public class boh { public static void main(String[] args) throws IOException { SystemIO io = new SystemIO(); new boh().resolve(io); io.out.flush(); } void resolve(IO io) throws IOException { int mcnt = io.readInt(); Monster[] m = new Monster[mcnt]; long life = io.readInt(); for (int mi = 0; mi < mcnt; mi++) { m[mi] = new Monster(mi + 1, io.readInt(), io.readInt()); } Arrays.sort(m); for (int mi = 0; mi < mcnt; mi++) { life += m[mi].getLeft(); if (life < 0) { io.writeString("NIE\n"); return; } } io.writeString("TAK\n"); for (int mi = 0; mi < mcnt; mi++) { io.writeString(m[mi].idx + " "); } } class Monster implements Comparable<Monster> { int idx; int dmg; int life; int diff; Monster(int idx, int dmg, int life) { this.idx =idx; this.dmg = dmg; this.life = life; diff = life - dmg; } int getLeft() { return diff; } @Override public int compareTo(Monster o) { int d = o.diff - diff; if (d == 0) { d = dmg - o.dmg; if (d == 0) { d = o.life - life; } } return d; } } /* ------------------------------- In / Out ------------------------------------------- */ void dbg(Object o) { if (false) System.err.println(o); } interface IO { int readInt() throws IOException; long readLong() throws IOException; String readLine() throws IOException; void writeString(String str) throws IOException; } static class SystemIO implements IO { StringBuilder b = new StringBuilder(); BufferedInputStream in = new BufferedInputStream(System.in); PrintStream out = new PrintStream(new BufferedOutputStream(System.out)); int skipWhitespace() throws IOException { int r = -1; do { r = in.read(); if (r < 0) break; } while (Character.isWhitespace(r)); return r; } public void readDigit() throws IOException { b.setLength(0); int r = skipWhitespace(); while (Character.isDigit(r)) { b.append((char) r); r = in.read(); } } @Override public int readInt() throws IOException { readDigit(); return Integer.parseInt(b.toString()); } @Override public long readLong() throws IOException { readDigit(); return Long.parseLong(b.toString()); } @Override public String readLine() throws IOException { b.setLength(0); int r = skipWhitespace(); while (r != 10) { if (r == -1) break; b.append((char) r); r = in.read(); } return b.toString(); } @Override public void writeString(String str) throws IOException { out.write(str.getBytes()); } } } |