#P4657. [CEOI 2017] Chase

    ID: 3593 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2017CEOI概率论,统计期望

[CEOI 2017] Chase

Description

In front of the fugitive there is a maze. The maze consists of nn rooms and n1n-1 bidirectional corridors. Each corridor connects two different rooms, and all rooms are reachable from each other through the corridors. In other words, it is a tree.

The fugitive will choose a room to enter the maze, walk through some corridors, and then leave the maze, but he will never walk through the same corridor twice.

In room ii, there are FiF_i iron balls. Whenever someone passes through this room, they will be blocked by the iron balls. The fugitive has VV magnets. When he arrives at a room, he may choose to drop one magnet (or not). This will pull all iron balls from every room adjacent to this room into this room. The process is as follows:

  1. The fugitive enters the room.
  2. The fugitive drops a magnet.
  3. The fugitive leaves the room.
  4. The iron balls are pulled into this room.

Note that the fugitive is only blocked by the iron balls originally in this room, and will not be blocked by the iron balls pulled into this room.

After the fugitive leaves the maze, the pursuer will go through the maze along the path taken by the fugitive, and he will encounter all iron balls on this path.

Please help the fugitive choose a path to maximize: (number of iron balls encountered by the pursuer) minus (number of iron balls encountered by the fugitive).

Input Format

The first line contains two space-separated integers nn and VV.

The second line contains nn space-separated integers representing FiF_i.

Then follow n1n-1 lines. Each line contains two space-separated integers xx and yy, indicating that there is a corridor connecting room xx and room yy.

Output Format

Output one integer, representing the maximum possible value of (number of iron balls encountered by the pursuer) minus (number of iron balls encountered by the fugitive) in the optimal case.

12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
36

Hint

Sample Explanation

One optimal plan is as follows:

  • Enter the maze from room 66 and drop the first magnet. He encounters 55 iron balls. At this time, room 66 will have 2727 iron balls, while rooms 55, 77, 88, and 99 will have no iron balls.
  • Walk to room 77, drop the second magnet, and leave the maze. He encounters 00 iron balls. At this time, room 77 will have 4141 iron balls, while rooms 22, 44, 66, and 1010 will have no iron balls.

In this process, the fugitive encounters 55 iron balls, while the pursuer encounters 4141 iron balls.

Constraints

For 100%100\% of the testdata, 1n105;0V100;0Fi1091\le n\le 10^5;0\le V\le 100;0\le F_i\le 10^9​​。

Translated by ChatGPT 5