链接:https://codeforces.com/contest/1466/problem/D
题目:
ac代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long int LL ;
const int N = 2e5 + 10, M = N * 2;
typedef pair<LL,int> PLI;
LL a[N];
int d[N];
LL zuida[N];
int h[N],e[M],ne[M],idx;
bool st[N];
LL b[N];
int cnt = 0 ;
void add(int A , int B )
{
e[idx] = B , ne[idx] = h[A] , h[A] = idx ++ ;
}
bool cmp(LL A , LL B)
{
return A > B;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n ;
for(int i = 1; i <= n ; i ++)
{
h[i] = -1;
d[i] = 0 ;
st[i] = false;
}
LL ans = cnt = idx = 0 ;
for(int i = 1; i <= n ; i ++)
{
cin >> a[i];
ans += a[i];
}
int x, y ;
for(int i = 1; i <= n - 1 ; i ++)
{
cin >> x >> y ;
d[x] ++ ;
d[y] ++ ;
}
cnt = 0 ;
for(int i = 1; i <= n ; i ++)
{
d[i]--;
while(d[i]--)
b[cnt++] = a[i];
}
sort(b,b+cnt,cmp);
for(int i = 0; i <= n - 2; i ++)
{
cout << ans <<' ';
if(i < cnt)ans += b[i];
}
cout <<endl;
}
}