luogu#P10555. [ICPC2024 Xi'an I] Fix the Tree

[ICPC2024 Xi'an I] Fix the Tree

题目描述

You are given a tree consisting of nn vertices. Each vertex ii of this tree has a value wiw_i assigned to it.

Now the vertex uu will be broken. Once it's broken, vertex uu and all edges with one end at vertex uu will be removed from the tree.

To make the tree connected, you can do the following operation any number of times(possibly zero) in any order:

  • First choose two vertices uu and vv from the tree;
  • Then pay wu+wvw_u+w_v coins, and add an edge between vertices uu and vv;
  • At last, replace wu+1w_u+1 with wuw_u, replace wv+1w_v+1 with wvw_v.

Your task is to calculate the minimum number of coins to be paid.

But you don't know which vertex uu is, so you need to find the answer for each 1un1\le u\le n. Please answer all the queries independently.

输入格式

The first line contains a single integer n(2n106)n(2\le n\le 10^6) --- the number of vertices in this tree.

The next line contains nn numbers, the ii -th number is wi(1win)w_i(1\le w_i\le n).

The next n1n-1 lines contain a description of the tree's edges. The ii -th of these lines contains two integers uiu_i and vi(1ui,vin)v_i(1\le u_i,v_i\le n) --- the numbers of vertices connected by the ii -th edge.

It is guaranteed that the given edges form a tree.

输出格式

Print nn integers, the ii -th integer is the answer when u=iu=i.

6
1 1 1 1 1 1
1 2
1 3
1 4
2 5
2 6
4 4 0 0 0 0
4
1 2 3 4
1 2
1 3
1 4
12 0 0 0
7
1 2 3 4 5 6 7
1 2
1 3
2 4
2 5
3 6
3 7
5 12 16 0 0 0 0