package jp.fedom.challange.yuki.l2.q297; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String[] c = new String[N]; for (int i = 0; i < N; i++) { c[i] = sc.next(); } sc.close(); System.out.println(solve(N, c)); } public static String solve(int N, String[] c) { PriorityQueue n = new PriorityQueue<>(new Comparator() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; }; }); PriorityQueue ex = new PriorityQueue<>(new Comparator() { @Override public int compare(Character o1, Character o2) { return o1 - o2; }; }); PriorityQueue nex = new PriorityQueue<>(new Comparator() { @Override public int compare(Character o1, Character o2) { return o2 - o1; }; }); for (String s : c) { if (s.equals("+") || s.equals("-")) { ex.add(s.charAt(0)); } else { n.add(new Integer(s)); } } System.out.println(n); System.out.println(ex); int max = 0; int min = 0; while (n.size() > ex.size()) { max *= 10; max += n.poll(); } PriorityQueue nc = new PriorityQueue<>(n); System.out.println(nc); System.out.println(n); nex.addAll(ex); int absmax = max; while (ex.size() > 0) { char ch = (char) ex.poll(); if (ch == '-') { max -= n.poll(); } else { max += n.poll(); } } nc.add(absmax); nex.add('+'); System.out.println(nc); while (nex.size() > 0) { char ch = (char) nex.poll(); if (ch == '-') { min -= nc.poll(); } else { min += nc.poll(); } } return max + " " + min; } }