D. Chloe and pleasant prizes
阅读原文时间:2023年07月09日阅读:1

D. Chloe and pleasant prizes

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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 ≤ nu__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.

Output

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.

Examples

input

8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8

output

25

input

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

output

2

input

1
-1

output

Impossible

题意:给你一棵树,以1为根节点,让你找这树的两个子树和的最大值,子树的和为该子树的各个节点值的和。

思路:dfs+dp;

先dfs求出每个子树的权值,然后dp[i]表示i为根节点,以及他的子树的权值的最小值,那么dp[i] = min(dp[i],dp[x]);x为与i相连的节点,同时,如果该节点有超过一个子节点时,取两个最大的更新答案。

复杂度O(n);

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;
10 typedef long long LL;
11 LL id[300000];
12 LL dp[300000];
13 LL pp[300000];
14 bool flag[300000];
15 vectorvec[300000];
16 int dfs(int n);
17 int ddf(int n);
18 int ac = 0;
19 LL maxxx = -1e18;
20 int main(void)
21 {
22 int n;
23 while(scanf("%d",&n)!=EOF)
24 {
25 int i,j;
26 for(i = 1; i <= n; i++) 27 scanf("%lld",&id[i]); 28 for(i = 0; i <= 200000; i++) 29 dp[i] = -1e16,vec[i].clear(); 30 memset(flag,0,sizeof(flag)); 31 for(i = 1; i < n; i++) 32 { 33 int x,y; 34 scanf("%d %d",&x,&y); 35 vec[x].push_back(y); 36 vec[y].push_back(x); 37 } 38 if(n == 1) 39 { 40 printf("Impossible\n"); 41 } 42 else 43 { 44 ddf(1); 45 memset(flag,0,sizeof(flag)); 46 dfs(1); 47 if(ac==0) 48 printf("Impossible\n"); 49 else printf("%lld\n",maxxx); 50 } 51 } 52 return 0; 53 } 54 int ddf(int n) 55 { 56 flag[n] = true; 57 for(int i = 0; i < vec[n].size(); i++) 58 { 59 int x = vec[n][i]; 60 if(!flag[x]) 61 { 62 ddf(x); 63 id[n]+=id[x]; 64 } 65 } 66 67 } 68 int dfs(int n) 69 { 70 flag[n] = true; 71 int cn = 0; 72 vectorvv;
73 dp[n] = id[n];
74 for(int i = 0; i < vec[n].size(); i++) 75 { 76 int x = vec[n][i]; 77 if(!flag[x]) 78 { 79 dfs(x); 80 vv.push_back(x); 81 dp[n] = max(dp[x],dp[n]); 82 } 83 } 84 for(int i = 0; i < vv.size(); i++) 85 { 86 pp[cn++] = dp[vv[i]]; 87 } 88 sort(pp,pp+cn); 89 if(cn > 1)
90 {
91 maxxx = max(maxxx,pp[cn-1]+pp[cn-2]);
92 ac = 1;
93 }
94 }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章