import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; class Potwor { int pos; Long spadek; Long moc; public Potwor(int pos, Long spadek, Long moc) { this.pos = pos; this.spadek = spadek; this.moc = moc; } public int getPos() { return pos; } public Long getSpadek() { return spadek; } public Long getMoc() { return moc; } @Override public String toString() { return "Potwor [pos=" + pos + ", spadek=" + spadek + ", moc=" + moc + "]"; } } public class boh { private PrintWriter pw = null; private BufferedReader br = null; boh(boolean skip) throws FileNotFoundException { this.pw = new PrintWriter(System.out); this.br = new BufferedReader(new InputStreamReader(System.in), 1 << 16); if (skip) return; } void doit() throws IllegalArgumentException, IOException { StringTokenizer st = new StringTokenizer(br.readLine()); final long N = Long.parseLong(st.nextToken()); long Z = Long.parseLong(st.nextToken()); List<Potwor> potwory = new ArrayList<Potwor>(); for (int i = 0; i < N; i++) { st = new StringTokenizer(br.readLine()); final long d = Long.parseLong(st.nextToken()); final long a = Long.parseLong(st.nextToken()); Potwor p = new Potwor(i + 1, d, a); potwory.add(p); //System.out.println(p); } Comparator<Potwor> comparator = new Comparator<Potwor>() { public int compare(Potwor object1, Potwor object2) { // moc - spadek Long v1 = object1.getMoc() - object1.getSpadek(); Long v2 = object2.getMoc() - object2.getSpadek(); // jesli v1 i v2 sa < 0 to odwroc porzadek if (v1 < 0 && v2 < 0) { if (v1 == v2) { return object2.getMoc().compareTo(object1.getMoc()); } return v1.compareTo(v2); } return v2.compareTo(v1); } }; Collections.sort(potwory, comparator); List<Integer> kolejnosc = new ArrayList<Integer>(); for (Potwor p : potwory) { //System.out.println(p + " Z="); Z -= p.getSpadek(); if (Z <= 0) { pw.println("NIE"); pw.flush(); return; } Z += p.getMoc(); //System.out.println(" " + Z); kolejnosc.add(p.getPos()); } pw.println("TAK"); for (int ii = 0; ii < kolejnosc.size(); ii++) { pw.print(kolejnosc.get(ii)); if (ii != kolejnosc.size() - 1) { pw.print(" "); } } pw.println(); pw.flush(); } public static void main(String[] args) throws IllegalArgumentException, IOException { new boh(true).doit(); } }
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 | import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; class Potwor { int pos; Long spadek; Long moc; public Potwor(int pos, Long spadek, Long moc) { this.pos = pos; this.spadek = spadek; this.moc = moc; } public int getPos() { return pos; } public Long getSpadek() { return spadek; } public Long getMoc() { return moc; } @Override public String toString() { return "Potwor [pos=" + pos + ", spadek=" + spadek + ", moc=" + moc + "]"; } } public class boh { private PrintWriter pw = null; private BufferedReader br = null; boh(boolean skip) throws FileNotFoundException { this.pw = new PrintWriter(System.out); this.br = new BufferedReader(new InputStreamReader(System.in), 1 << 16); if (skip) return; } void doit() throws IllegalArgumentException, IOException { StringTokenizer st = new StringTokenizer(br.readLine()); final long N = Long.parseLong(st.nextToken()); long Z = Long.parseLong(st.nextToken()); List<Potwor> potwory = new ArrayList<Potwor>(); for (int i = 0; i < N; i++) { st = new StringTokenizer(br.readLine()); final long d = Long.parseLong(st.nextToken()); final long a = Long.parseLong(st.nextToken()); Potwor p = new Potwor(i + 1, d, a); potwory.add(p); //System.out.println(p); } Comparator<Potwor> comparator = new Comparator<Potwor>() { public int compare(Potwor object1, Potwor object2) { // moc - spadek Long v1 = object1.getMoc() - object1.getSpadek(); Long v2 = object2.getMoc() - object2.getSpadek(); // jesli v1 i v2 sa < 0 to odwroc porzadek if (v1 < 0 && v2 < 0) { if (v1 == v2) { return object2.getMoc().compareTo(object1.getMoc()); } return v1.compareTo(v2); } return v2.compareTo(v1); } }; Collections.sort(potwory, comparator); List<Integer> kolejnosc = new ArrayList<Integer>(); for (Potwor p : potwory) { //System.out.println(p + " Z="); Z -= p.getSpadek(); if (Z <= 0) { pw.println("NIE"); pw.flush(); return; } Z += p.getMoc(); //System.out.println(" " + Z); kolejnosc.add(p.getPos()); } pw.println("TAK"); for (int ii = 0; ii < kolejnosc.size(); ii++) { pw.print(kolejnosc.get(ii)); if (ii != kolejnosc.size() - 1) { pw.print(" "); } } pw.println(); pw.flush(); } public static void main(String[] args) throws IllegalArgumentException, IOException { new boh(true).doit(); } } |