CF1303G.Sum of Prefix Sums

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

We define the sum of prefix sums of an array [s1,s2,,sk][s_1, s_2, \dots, s_k] as s1+(s1+s2)+(s1+s2+s3)++(s1+s2++sk)s_1 + (s_1 + s_2) + (s_1 + s_2 + s_3) + \dots + (s_1 + s_2 + \dots + s_k) .

You are given a tree consisting of nn vertices. Each vertex ii has an integer aia_i written on it. We define the value of the simple path from vertex uu to vertex vv as follows: consider all vertices appearing on the path from uu to vv , write down all the numbers written on these vertices in the order they appear on the path, and compute the sum of prefix sums of the resulting sequence.

Your task is to calculate the maximum value over all paths in the tree.

输入格式

The first line contains one integer nn ( 2n1500002 \le n \le 150000 ) — the number of vertices in the tree.

Then n1n - 1 lines follow, representing the edges of the tree. Each line contains two integers uiu_i and viv_i ( 1ui,vin1 \le u_i, v_i \le n , uiviu_i \ne v_i ), denoting an edge between vertices uiu_i and viv_i . It is guaranteed that these edges form a tree.

The last line contains nn integers a1a_1 , a2a_2 , ..., ana_n ( 1ai1061 \le a_i \le 10^6 ).

输出格式

Print one integer — the maximum value over all paths in the tree.

输入输出样例

  • 输入#1

    4
    4 2
    3 2
    4 1
    1 3 3 7

    输出#1

    36

说明/提示

The best path in the first example is from vertex 33 to vertex 11 . It gives the sequence [3,3,7,1][3, 3, 7, 1] , and the sum of prefix sums is 3636 .

首页