import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; public class boh { private static InputStream in; public static void main(String[] args) throws IOException { in = new BufferedInputStream(System.in); int n = nextInt(); int z = nextInt(); final int[] healthPoints = new int[n]; final int[] injuryPoints = new int[n]; final int[] battleResults = new int[n]; Integer[] indices = new Integer[n]; long tempSum = z; int temp; int smallestInjury = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int d = nextInt(); // injury int a = nextInt(); // health if (d < smallestInjury) { smallestInjury = d; } temp = a - d; healthPoints[i] = a; injuryPoints[i] = d; battleResults[i] = temp; indices[i] = i; tempSum += temp; } // if the all battles sum goes below zero, he will definitely fail: if (tempSum <= 0) { fail(); return; } // if first smallest injury kills, then fail: if (smallestInjury >= z) { fail(); return; } // sort by injuries: Arrays.sort(indices, new Comparator<Integer>() { @Override public int compare(Integer idx1, Integer idx2) { return injuryPoints[idx1] - injuryPoints[idx2]; } }); long health = z; boolean dead = false; int[] battle = new int[n]; mainLoop : for (int i = 0; i < indices.length; i++) { int largestHealing = Integer.MIN_VALUE; int nextStepIdx = -1; int battleUsageMarker = 0; for (int j = 0; j < indices.length; j++) { int idx = indices[j]; // skip if battle already used: if (idx == -1) { continue; } // if too far, then stop: if (injuryPoints[idx] >= health) { break; } int healing = battleResults[idx]; boolean ok = false; if (i == n - 1) { ok = true; } else { long healthInNextStep = health + healing; for (int k = 0; k < indices.length; k++) { if (indices[k] != -1 && k != j) { if (injuryPoints[indices[k]] < healthInNextStep) { ok = true; break; } } } } if (!ok) { continue; } if (healing > largestHealing) { largestHealing = healing; nextStepIdx = idx; battleUsageMarker = j; } } if (nextStepIdx == -1) { dead = true; break mainLoop; } health -= injuryPoints[nextStepIdx]; if (health <= 0) { dead = true; break mainLoop; } health += healthPoints[nextStepIdx]; battle[i] = nextStepIdx; indices[battleUsageMarker] = -1; // marked used battle step } System.out.println(dead ? "NIE" : "TAK"); if (!dead) { int len = indices.length - 1; for (int i = 0; i < len; i++) { System.out.print(battle[i] + 1); System.out.print(" "); } System.out.print(battle[len] + 1); } in.close(); } private static void fail() { System.out.println("NIE"); return; } private static int nextInt() throws IOException { int ret = 0; boolean dig = false; for (int c = 0; (c = in.read()) != -1; ) { if (c >= 48 && c <= 57) { dig = true; ret = ret * 10 + c - 48; } else if (dig) break; } return ret; } }
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 | import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; public class boh { private static InputStream in; public static void main(String[] args) throws IOException { in = new BufferedInputStream(System.in); int n = nextInt(); int z = nextInt(); final int[] healthPoints = new int[n]; final int[] injuryPoints = new int[n]; final int[] battleResults = new int[n]; Integer[] indices = new Integer[n]; long tempSum = z; int temp; int smallestInjury = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int d = nextInt(); // injury int a = nextInt(); // health if (d < smallestInjury) { smallestInjury = d; } temp = a - d; healthPoints[i] = a; injuryPoints[i] = d; battleResults[i] = temp; indices[i] = i; tempSum += temp; } // if the all battles sum goes below zero, he will definitely fail: if (tempSum <= 0) { fail(); return; } // if first smallest injury kills, then fail: if (smallestInjury >= z) { fail(); return; } // sort by injuries: Arrays.sort(indices, new Comparator<Integer>() { @Override public int compare(Integer idx1, Integer idx2) { return injuryPoints[idx1] - injuryPoints[idx2]; } }); long health = z; boolean dead = false; int[] battle = new int[n]; mainLoop : for (int i = 0; i < indices.length; i++) { int largestHealing = Integer.MIN_VALUE; int nextStepIdx = -1; int battleUsageMarker = 0; for (int j = 0; j < indices.length; j++) { int idx = indices[j]; // skip if battle already used: if (idx == -1) { continue; } // if too far, then stop: if (injuryPoints[idx] >= health) { break; } int healing = battleResults[idx]; boolean ok = false; if (i == n - 1) { ok = true; } else { long healthInNextStep = health + healing; for (int k = 0; k < indices.length; k++) { if (indices[k] != -1 && k != j) { if (injuryPoints[indices[k]] < healthInNextStep) { ok = true; break; } } } } if (!ok) { continue; } if (healing > largestHealing) { largestHealing = healing; nextStepIdx = idx; battleUsageMarker = j; } } if (nextStepIdx == -1) { dead = true; break mainLoop; } health -= injuryPoints[nextStepIdx]; if (health <= 0) { dead = true; break mainLoop; } health += healthPoints[nextStepIdx]; battle[i] = nextStepIdx; indices[battleUsageMarker] = -1; // marked used battle step } System.out.println(dead ? "NIE" : "TAK"); if (!dead) { int len = indices.length - 1; for (int i = 0; i < len; i++) { System.out.print(battle[i] + 1); System.out.print(" "); } System.out.print(battle[len] + 1); } in.close(); } private static void fail() { System.out.println("NIE"); return; } private static int nextInt() throws IOException { int ret = 0; boolean dig = false; for (int c = 0; (c = in.read()) != -1; ) { if (c >= 48 && c <= 57) { dig = true; ret = ret * 10 + c - 48; } else if (dig) break; } return ret; } } |