import java.util.Comparator; import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; public class boh { static Scanner sc = new Scanner(System.in); TreeSet<int[]> ts = new TreeSet<int[]>(new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { return ((o1[2] - o1[1]) < (o2[2] - o2[1]) ? 1 : ((o1[2] - o1[1]) == (o2[2] - o2[1]) ? 1 : -1)); } }); TreeSet<int[]> tsn = new TreeSet<int[]>(new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { return (o1[1] > o2[1] ? -1 : 1); } }); public static void main(String[] args) { new boh().start(); } private void print(TreeSet<int[]> ts) { Iterator<int[]> iter = ts.iterator(); while (iter.hasNext()) { int[] n = iter.next(); for (int i = 0; i < 3; i++) { System.out.print(n[i] + " "); } System.out.println(); } } private void start() { int size = sc.nextInt(); long lives = sc.nextLong(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { int[] n = new int[] { i + 1, sc.nextInt(), sc.nextInt() }; if (n[2] - n[1] >= 0) ts.add(n); else tsn.add(n); } out: while (ts.size() > 0 && lives > 0) { int[] best = ts.first(); Iterator<int[]> iter = ts.iterator(); while (iter.hasNext()) { int[] n = iter.next(); if (n[1] < lives) { iter.remove(); lives -= n[1]; lives += n[2]; sb.append(n[0] + " "); continue out; } } lives -= best[1]; continue out; } if (lives < 0) { System.out.println("NIE"); return; } out: while (tsn.size() > 0 && lives > 0) { int[] best = tsn.first(); Iterator<int[]> iter = tsn.iterator(); while (iter.hasNext()) { int[] n = iter.next(); if (n[1] < lives) { iter.remove(); lives -= n[1]; lives += n[2]; sb.append(n[0] + " "); continue out; } } lives -= best[1]; continue out; } if (lives > 0) { System.out.println("TAK"); System.out.println(sb.toString()); } 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 | import java.util.Comparator; import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; public class boh { static Scanner sc = new Scanner(System.in); TreeSet<int[]> ts = new TreeSet<int[]>(new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { return ((o1[2] - o1[1]) < (o2[2] - o2[1]) ? 1 : ((o1[2] - o1[1]) == (o2[2] - o2[1]) ? 1 : -1)); } }); TreeSet<int[]> tsn = new TreeSet<int[]>(new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { return (o1[1] > o2[1] ? -1 : 1); } }); public static void main(String[] args) { new boh().start(); } private void print(TreeSet<int[]> ts) { Iterator<int[]> iter = ts.iterator(); while (iter.hasNext()) { int[] n = iter.next(); for (int i = 0; i < 3; i++) { System.out.print(n[i] + " "); } System.out.println(); } } private void start() { int size = sc.nextInt(); long lives = sc.nextLong(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { int[] n = new int[] { i + 1, sc.nextInt(), sc.nextInt() }; if (n[2] - n[1] >= 0) ts.add(n); else tsn.add(n); } out: while (ts.size() > 0 && lives > 0) { int[] best = ts.first(); Iterator<int[]> iter = ts.iterator(); while (iter.hasNext()) { int[] n = iter.next(); if (n[1] < lives) { iter.remove(); lives -= n[1]; lives += n[2]; sb.append(n[0] + " "); continue out; } } lives -= best[1]; continue out; } if (lives < 0) { System.out.println("NIE"); return; } out: while (tsn.size() > 0 && lives > 0) { int[] best = tsn.first(); Iterator<int[]> iter = tsn.iterator(); while (iter.hasNext()) { int[] n = iter.next(); if (n[1] < lives) { iter.remove(); lives -= n[1]; lives += n[2]; sb.append(n[0] + " "); continue out; } } lives -= best[1]; continue out; } if (lives > 0) { System.out.println("TAK"); System.out.println(sb.toString()); } else { System.out.println("NIE"); } } } |