409491: GYM103584 D Collecting Syrup

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Collecting Syruptime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Maria is a big fan of all things sweet, especially maple syrup. Luckily, she's managed to find a large grove of maple trees and wants to collect as much syrup as she can! The grove consists of $$$n$$$ trees and $$$n - 1$$$ paved roads that Maria can travel on. The grove is connected, so Maria can reach any tree from any start tree by traveling along these paved roads. Each tree has an associated syrup value, $$$s_i$$$, which gives how much syrup Maria can potentially extract from the tree.

Unfortunately, Maria doesn't want to risk getting lost in the grove and straying too far away from her starting position. Given that Maria can start from any tree in the grove and collect syrup from all trees that are directly connected to her starting tree with a single road, find out the maximum amount of syrup that Maria can collect!

Input

The first line is a single integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$, which gives the number of trees in the maple grove. The next line consists of $$$n$$$ integers $$$s_i$$$ $$$(1 \leq s_i \leq 1000)$$$, where the $$$i$$$th value gives the syrup values for the $$$i$$$th tree. The next $$$n - 1$$$ lines consist of two integers $$$u_i, v_i$$$ $$$1 \leq u_i, v_i \leq n$$$, which gives two trees that are connected by a paved road.

Output

Output a single integer giving the maximum amount of syrup that Maria can collect.

ExampleInput
4
1 2 3 4
1 2
2 3
3 4
Output
9
Note

If Maria starts at tree $$$3$$$, she can collect $$$2 + 3 + 4 = 9$$$ syrup overall.

加入题单

算法标签: