CF77C.Beavermuncher-0xFF

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

"Eat a beaver, save a tree!" — That will be the motto of ecologists' urgent meeting in Beaverley Hills.

And the whole point is that the population of beavers on the Earth has reached incredible sizes! Each day their number increases in several times and they don't even realize how much their unhealthy obsession with trees harms the nature and the humankind. The amount of oxygen in the atmosphere has dropped to 17 per cent and, as the best minds of the world think, that is not the end.

In the middle of the 50-s of the previous century a group of soviet scientists succeed in foreseeing the situation with beavers and worked out a secret technology to clean territory. The technology bears a mysterious title "Beavermuncher-0xFF". Now the fate of the planet lies on the fragile shoulders of a small group of people who has dedicated their lives to science.

The prototype is ready, you now need to urgently carry out its experiments in practice.

You are given a tree, completely occupied by beavers. A tree is a connected undirected graph without cycles. The tree consists of nn vertices, the ii -th vertex contains kik_{i} beavers.

"Beavermuncher-0xFF" works by the following principle: being at some vertex uu , it can go to the vertex vv , if they are connected by an edge, and eat exactly one beaver located at the vertex vv . It is impossible to move to the vertex vv if there are no beavers left in vv . "Beavermuncher-0xFF" cannot just stand at some vertex and eat beavers in it. "Beavermuncher-0xFF" must move without stops.

Why does the "Beavermuncher-0xFF" works like this? Because the developers have not provided place for the battery in it and eating beavers is necessary for converting their mass into pure energy.

It is guaranteed that the beavers will be shocked by what is happening, which is why they will not be able to move from a vertex of the tree to another one. As for the "Beavermuncher-0xFF", it can move along each edge in both directions while conditions described above are fulfilled.

The root of the tree is located at the vertex ss . This means that the "Beavermuncher-0xFF" begins its mission at the vertex ss and it must return there at the end of experiment, because no one is going to take it down from a high place.

Determine the maximum number of beavers "Beavermuncher-0xFF" can eat and return to the starting vertex.

输入格式

The first line contains integer nn — the number of vertices in the tree ( 1<=n<=1051<=n<=10^{5} ). The second line contains nn integers kik_{i} ( 1<=ki<=1051<=k_{i}<=10^{5} ) — amounts of beavers on corresponding vertices. Following n1n-1 lines describe the tree. Each line contains two integers separated by space. These integers represent two vertices connected by an edge. Vertices are numbered from 11 to nn . The last line contains integer ss — the number of the starting vertex ( 1<=s<=n1<=s<=n ).

输出格式

Print the maximum number of beavers munched by the "Beavermuncher-0xFF".

Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).

输入输出样例

  • 输入#1

    5
    1 3 1 3 2
    2 5
    3 4
    4 5
    1 5
    4
    

    输出#1

    6
    
  • 输入#2

    3
    2 1 1
    3 2
    1 2
    3
    

    输出#2

    2
    
首页