You are given a tree consisting exactly of n vertices. Tree is a connected undirected
graph with n−1 edges. Each vertex v of this tree has a value avassigned to it.
Let dist(x,y)be the distance between the vertices x and y. The distance between the
vertices is the number of edges on the simple path between them.
Let's define the cost of the tree as the following value: firstly, let's fix some
vertex of the tree. Let it be v. Then the cost of the tree is ∑i=1ndist(i,v)⋅ai.
Your task is to calculate the maximum possible cost of the tree if you can choose v
arbitrarily.
【Input】
The first line contains one integer n, the number of vertices in the tree (1≤n≤2⋅105).
The second line of the input contains nintegers a1,a2,…,an (1≤ai≤2⋅105), where ai is
the value of the vertex i
Each of the next n−1lines describes an edge of the tree. Edge i is denoted by two
integers ui and vi, the labels of vertices it connects (1≤ui,vi≤n, ui≠vi).
It is guaranteed that the given edges form a tree.
【Output】
Print one integer — the maximum possible cost of the tree if you can choose any vertex
as v.