最优二叉树问题
把最小的两端拿出来连接后放入队列中,重复的上述操作,,
用优先队列可以容易完成
(注:博客作为交流使用,切勿抄袭应付作业)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
using namespace std;
typedef long long ll;
const int maxn = 1000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int n;
priority_queue, greater > qu;
void init() {
scanf("%d", &n);
int x;
for(int i = 0; i < n; ++i) {
scanf("%d", &x);
qu.push(x);
}
}
void solve() {
int ans = 0;
while(!qu.empty()) {
int t1 = qu.top(); qu.pop();
if(qu.empty()) { break; }
int t2 = qu.top(); qu.pop();
t1 += t2;
ans += t1;
if(qu.empty()) break;
qu.push(t1);
}
cout << ans << endl;
}
int main() {
init();
solve();
return 0;
}
/*
8
4 5 1 2 1 3 1 1
*/