Advertisement
LLIAMA3OB

Alice and apples

Feb 24th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class AliceAndApples {
  5.     static void solution() throws IOException {
  6.         Apple[] a = new Apple[readInt()];
  7.         long s = readInt();
  8.         for (int i = 0; i < a.length; i++) {
  9.             a[i] = new Apple(readInt(), readInt(), i + 1);
  10.         }
  11.         Arrays.sort(a);
  12.         for (int i = 0; i < a.length; i++) {
  13.             s -= a[i].a;
  14.             if (s <= 0) {
  15.                 pw.print(-1);
  16.                 return;
  17.             }
  18.             s += a[i].b;
  19.         }
  20.         for (int i = 0; i < a.length; i++) {
  21.             pw.print(a[i].num + " ");
  22.         }
  23.     }
  24.  
  25.     static class Apple implements Comparable<Apple> {
  26.         int a, b, num;
  27.  
  28.         Apple(int a, int b, int num) {
  29.             this.a = a;
  30.             this.b = b;
  31.             this.num = num;
  32.         }
  33.  
  34.         int d() {
  35.             return b - a;
  36.         }
  37.  
  38.         @Override
  39.         public int compareTo(Apple o) {
  40.             int d1 = d(), d2 = o.d();
  41.             if (d1 > 0 && d2 > 0) return a - o.a;
  42.             if (d1 > 0) return -1;
  43.             if (d2 > 0) return 1;
  44.             return o.b - b;
  45.         }
  46.     }
  47.  
  48.     static String fileName = "apples";
  49.  
  50.     public static void main(String[] args) throws IOException {
  51.         if (new File(fileName + ".in").exists()) {
  52.             br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName + ".in"))));
  53.             pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(new File(fileName + ".out"))));
  54.         } else {
  55.             br = new BufferedReader(new InputStreamReader(System.in));
  56.             pw = new PrintWriter(new BufferedOutputStream(System.out));
  57.         }
  58.         solution();
  59.         pw.close();
  60.     }
  61.  
  62.     static String readLine() throws IOException {
  63.         while (st == null || !st.hasMoreTokens())
  64.             st = new StringTokenizer(br.readLine());
  65.         return st.nextToken("\n");
  66.     }
  67.  
  68.     static boolean hasNext() throws IOException {
  69.         while (st == null || !st.hasMoreTokens()) {
  70.             String s = br.readLine();
  71.             if (s == null) {
  72.                 return false;
  73.             }
  74.             st = new StringTokenizer(s);
  75.         }
  76.         return true;
  77.     }
  78.  
  79.     static boolean hasNextInLine() {
  80.         if (st == null || !st.hasMoreTokens()) {
  81.             return false;
  82.         }
  83.         return true;
  84.     }
  85.  
  86.     static void toNextLine() throws IOException {
  87.         st = new StringTokenizer(br.readLine());
  88.     }
  89.  
  90.     static String next() throws IOException {
  91.         while (st == null || !st.hasMoreTokens()) {
  92.             st = new StringTokenizer(br.readLine());
  93.         }
  94.         return st.nextToken();
  95.     }
  96.  
  97.     static int readInt() throws IOException {
  98.         return Integer.parseInt(next());
  99.     }
  100.  
  101.     static long readLong() throws IOException {
  102.         return Long.parseLong(next());
  103.     }
  104.  
  105.     static short readShort() throws IOException {
  106.         return Short.parseShort(next());
  107.     }
  108.  
  109.     static byte readByte() throws IOException {
  110.         return Byte.parseByte(next());
  111.     }
  112.  
  113.     static double readDouble() throws IOException {
  114.         return Double.parseDouble(next());
  115.     }
  116.  
  117.  
  118.     static PrintWriter pw;
  119.     static BufferedReader br;
  120.     static StringTokenizer st;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement