1. 程式人生 > 實用技巧 >[CF743D] Chloe and pleasant prizes(樹形dp)

[CF743D] Chloe and pleasant prizes(樹形dp)

【原題】

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

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

【題意】
求兩個不交錯的子樹的最大權值和。

【思路】

樹形dp,求出當前節點下的最大子樹和和次大子樹和,並將最大子樹和和以當前節點為根的子樹和比較,這個最大值傳給其父節點繼續比較。

AC程式碼:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int LL x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn = 2e5 + 7;
const int maxm = 1e9 + 7;
const int mod = 1e9 + 7;
LL n, a[maxn], w[maxn], mx[maxn], submx[maxn], ans = -INF;
vector<int> G[maxn];

LL build(int u, int f)
{
	w[u] = a[u];
	LL tmp;
	f(i, 0, G[u].size())
	{
		int v = G[u][i];
		if (v == f) continue;
		LL tmp = build(v, u);
		w[u] += w[v];
		if (tmp >= mx[u])
		{
			submx[u] = mx[u];
			mx[u] = tmp;
		}
		else if (tmp > submx[u])
		{
			submx[u] = tmp;
		}
		if (submx[u] > -INF) ans = max(ans, mx[u] + submx[u]);
	}
	if (w[u] > mx[u]) mx[u] = w[u];
	return mx[u];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	_rep(i, 1, n)
	{
		cin >> a[i];
		mx[i] = submx[i] = -INF;
	}
	int ta, tb;
	_rep(i, 1, n - 1)
	{
		cin >> ta >> tb;
		G[ta].push_back(tb);
		G[tb].push_back(ta);
	}
	int yz = 0;
	_rep(i, 2, n)
	{
		if (G[i].size() == 1) yz++;
	}
	if (yz < 2)
	{
		cout << "Impossible" << endl;
		return 0;
	}
	build(1, -1);
	cout << ans << endl;
}

WA程式碼:儲存每個節點的最大子樹和,第二次dfs找子節點數>1的點,計算其子節點的最大和次大子樹和,忽略了最大和次大都在一個子節點之下的情況。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int LL x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn = 2e5 + 7;
const int maxm = 1e9 + 7;
const int mod = 1e9 + 7;
LL n, a[maxn], w[maxn], p[maxn], mxx[maxn];
vector<int> G[maxn];
int build(int u, int f)
{
	w[u] = a[u];
	f(i, 0, G[u].size())
	{
		int v = G[u][i];
		if (v == f) continue;
		build(v, u);
		if (mxx[v] > mxx[u])
		{
			mxx[u] = mxx[v];
			p[u] = p[v];
		}
		w[u] += w[v];
	}
	if (w[u] > mxx[u])
	{
		mxx[u] = w[u];
		p[u] = u;
	}
	return 0;
}
LL dfs(int u, int f)
{
	int num = 0, son = 0;
	LL mx = -INF, submx = -INF;
	f(i, 0, G[u].size())
	{
		int v = G[u][i];
		if (v == f) continue;
		num++;
		son = v;
		if (mxx[v] >= mx)
		{
			submx = mx;
			mx = mxx[v];
		}
		else if (mxx[v] > submx)
		{
			submx = w[v];
		}
	}
	if (!son) return mxx[u];
	if (num >= 2) return submx + mx;
	else return (dfs(son, u));
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	_rep(i, 1, n)
	{
		cin >> a[i];
		mxx[i] = -INF;
	}
	int ta, tb;
	_rep(i, 1, n - 1)
	{
		cin >> ta >> tb;
		G[ta].push_back(tb);
		G[tb].push_back(ta);
	}
	int yz = 0;
	_rep(i, 2, n)
	{
		if (G[i].size() == 1) yz++;
	}
	if (yz < 2)
	{
		cout << "Impossible" << endl;
		return 0;
	}
	build(1, -1);
	cout << dfs(1, -1) << endl;
}