fork download
  1. import java.util.PriorityQueue;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. PriorityQueue<Long> nums = new PriorityQueue<Long>();
  8. Scanner sc = new Scanner(System.in);
  9. long n = sc.nextLong();
  10. for (int i = 0; i < n; i++) {
  11. long number = sc.nextLong();
  12. nums.add(number);
  13. }
  14.  
  15. long minSum = 0;
  16. long a;
  17. long b;
  18. while (!nums.isEmpty()) {
  19.  
  20. a = nums.poll();
  21.  
  22. if (nums.isEmpty()) {
  23. break;
  24. }
  25. b = nums.poll();
  26. minSum += a + b;
  27. nums.add(a + b);
  28.  
  29. }
  30. System.out.println(minSum);
  31.  
  32. }
  33. }
  34.  
Runtime error #stdin #stdout #stderr 0.06s 2184192KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextLong(Scanner.java:2222)
	at java.util.Scanner.nextLong(Scanner.java:2182)
	at Main.main(Main.java:9)