CF1303G.Sum of Prefix Sums
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
We define the sum of prefix sums of an array [s1,s2,…,sk] as s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk) .
You are given a tree consisting of n vertices. Each vertex i has an integer ai written on it. We define the value of the simple path from vertex u to vertex v as follows: consider all vertices appearing on the path from u to v , 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 n ( 2≤n≤150000 ) — the number of vertices in the tree.
Then n−1 lines follow, representing the edges of the tree. Each line contains two integers ui and vi ( 1≤ui,vi≤n , ui=vi ), denoting an edge between vertices ui and vi . It is guaranteed that these edges form a tree.
The last line contains n integers a1 , a2 , ..., an ( 1≤ai≤106 ).
输出格式
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 3 to vertex 1 . It gives the sequence [3,3,7,1] , and the sum of prefix sums is 36 .