import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
public class boh {
// by asokoly
private static int skipWhitespace(final InputStream in) throws IOException {
int val = -1;
while ((val = in.read()) != -1) {
if (!Character.isWhitespace((char) val))
break;
}
return val;
}
private static int readInt(final InputStream in) {
final StringBuilder b = new StringBuilder();
try {
int val = skipWhitespace(in);
b.append((char) val);
while ((val = in.read()) != -1) {
if (Character.isWhitespace((char) val))
break;
b.append((char) val);
}
return Integer.parseInt(b.toString());
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
// by asokoly */
static class data {
int d, a, i;
data(int d, int a, int i) {
this.d = d;
this.a = a;
this.i = i;
}
@Override
public String toString() {
return i + " " + d + " " + a;
}
}
public static void run(final InputStream in, PrintStream out) {
int n = readInt(in);
int z = readInt(in);
double sd = 0, sa = 0;
data[] ta = new data[n];
for (int i = 0; i < n; i++) {
int d = readInt(in);
int a = readInt(in);
ta[i] = new boh.data(d, a, i);
sd += d;
sa += a;
}
if (z + sa - sd <= 0) {
out.println("NIE");
} else {
Arrays.sort(ta, new Comparator<data>() {
@Override
public int compare(data o1, data o2) {
return o2.a - o1.a;
}
});
int ic = 0;
int i = 0;
int w = 0;
while(i+w < n) {
if (ta[i].d < z)
{
z -= ta[i].d;
z += ta[i].a;
ic++;
i ++;
w = 0;
}
else
if (i + w + 1 < n)
{
data k = ta[i+1+w];
ta[i+1+w] = ta[i];
ta[i] = k;
w ++;
}
else{
i++;
}
}
if( ic == n)
{
out.println("TAK");
for (int j = 0; j < ic; j++) {
System.out.print((ta[j].i+1)+ " ");
}
}
else
{
out.println("NIE");
}
}
}
public static void main(String[] args) {
run(System.in, System.out);
}
}
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 | import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; public class boh { // by asokoly private static int skipWhitespace(final InputStream in) throws IOException { int val = -1; while ((val = in.read()) != -1) { if (!Character.isWhitespace((char) val)) break; } return val; } private static int readInt(final InputStream in) { final StringBuilder b = new StringBuilder(); try { int val = skipWhitespace(in); b.append((char) val); while ((val = in.read()) != -1) { if (Character.isWhitespace((char) val)) break; b.append((char) val); } return Integer.parseInt(b.toString()); } catch (final IOException e) { throw new RuntimeException(e); } } // by asokoly */ static class data { int d, a, i; data(int d, int a, int i) { this.d = d; this.a = a; this.i = i; } @Override public String toString() { return i + " " + d + " " + a; } } public static void run(final InputStream in, PrintStream out) { int n = readInt(in); int z = readInt(in); double sd = 0, sa = 0; data[] ta = new data[n]; for (int i = 0; i < n; i++) { int d = readInt(in); int a = readInt(in); ta[i] = new boh.data(d, a, i); sd += d; sa += a; } if (z + sa - sd <= 0) { out.println("NIE"); } else { Arrays.sort(ta, new Comparator<data>() { @Override public int compare(data o1, data o2) { return o2.a - o1.a; } }); int ic = 0; int i = 0; int w = 0; while(i+w < n) { if (ta[i].d < z) { z -= ta[i].d; z += ta[i].a; ic++; i ++; w = 0; } else if (i + w + 1 < n) { data k = ta[i+1+w]; ta[i+1+w] = ta[i]; ta[i] = k; w ++; } else{ i++; } } if( ic == n) { out.println("TAK"); for (int j = 0; j < ic; j++) { System.out.print((ta[j].i+1)+ " "); } } else { out.println("NIE"); } } } public static void main(String[] args) { run(System.in, System.out); } } |
English