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 ai — 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 a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the pleasantness of the gifts.

The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.

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<stdio.h>
2 #include<math.h>
3 #include<queue>
4 #include<algorithm>
5 #include<string.h>
6 #include<iostream>
7 #include<stack>
8 #include<vector>
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 vector<int>vec[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 vector<int>vv;
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 }

D. Chloe and pleasant prizes的更多相关文章

  1. coderforces #384 D Chloe and pleasant prizes(DP)

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp

    D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...

  3. Chloe and pleasant prizes

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. Codeforces 743D Chloe and pleasant prizes(树型DP)

                                                                D. Chloe and pleasant prizes             ...

  5. CodeForces - 743D Chloe and pleasant prizes

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]

    这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...

  8. Codeforces 743D:Chloe and pleasant prizes(树形DP)

    http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...

  9. D. Chloe and pleasant prizes 树上dp + dfs

    http://codeforces.com/contest/743/problem/D 如果我们知道mx[1]表示以1为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...

随机推荐

  1. 深度探讨 PHP 之性能

    1.缘起 关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言,库类丰富,使用简单,安全,非常适合WEB开发,但性能低下.PHP的性能是否真的就 如同大家的感觉一样的差呢?本文就是围绕这么一个话题 ...

  2. java.sql.SQLException: Cannot create com._51doit.pojo.User: com._51doit.pojo.User Query: select * from user where username = ? and password = ? Parameters: [AA, 123]

    在从数据库中查询数据,并存入javabean中去时,报这个错误 原因:在建立User类去存储信息时没有创建无参构造方法,创建一个无参构造方法即可

  3. HTTP 之 options预请求

    一.HTTP一共有八种常见请求方法 get:参数在url上,浏览器长度有限制,不安全 post:参数不可见,长度不受限制 put:上传最新内容到指定位置 delete:删除请求的url所表示的资源 h ...

  4. [php安全]原生类的利用

    php原生类的利用 查看原生类中具有魔法函数的类 $classes = get_declared_classes(); foreach ($classes as $class) { $methods ...

  5. C++构造函数和析构函数初步认识

    构造函数 1.构造函数与类名相同,是特殊的公有成员函数.2.构造函数无函数返回类型说明,实际上构造函数是有返回值的,其返回值类型即为构造函数所构建到的对象.3.当新对象被建立时,构造函数便被自动调用, ...

  6. Cocoapods 版本更新与更新到指定版本

    1.本地现有的Cocoapods的版本号是1.1.0.rc.2,想升级到最新版本 1.先切换gem源 gem sources --remove https://rubygems.org/ gem so ...

  7. IOS_UIButton去掉系统的按下高亮置灰效果

    setAdjustsImageWhenHighlighted   // default is YES. if YES, image is drawn darker when highlighted(p ...

  8. Javaj基础知识runtime error

    遇到的java 运行时错误: NullPointerException空指针  ,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始 ...

  9. 类型类 && .class 与 .getClass() 的区别

    一. 什么是类型类 Java 中的每一个类(.java 文件)被编译成 .class 文件的时候,Java虚拟机(JVM)会为这个类生成一个类对象(我们姑且认为就是 .class 文件),这个对象包含 ...

  10. @Order注解使用

    注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: @ ...