codeforces 743D Chloe and pleasant prizes (DFS/树形DP)

D. Chloe and pleasant prizes

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 ton). A gifti is characterized by integerai — pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift1 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 withn 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, printImpossible.

Input

The first line contains a single integern (1 ≤ n ≤ 2·105) — the number of gifts.

The next line contains n integersa1, 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 integersui and vi (1 ≤ ui, vi ≤ n,ui ≠ 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
题目大意:从一棵树中求出最大的两个不相交子树和,权值可能为负
解题思路:首先告诉你 n-1 条边来建一个树,不要被题目迷惑,最终树的情况是唯一的。然后从根节点开始向下遍历,依次求出当前节点为根的树的权值和 sum[ i ] ,还有其最大的两个子树和(这里的处理比较巧妙),递归实现。然后每次更新当前答案。注意 a 数组的两个含义。
AC代码:
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 5005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

const ll INF = (ll)1e18 + 1;
const int N = 300300;
int n;
ll ans = -INF;
ll a[N];
ll sum[N];//储存每个节点的权值加上所有子树和
vector<int> g[N];//储存每个节点的
bool used[N];//建树时使用,表明是否访问

void dfs(int u)
{
    used[u] = 1;
    sum[u] = a[u];
    a[u] = -INF;
    ll m1 = -INF , m2 = -INF;
    for(int i = 0; i<g[u].size(); i++)
    {
        int v = g[u][i];
        if(used[v]) continue;
        dfs(v);
        sum[u] += sum[v];
        ll val = a[v];
        a[u] = max(a[u], val);//储存最大子树和
        if(m1 < val)    swap(m1,val);//这两行比较精彩,很简洁的找到序列中最大的两个数
        if(m2 < val)    swap(m2,val);//这两步是用来保留根节点最大的两个子树和
    }
    if(m2 > -INF)    ans = max(ans, m1 + m2);//如果有两个子树和 更新答案
    a[u] = max(a[u], sum[u]);//更新a[i] 表示为根节点为u的树和其所有子树的中最大的和
}
int main()
{
    //freopen("test.txt","r",stdin);
    scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%lld", &a[i]);
    for(int i = 1; i<n; i++)
    {
        int u,v;
        cin>>u>>v;
        v--;u--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0);
    if(ans==-INF)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;
    return 0;
}

/*

unsigned   int   0~4294967295
int   2147483648~2147483647
unsigned long 0~4294967295
long   2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值