CF743D.Chloe and pleasant prizes
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer a__i — pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
为 Chloe 和 Vladik 参加的奥林匹克竞赛慷慨赞助的赞助商,允许所有参赛者自行选择奖品。圣诞节即将来临,因此赞助商决定用这些奖品来装饰圣诞树。
他们为参赛者准备了 n 件奖品,并在每件奖品上写了一个唯一的编号(从 1 到 n 的整数)。奖品 i 由一个整数 ai 表征——即该奖品的“愉悦值”。愉悦值可以为正、为负或为零。赞助商将编号为 1 的奖品置于树顶。其余所有奖品均通过绳子悬挂在某件其他奖品之下,使得每件奖品最终都通过若干绳子和中间奖品间接悬挂在编号为 1 的奖品之下。形式上,这些奖品构成了一棵含 n 个顶点的有根树。
颁奖过程如下:参赛者依次来到树前,每人从剩余奖品中任选一件,并剪断悬挂该奖品的那根绳子。注意:所有用于将其他奖品悬挂在所选奖品之下的绳子不会被剪断。因此,该参赛者获得所选奖品,以及所有直接或间接悬挂在它之下的奖品(即以它为根的子树中的全部奖品)。
我们的朋友 Chloe 和 Vladik 在本次奥林匹克竞赛中并列第一,他们将同时选择奖品!为避免争执,他们决定各自选择两件不同的奖品,使得以 Chloe 所选奖品为根的子树与以 Vladik 所选奖品为根的子树互不相交。换言之,不存在任何一件奖品,它既悬挂在 Chloe 所选奖品之下,又悬挂在 Vladik 所选奖品之下。在所有满足条件的选择方案中,他们将选择使两人最终获得的所有奖品的愉悦值之和尽可能大的一对奖品。
请输出 Vladik 和 Chloe 能获得的最大愉悦值总和。若他们无法在不争执的前提下选择奖品,则输出 Impossible。
输入格式
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of gifts.
The next line contains n integers _a_1, _a_2, ..., a__n ( - 109 ≤ a__i ≤ 109) — the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers u__i and v__i (1 ≤ u__i, v__i ≤ n, u__i ≠ v__i) — the description of the tree's edges. It means that gifts with numbers u__i and v__i are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: v__i hangs on u__i or u__i hangs on v__i.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
第一行包含一个整数 n(1≤n≤2⋅105)—— 礼物的数量。
第二行包含 n 个整数 a1,a2,…,an(−109≤ai≤109)—— 各礼物的愉悦值。
接下来的 n−1 行,每行包含两个整数。其中第 i 行包含整数 ui 和 vi(1≤ui,vi≤n,且 ui=vi)—— 描述树的边。这表示编号为 ui 和 vi 的礼物通过一根绳子相连。在绳子的描述中,礼物的编号顺序是任意的:可能是 vi 悬挂在 ui 下,也可能是 ui 悬挂在 vi 下。
保证所有礼物均通过若干根绳子及其它礼物,最终悬挂在第一个礼物之下。
输出格式
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer — the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
如果 Chloe 和 Vladik 能够选择奖品而不发生争执,则输出一个整数——他们共同能获得的最大愉悦值之和。
否则输出 Impossible。
输入输出样例
输入#1
8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8
输出#1
25
输入#2
4 1 -5 1 1 1 2 1 4 2 3
输出#2
2
输入#3
1 -1
输出#3
Impossible
输入解题思路,AI测评打分。不知道怎么写?