B - Tree with Maximum Cost(樹形DP)

You are given a tree consisting exactly of nn vertices. Tree is a connected undirected graph with n−1n−1 edges. Each vertex vv of this tree has a value avavassigned to it.

Let dist(x,y)dist(x,y) be the distance between the vertices xx and yy. 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 vv. Then the cost of the tree is ∑i=1ndist(i,v)⋅ai∑i=1ndist(i,v)⋅ai.

Your task is to calculate the maximum possible cost of the tree if you can choose vvarbitrarily.

Input

The first line contains one integer nn, the number of vertices in the tree (1≤n≤2⋅1051≤n≤2⋅105).

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the value of the vertex ii.

Each of the next n−1n−1 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the labels of vertices it connects (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠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 vv.

Examples

Input

8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8

Output

121

Input

1
1337

Output

0

Note

Picture corresponding to the first example: 

You can choose the vertex 33 as a root, then the answer will be 2⋅9+1⋅4+0⋅1+3⋅7+3⋅10+4⋅1+4⋅6+4⋅5=18+4+0+21+30+4+24+20=1212⋅9+1⋅4+0⋅1+3⋅7+3⋅10+4⋅1+4⋅6+4⋅5=18+4+0+21+30+4+24+20=121.

In the second example tree consists only of one vertex so the answer is always 00.

題目大意:一個樹,每個點都有權值,對於一節點k,定義f[k] 爲 其他節點p到該點的距離*val[p] 的和,輸出最大的f[k]

思路概括:

兩邊dfs,第一遍,保存子節點到父節點的結果和全部子節點的val和,因爲向上跟新一個節點,就相當於加上一次val。

第二遍跟新父節點到子節點,也就是父節點的結果 減去 子節點累計的權值和 加上(總權值和 - 子節點的權值和)

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define lt k<<1
#define rt k<<1|1
#define lowbit(x) x&(-x)
#define lson l,mid,lt
#define rson mid+1,r,rt
using namespace std;
typedef long long ll;
typedef long double ld;
#define ios ios::sync_with_stdio(false);cin.tie(nullptr);
#define mem(a, b) memset(a, b, sizeof(a))
//#define int ll
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5 + 5;
int n;
ll sum;
ll val[maxn];
vector<ll> ve[maxn];
ll a[maxn];
ll dp[maxn];

void dfs1(int u, int f)
{
    val[u] = a[u];
    for(int i=0;i<ve[u].size();i++)
    {
        int to = ve[u][i];
        if(to == f) continue;
        dfs1(to, u);
        dp[u] += val[to] + dp[to];
        val[u] += val[to];
    }
}
ll ans;
void dfs2(int u, int f)
{
    ans = max(ans, dp[u]);
    for(int i=0;i<ve[u].size();i++)
    {
        int to = ve[u][i];
        if(to == f) continue;
        dp[to] = dp[u] - val[to] + (sum - val[to]);
        dfs2(to, u);
    }
}
int main()
{
    mem(val, 0);
    sum = 0;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    int u, v;
    for(int i=1;i<n;i++)
    {
        cin >> u >> v;
        ve[u].push_back(v);
        ve[v].push_back(u);
    }
    dfs1(1, 0);
    ans = dp[1];
    dfs2(1, 0);
    cout << ans << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章