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. mysql order by 多个字段排序实现组内排序

    总结:大组在前,小组在后,计量值再最后,即可实现组内排序:下边是参考别人的具体实例: 工作中需用到order by 后两个字段排序,但结果却产生了一个Bug,以此备录. [1]复现问题场景 为了说明问 ...

  2. 巩固javaweb第一天

    巩固内容: 实例解析 <!DOCTYPE html> 声明为 HTML5 文档 <html> 元素是 HTML 页面的根元素 <head> 元素包含了文档的元(me ...

  3. Netty实现Socket

    Netty实现Socket 从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念 ...

  4. MyBatis常用批量方法

    <!-- 批量添加派车单子表数据 --> <insert id="addBatch" parameterType="java.util.List&quo ...

  5. ArrayList删除特定元素的方法

    最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...

  6. idea集成开发工具快捷键大全

    1  执行(run)                                                 alt+r 2  提示补全 (Class Name Completion)    ...

  7. Socket通信和多线程的总结

    1.ServerSocket进行多线程接收 package com.yh.chat; import java.io.IOException; import java.net.ServerSocket; ...

  8. 关于finally中的语句和try中的return之间的执行顺序

    首先是第一种情况: 我们这里由于程序只是单一的,所以后面的代码只有主题部分: Public class test{ Public static void main(String[] args){ Sy ...

  9. 04 - Vue3 UI Framework - 文档页

    官网的首页做完了,接下来开始做官网的文档页 返回阅读列表点击 这里 路由设计 先想想我们需要文档页通向哪些地方,这里直接给出我的设计: 所属 子标题 跳转路径 文件名(*.vue) 指南 介绍 /do ...

  10. thinkPHP的多语言支持如何配置和部署

    框架底层语言包位于:ThinkPHP/Lang/zh-cn.php 可以参考如何编写语言文件 第一步:项目目录下的配置文件中配置多语言支持的相关选项 以我的路径为例是Application\Commo ...