CF633F.The Chocolate Spree

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Alice and Bob have a tree (undirected acyclic connected graph). There are aia_{i} chocolates waiting to be picked up in the ii -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?

输入格式

The first line of the input contains the single integer nn ( 2<=2<= n <=100000<=100000 ) — the number of vertices in the tree.

The second line contains nn integers aia_{i} ( 1<=ai<=1091<=a_{i}<=10^{9} ), ii -th of these numbers stands for the number of chocolates stored at the node ii .

Then follow n1n-1 lines that describe the tree. Each of them contains two integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n ) — indices of vertices connected by the ii -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 99 and Bob at vertex 88 . Alice will select vertex 11 and Bob has no options now. Alice selects the vertex 77 and they both stop.

In the second sample, both of them will pick either of the nodes alternately.

首页