Huffman树

练习
01 合并果子

import java.io.*;
import java.util.*;
public class huffmantree1 {
public static int n;
public static PriorityQueue<Integer> heap = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(br.readLine());
String[] str1 = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
heap.offer(Integer.parseInt(str1[i]));
}
int res = 0;
while (heap.size() != 1) {
int a = heap.poll();
int b = heap.poll();
heap.offer(a + b);
res += a + b;
}
pw.println(res);
pw.close();
br.close();
}
}