fork download
  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args) {
  5. int arr[] = { 4, 6, 8 };
  6. System.out.println(solve(arr));
  7. }
  8.  
  9. private static int solve(int[] arr){
  10. PriorityQueue<Integer> pq = new PriorityQueue<>();
  11.  
  12. for(int a : arr)
  13. pq.add(a);
  14. int cost = 0;
  15. while(pq.size() >= 2){
  16. int x = pq.remove();
  17. int y = pq.remove();
  18. cost += x+y;
  19. pq.add(x+y);
  20. }
  21.  
  22. return cost;
  23. }
  24. }
Success #stdin #stdout 0.1s 52792KB
stdin
Standard input is empty
stdout
28