A13183 | Sum of Prefix Sums
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
We define the sum of prefix sums of an array $[s_1, s_2, \dots, s_k]$ as $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 $n$ vertices. Each vertex $i$ has an integer $a_i$ 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.
You are given a tree consisting of $n$ vertices. Each vertex $i$ has an integer $a_i$ 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 \le n \le 150000$ ) — the number of vertices in the tree.
Then $n - 1$ lines follow, representing the edges of the tree. Each line contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ , $u_i \ne v_i$ ), denoting an edge between vertices $u_i$ and $v_i$ . It is guaranteed that these edges form a tree.
The last line contains $n$ integers $a_1$ , $a_2$ , ..., $a_n$ ( $1 \le a_i \le 10^6$ ).
Then $n - 1$ lines follow, representing the edges of the tree. Each line contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ , $u_i \ne v_i$ ), denoting an edge between vertices $u_i$ and $v_i$ . It is guaranteed that these edges form a tree.
The last line contains $n$ integers $a_1$ , $a_2$ , ..., $a_n$ ( $1 \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 $3$ to vertex $1$ . It gives the sequence $[3, 3, 7, 1]$ , and the sum of prefix sums is $36$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted