Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
492 B
20
Indexable
Never
public class Solution {
    public Long solve(ArrayList<Integer> A) {
        
        int n = A.size();
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int i = 0 ; i < n ; i++){
            pq.add(A.get(i));
        }
        long ans = 0;
        
        while( pq.size() > 1 ){
           
           int x = pq.poll();
           int y = pq.poll();
           
           ans = ans + x+y;
           pq.add(x+y);
            
        }
      return ans;  
    }
}