import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.ListIterator; public class boh { public static void main(String args[]) throws IOException{ int curHp = 0; int hp = 0; int n = 0; int dmg = 0; long suma = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] line = new String[2]; line = reader.readLine().split(" "); n = Integer.parseInt(line[0]); hp = Integer.parseInt(line[1]); LinkedList<Integer> doZabiciaNr = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaDmg = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaHp = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaNr1 = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaDmg1 = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaHp1 = new LinkedList<Integer>(); LinkedList<Integer> kolejnosc = new LinkedList<Integer>(); curHp = hp; suma += hp; for (int i = 0; i < n; i++){ line = reader.readLine().split(" "); dmg = Integer.parseInt(line[0]); hp = Integer.parseInt(line[1]); suma = suma + hp - dmg; if (dmg >= curHp){ if (dmg > hp){ doZabiciaDmg.add(dmg); doZabiciaHp.add(hp); doZabiciaNr.add(i); } else { doZabiciaDmg1.add(dmg); doZabiciaHp1.add(hp); doZabiciaNr1.add(i); } } else { if (dmg > hp){ doZabiciaDmg.add(dmg); doZabiciaHp.add(hp); doZabiciaNr.add(i); } else { kolejnosc.add(i); curHp = curHp + hp - dmg; } } } if (suma > 0){ while (doZabiciaNr1.isEmpty() == false){ int size1 = doZabiciaNr1.size(); ListIterator<Integer> itNr = doZabiciaNr1.listIterator(0); ListIterator<Integer> itDmg = doZabiciaDmg1.listIterator(0); ListIterator<Integer> itHp = doZabiciaHp1.listIterator(0); while (itNr.hasNext()){ int nr = itNr.next(); hp = itHp.next(); dmg = itDmg.next(); if (dmg < curHp){ kolejnosc.add(nr); itNr.remove(); itHp.remove(); itDmg.remove(); curHp = curHp + hp - dmg; } } if (size1 == doZabiciaNr1.size()) { System.out.println("NIE"); return; } } while (doZabiciaNr.isEmpty() == false){ ListIterator<Integer> itNr = doZabiciaNr.listIterator(); ListIterator<Integer> itDmg = doZabiciaDmg.listIterator(); ListIterator<Integer> itHp = doZabiciaHp.listIterator(); while (itNr.hasNext()){ hp = itHp.next(); dmg = itDmg.next(); kolejnosc.add(itNr.next()); itNr.remove(); itHp.remove(); itDmg.remove(); curHp = curHp + hp - dmg; } } ListIterator<Integer> kol = kolejnosc.listIterator(); System.out.println("TAK"); System.out.print((kol.next() + 1)); while (kol.hasNext()){ System.out.print(" " + (kol.next() + 1)); } System.out.println(); } 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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.ListIterator; public class boh { public static void main(String args[]) throws IOException{ int curHp = 0; int hp = 0; int n = 0; int dmg = 0; long suma = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] line = new String[2]; line = reader.readLine().split(" "); n = Integer.parseInt(line[0]); hp = Integer.parseInt(line[1]); LinkedList<Integer> doZabiciaNr = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaDmg = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaHp = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaNr1 = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaDmg1 = new LinkedList<Integer>(); LinkedList<Integer> doZabiciaHp1 = new LinkedList<Integer>(); LinkedList<Integer> kolejnosc = new LinkedList<Integer>(); curHp = hp; suma += hp; for (int i = 0; i < n; i++){ line = reader.readLine().split(" "); dmg = Integer.parseInt(line[0]); hp = Integer.parseInt(line[1]); suma = suma + hp - dmg; if (dmg >= curHp){ if (dmg > hp){ doZabiciaDmg.add(dmg); doZabiciaHp.add(hp); doZabiciaNr.add(i); } else { doZabiciaDmg1.add(dmg); doZabiciaHp1.add(hp); doZabiciaNr1.add(i); } } else { if (dmg > hp){ doZabiciaDmg.add(dmg); doZabiciaHp.add(hp); doZabiciaNr.add(i); } else { kolejnosc.add(i); curHp = curHp + hp - dmg; } } } if (suma > 0){ while (doZabiciaNr1.isEmpty() == false){ int size1 = doZabiciaNr1.size(); ListIterator<Integer> itNr = doZabiciaNr1.listIterator(0); ListIterator<Integer> itDmg = doZabiciaDmg1.listIterator(0); ListIterator<Integer> itHp = doZabiciaHp1.listIterator(0); while (itNr.hasNext()){ int nr = itNr.next(); hp = itHp.next(); dmg = itDmg.next(); if (dmg < curHp){ kolejnosc.add(nr); itNr.remove(); itHp.remove(); itDmg.remove(); curHp = curHp + hp - dmg; } } if (size1 == doZabiciaNr1.size()) { System.out.println("NIE"); return; } } while (doZabiciaNr.isEmpty() == false){ ListIterator<Integer> itNr = doZabiciaNr.listIterator(); ListIterator<Integer> itDmg = doZabiciaDmg.listIterator(); ListIterator<Integer> itHp = doZabiciaHp.listIterator(); while (itNr.hasNext()){ hp = itHp.next(); dmg = itDmg.next(); kolejnosc.add(itNr.next()); itNr.remove(); itHp.remove(); itDmg.remove(); curHp = curHp + hp - dmg; } } ListIterator<Integer> kol = kolejnosc.listIterator(); System.out.println("TAK"); System.out.print((kol.next() + 1)); while (kol.hasNext()){ System.out.print(" " + (kol.next() + 1)); } System.out.println(); } else { System.out.println("NIE"); } } } |