CF1303G.Sum of Prefix Sums
省选/NOI-
通过率:0%
时间限制:6.00s
内存限制:512MB
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.
我们定义数组 [s1,s2,…,sk] 的前缀和之和为
s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk).
给你一棵包含 n 个顶点的树。每个顶点 i 上写有一个整数 ai。我们定义从顶点 u 到顶点 v 的简单路径的值如下:考虑从 u 到 v 的路径上出现的所有顶点,按它们在路径上出现的顺序写下这些顶点上的所有数字,然后计算所得序列的前缀和之和。
你的任务是计算树中所有路径的最大值。
输入格式
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).
第一行包含一个整数 n(2≤n≤150000)——树中顶点的数量。
接下来是 n−1 行,表示树的边。每行包含两个整数 ui 和 vi(1≤ui,vi≤n,ui=vi),表示顶点 ui 与 vi 之间存在一条边。保证这些边构成一棵树。
最后一行包含 n 个整数 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.
第一个样例中的最优路径是从顶点 3 到顶点 1。该路径给出的序列为 [3,3,7,1],其前缀和之和为 36。
输入解题思路,AI测评打分。不知道怎么写?