A10352 | The Chocolate Spree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alice and Bob have a tree (undirected acyclic connected graph). There are $a_{i}$ chocolates waiting to be picked up in the $i$ -th vertex of the tree. First, they choose two different vertices as their starting positions (Alice chooses first) and take all the chocolates contained in them.
Then, they alternate their moves, selecting one vertex at a time and collecting all chocolates from this node. To make things more interesting, they decided that one can select a vertex only if he/she selected a vertex adjacent to that one at his/her previous turn and this vertex has not been already chosen by any of them during other move.
If at any moment one of them is not able to select the node that satisfy all the rules, he/she will skip his turns and let the other person pick chocolates as long as he/she can. This goes on until both of them cannot pick chocolates any further.
Due to their greed for chocolates, they want to collect as many chocolates as possible. However, as they are friends they only care about the total number of chocolates they obtain together. What is the maximum total number of chocolates they may pick?
Then, they alternate their moves, selecting one vertex at a time and collecting all chocolates from this node. To make things more interesting, they decided that one can select a vertex only if he/she selected a vertex adjacent to that one at his/her previous turn and this vertex has not been already chosen by any of them during other move.
If at any moment one of them is not able to select the node that satisfy all the rules, he/she will skip his turns and let the other person pick chocolates as long as he/she can. This goes on until both of them cannot pick chocolates any further.
Due to their greed for chocolates, they want to collect as many chocolates as possible. However, as they are friends they only care about the total number of chocolates they obtain together. What is the maximum total number of chocolates they may pick?
输入格式
The first line of the input contains the single integer $n$ ( $2<=$ n $<=100000$ ) — the number of vertices in the tree.
The second line contains $n$ integers $a_{i}$ ( $1<=a_{i}<=10^{9}$ ), $i$ -th of these numbers stands for the number of chocolates stored at the node $i$ .
Then follow $n-1$ lines that describe the tree. Each of them contains two integers $u_{i}$ and $v_{i}$ ( $1<=u_{i},v_{i}<=n$ ) — indices of vertices connected by the $i$ -th edge.
The second line contains $n$ integers $a_{i}$ ( $1<=a_{i}<=10^{9}$ ), $i$ -th of these numbers stands for the number of chocolates stored at the node $i$ .
Then follow $n-1$ lines that describe the tree. Each of them contains two integers $u_{i}$ and $v_{i}$ ( $1<=u_{i},v_{i}<=n$ ) — indices of vertices connected by the $i$ -th edge.
输出格式
Print the number of chocolates Alice and Bob can collect together if they behave optimally.
输入输出样例
输入 #1
9 1 2 3 4 5 6 7 8 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9
输出 #1
25
输入 #2
2 20 10 1 2
输出 #2
30
In the first sample, Alice may start at the vertex $9$ and Bob at vertex $8$ . Alice will select vertex $1$ and Bob has no options now. Alice selects the vertex $7$ and they both stop.
In the second sample, both of them will pick either of the nodes alternately.
In the second sample, both of them will pick either of the nodes alternately.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted