Untitled
unknown
plain_text
2 years ago
492 B
30
Indexable
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;
}
}
Editor is loading...