哈夫曼樹裸題,用優先隊列做的。。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
struct ak
{
int x;
bool operator <(const ak & a)const
{
return a.x < x;
}
}temp;
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test0.out", "w", stdout);
//srand(time(NULL));
long long ans;
int N, a[20005];
while(~scanf("%d", &N))
{
priority_queue<ak> pq;
for(int i = 0; i < N; ++i)
{
scanf("%d", &temp.x);
pq.push(temp);
}
ans = 0;
while(pq.size() != 1)
{
temp.x = pq.top().x;
pq.pop();
temp.x += pq.top().x;
pq.pop();
ans += temp.x;
pq.push(temp);
}
printf("%lld\n", ans);
}
return 0;
}