AcWing 148. 合并果子
原题链接
简单
作者:
破云
,
2020-06-23 18:44:34
,
所有人可见
,
阅读 691
算法1
(java版) $O(nlgn)$
java 代码
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[]args){
Scanner sn =new Scanner(System.in);
int N = sn.nextInt();
PriorityQueue<Integer> pq = new PriorityQueue <>();
for (int i = 0; i < N; i++) {
pq.offer(sn.nextInt());
}
int res=0;
while (pq.size()>1){
Integer v = pq.poll();
Integer w = pq.poll();
res= res+v+w;
pq.offer(v+w);
}
System.out.println(res);
}
}