优先队列来存储因为优先队列的插入是logn的这样就是nlogn了不然用排序的话变成n2logn
#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <queue>
using namespace std;
int num[111111];
int num1[111111];
struct node{
int id,n1;
bool operator <(const node & b)const{
return n1<b.n1;
}
};
bool cmp(node a,node b)
{
return a.n1<b.n1;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
//memset(num1,0,sizeof(num1));
priority_queue<node> pq;
int k;
scanf("%d",&k);
long long k1=0;
long long k2=0;
for(int i=1;i<=k;i++)
{
scanf("%d",&num[i]);
}
int k3=k/2;
if(k%2==0) num1[k3]=1;
else num1[k3]=2;
for(int i=1;i<k3;i++)
num1[i]=2;
int cont=0;
for(int i=k3+1;i<=k;i++)
{
node ans;
ans.id=i;
ans.n1=num[i];
pq.push(ans);
}
int rr=1;
while(!pq.empty())
{
node t=pq.top();pq.pop();
if(rr&1) k1+=t.n1;
else k2+=t.n1;
int fa=t.id/2;
if((--num1[fa])==0)
{
node ans;
ans.id=fa;
ans.n1=num[fa];
pq.push(ans);
}
rr++;
}
printf("%lld %lld\n",k1,k2);
}
return 0;
}