不知道为何错了 ACWING 148求帮助
#include<iostream>
using namespace std;
const int N = 1e5+10;
int q[N]; int n;
void down(int t)
{
int u = t;
if(t*2 <= n && q[u] > q[2*t]) u = 2*t;
if(t*2+1 <= n && q[u] > q[2*t+1]) u = 2*t+1;
if(u != t)
{
swap(q[u],q[t]);
down(u);
}
}
void up(int k)
{
while(k/2 && q[k/2] > q[k]) {
swap(q[k],q[k/2]);
k = k/2;
}
}
void insert(int val)
{
q[++n] = val;
up(n);
}
int getop(){
int t = q[1];
q[1] = q[n];
n--;
down(1);
return t;
}
int main(){
cin >> n;
for(int i =1; i<= n;i++)
{
cin >> q[i];
}
for(int i = n/2; i ; i--)
{
down(q[i]);
}
int res = 0;
while(n>1)
{
int a= getop();
int b = getop();
insert(a+b);
res+= (a+b);
}
cout << res;
}
用STL的priority_queue不香???