import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class boh {
public static void main(String ... args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
String[] splitedLine = line.split(" ");
int n = Integer.parseInt(splitedLine[0]);
int z = Integer.parseInt(splitedLine[1]);
int[][] p = new int[n][3];
for(int i = 0; i < n; i++) {
line = reader.readLine();
splitedLine = line.split(" ");
p[i][0] = Integer.parseInt(splitedLine[0]); // demage
p[i][1] = Integer.parseInt(splitedLine[1]); // elixir
p[i][2] = i + 1; // index
}
// demage asc
Arrays.sort(p, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
int[] t1 = (int[]) o1;
int[] t2 = (int[]) o2;
if (t1[0] < t2[0]) {
return -1;
} else if (t1[0] > t2[0]) {
return 1;
} else if (t1[1] < t2[1]) {
return -1;
} else if (t1[1] > t2[1]) {
return 1;
} else if (t1[2] < t2[2]) {
return -1;
} else if (t1[2] > t2[2]) {
return 1;
} else {
return 0;
}
}
});
StringBuilder stringBuilder = new StringBuilder(n * 7);
boolean printed = false;
boolean dead = false;
for(int i = 0; i < n; i++) {
if (p[i][0] <= p[i][1]) {
if (printed) {
stringBuilder.append(' ').append(p[i][2]);
} else {
stringBuilder.append(p[i][2]);
printed = true;
}
z -= p[i][0];
if (z <= 0) {
dead = true;
break;
}
z += p[i][1];
}
}
// elixir asc
Arrays.sort(p, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
int[] t1 = (int[]) o1;
int[] t2 = (int[]) o2;
if (t1[1] < t2[1]) {
return -1;
} else if (t1[1] > t2[1]) {
return 1;
} else if (t1[0] < t2[0]) {
return -1;
} else if (t1[0] > t2[0]) {
return 1;
} else if (t1[2] < t2[2]) {
return -1;
} else if (t1[2] > t2[2]) {
return 1;
} else {
return 0;
}
}
});
for(int i = n - 1; i >= 0; i--) {
if (p[i][0] > p[i][1]) {
if (printed) {
stringBuilder.append(' ').append(p[i][2]);
} else {
stringBuilder.append(p[i][2]);
printed = true;
}
z -= p[i][0];
if (z <= 0) {
dead = true;
break;
}
z += p[i][1];
}
}
if (dead) {
System.out.println("NIE");
} else {
System.out.println("TAK");
System.out.println(stringBuilder.toString());
}
}
}
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; public class boh { public static void main(String ... args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); String[] splitedLine = line.split(" "); int n = Integer.parseInt(splitedLine[0]); int z = Integer.parseInt(splitedLine[1]); int[][] p = new int[n][3]; for(int i = 0; i < n; i++) { line = reader.readLine(); splitedLine = line.split(" "); p[i][0] = Integer.parseInt(splitedLine[0]); // demage p[i][1] = Integer.parseInt(splitedLine[1]); // elixir p[i][2] = i + 1; // index } // demage asc Arrays.sort(p, new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { int[] t1 = (int[]) o1; int[] t2 = (int[]) o2; if (t1[0] < t2[0]) { return -1; } else if (t1[0] > t2[0]) { return 1; } else if (t1[1] < t2[1]) { return -1; } else if (t1[1] > t2[1]) { return 1; } else if (t1[2] < t2[2]) { return -1; } else if (t1[2] > t2[2]) { return 1; } else { return 0; } } }); StringBuilder stringBuilder = new StringBuilder(n * 7); boolean printed = false; boolean dead = false; for(int i = 0; i < n; i++) { if (p[i][0] <= p[i][1]) { if (printed) { stringBuilder.append(' ').append(p[i][2]); } else { stringBuilder.append(p[i][2]); printed = true; } z -= p[i][0]; if (z <= 0) { dead = true; break; } z += p[i][1]; } } // elixir asc Arrays.sort(p, new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { int[] t1 = (int[]) o1; int[] t2 = (int[]) o2; if (t1[1] < t2[1]) { return -1; } else if (t1[1] > t2[1]) { return 1; } else if (t1[0] < t2[0]) { return -1; } else if (t1[0] > t2[0]) { return 1; } else if (t1[2] < t2[2]) { return -1; } else if (t1[2] > t2[2]) { return 1; } else { return 0; } } }); for(int i = n - 1; i >= 0; i--) { if (p[i][0] > p[i][1]) { if (printed) { stringBuilder.append(' ').append(p[i][2]); } else { stringBuilder.append(p[i][2]); printed = true; } z -= p[i][0]; if (z <= 0) { dead = true; break; } z += p[i][1]; } } if (dead) { System.out.println("NIE"); } else { System.out.println("TAK"); System.out.println(stringBuilder.toString()); } } } |
English