题解 | #哈夫曼树#
哈夫曼树
https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155
#include <cstdio> #include <queue> using namespace std; int main(){ int n; scanf("%d",&n); priority_queue<int> que; for(int i=0;i<n;i++){ int num; scanf("%d",&num); que.push(-num); } int res=0; while(que.size()>1){ int left1=que.top(); que.pop(); int left2=que.top(); que.pop(); res=res+left1+left2; que.push(left1+left2); } printf("%d",-res); }