题解 | #哈夫曼树#
哈夫曼树
https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155
#include <iostream> #include <algorithm> using namespace std; int main(void){ for(int n;cin >> n;){ int *arr = new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; sort(arr,arr+n); int sum=0; for(int i=0;i<n-1;i++){ int t = arr[i]+arr[i+1]; sum+=t; arr[i+1]=t; sort(arr+i+1,arr+n); } cout << sum << endl; } return 0; }