public void solve() throws IOException {
    PriorityQueue<Integer> minqueue = new PriorityQueue<Integer>();
    PriorityQueue<Integer> maxqueue = new PriorityQueue<Integer>(11, Collections.reverseOrder());

    // ArrayList<Integer> cookies = new ArrayList<Integer>();
    String nextline = "";
    while ((nextline = nextLine()) != null) {
      if (nextline.equals("#")) {
        if (minqueue.size() >= maxqueue.size())
          System.out.println(minqueue.remove());
        else
          System.out.println(maxqueue.remove());
      } else {
        Integer a = Integer.valueOf(nextline);
        minqueue.offer(a);
      }
      if (minqueue.size() > maxqueue.size() + 1)
        maxqueue.offer(minqueue.remove());
      if (maxqueue.size() > minqueue.size() + 1)
        minqueue.offer(maxqueue.remove());
    }

  }