import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class boh {
public static int n;
public static long z;
public static List<Monster> goodMonsters;
public static List<Monster> badMonsters;
public static class Tokenizer {
public static int[] stoints(String line) {
String[] ss = line.split(" ");
int[] ret = new int[ss.length];
for (int i = 0; i<ss.length; i++) {
ret[i] = Integer.valueOf(ss[i]);
}
return ret;
}
public static int stoint(String s) {
return stoints(s)[0];
}
}
public static class Monster {
public int num;
public int d;
public int a;
public Monster(int num, int d, int a) {
this.num = num;
this.d = d;
this.a = a;
}
public boolean isAdding() {
return a-d > 0;
}
public int value() {
return a-d;
}
@Override
public String toString() {
return "-"+d+"+"+a;
}
}
public static void io() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] nz = Tokenizer.stoints(br.readLine());
n = nz[0];
z = nz[1];
goodMonsters = new ArrayList<Monster>();
badMonsters = new ArrayList<Monster>();
for (int i = 0; i<n; i++) {
//TODO: za kazdym razem tworzymy nowa tablice... to moze byc wolne
int[] da = Tokenizer.stoints(br.readLine());
Monster m = new Monster(i+1, da[0], da[1]);
if (m.isAdding()) {
goodMonsters.add(m);
} else {
badMonsters.add(m);
}
}
}
public static boolean czyDaSie() {
Collections.sort(goodMonsters, new Comparator<Monster>() {
@Override public int compare(Monster m, Monster mm) {
return m.d - mm.d;
}
});
Collections.sort(badMonsters, new Comparator<Monster>() {
@Override public int compare(Monster m, Monster mm) {
return mm.d - m.d;
}
});
// System.out.println(goodMonsters);
for (Monster m : goodMonsters) {
// System.out.print("z:");
if (z <= m.d) {
return false;
}
// System.out.println(""+z+" M: "+m);
z += m.value();
}
for (Monster m : badMonsters) {
// System.out.print("z:");
if (z <= m.d) {
return false;
}
// System.out.println(""+z+" M: "+m);
z += m.value();
}
return true;
}
public static void main(String[] args) throws IOException {
io();
if (czyDaSie()) {
System.out.println("TAK");
StringBuffer sb = new StringBuffer();
for (Monster m : goodMonsters) {
sb.append(m.num).append(' ');
}
System.out.print(sb);
sb = new StringBuffer();
for (Monster m : badMonsters) {
sb.append(m.num).append(' ');
}
System.out.println(sb);
} else {
System.out.println("NIE");
}
}
}
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class boh { public static int n; public static long z; public static List<Monster> goodMonsters; public static List<Monster> badMonsters; public static class Tokenizer { public static int[] stoints(String line) { String[] ss = line.split(" "); int[] ret = new int[ss.length]; for (int i = 0; i<ss.length; i++) { ret[i] = Integer.valueOf(ss[i]); } return ret; } public static int stoint(String s) { return stoints(s)[0]; } } public static class Monster { public int num; public int d; public int a; public Monster(int num, int d, int a) { this.num = num; this.d = d; this.a = a; } public boolean isAdding() { return a-d > 0; } public int value() { return a-d; } @Override public String toString() { return "-"+d+"+"+a; } } public static void io() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int[] nz = Tokenizer.stoints(br.readLine()); n = nz[0]; z = nz[1]; goodMonsters = new ArrayList<Monster>(); badMonsters = new ArrayList<Monster>(); for (int i = 0; i<n; i++) { //TODO: za kazdym razem tworzymy nowa tablice... to moze byc wolne int[] da = Tokenizer.stoints(br.readLine()); Monster m = new Monster(i+1, da[0], da[1]); if (m.isAdding()) { goodMonsters.add(m); } else { badMonsters.add(m); } } } public static boolean czyDaSie() { Collections.sort(goodMonsters, new Comparator<Monster>() { @Override public int compare(Monster m, Monster mm) { return m.d - mm.d; } }); Collections.sort(badMonsters, new Comparator<Monster>() { @Override public int compare(Monster m, Monster mm) { return mm.d - m.d; } }); // System.out.println(goodMonsters); for (Monster m : goodMonsters) { // System.out.print("z:"); if (z <= m.d) { return false; } // System.out.println(""+z+" M: "+m); z += m.value(); } for (Monster m : badMonsters) { // System.out.print("z:"); if (z <= m.d) { return false; } // System.out.println(""+z+" M: "+m); z += m.value(); } return true; } public static void main(String[] args) throws IOException { io(); if (czyDaSie()) { System.out.println("TAK"); StringBuffer sb = new StringBuffer(); for (Monster m : goodMonsters) { sb.append(m.num).append(' '); } System.out.print(sb); sb = new StringBuffer(); for (Monster m : badMonsters) { sb.append(m.num).append(' '); } System.out.println(sb); } else { System.out.println("NIE"); } } } |
English