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 avav assigned 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

给定一棵树 ,u点关于数的权值是∑distance (i, u)×value ( i ),求得所有点中对应数的全值的最大值

sum[ i ]为 i 和 i 的子树的权值和;dp[ i ] 为以i点为根节点的树所求出的这个子树的权值,

dp[1]为当u等于1时的值,则,dp[2] = dp[1] -sum[2]+sum[1]-sum[2]

因此dp[i] = dp[father]  + sum[1]- 2*dp[i]

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int to,next;
} edge[400005];

long long head[200005],dp[200005],sum[200005],cnt,ans;

void add(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}


void bfs(int u, int father)
{
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == father)
            continue;
        bfs(v,u);
        sum[u] += sum[v];
        dp[u] += dp[v] + sum[v];
    }
}


void bfs2(int u, int father)
{
    if(u !=1)
        dp[u] = dp[father] + sum[1]-2*sum[u];
    ans = max(ans,dp[u]);
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == father)
            continue;
        bfs2(v,u);

    }
    return ;
}

int main()
{
    int i,n,v,u;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    memset(dp,0,sizeof(dp));
    for(i = 1; i <= n; i ++)
    {
        scanf("%lld",&sum[i]);
    }
    cnt = 0;
    for(i = 0; i < n-1; i ++)
    {
        scanf("%d %d",&u, &v);
        add(u,v);
        add(v,u);
    }
    bfs(1,0);
    bfs2(1,0);
    printf("%lld\n",ans);

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值