-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathD.cpp
56 lines (49 loc) · 1.22 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
typedef long long ll;
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl;
#define F first
#define S second
#define MEM(a,b) memset( a, b, sizeof a );
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
ll a[200005];
vector<ll> v[200005];
ll MaxVal;
ll INF = LLONG_MAX/100;
pair<ll,ll> dfs( ll x, ll p ) {
ll val = a[x];
vector<ll> idx;
for( ll q : v[x] )
if( q != p ) {
auto d = dfs( q, x );
idx.pb(d.S);
val += d.F;
}
sort( idx.rbegin(), idx.rend() );
if( idx.size() >= 2 )
MaxVal = max( MaxVal, idx[0] + idx[1] );
if( idx.empty() )
idx.pb(val);
return { val, max(idx[0],val) } ;
}
int main() {
ll n; cin >> n;
FOR(i,1,n) cin >> a[i];
FOR(i,1,n-1) {
ll x, y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
MaxVal = -INF;
dfs(1,-1);
if( MaxVal == -INF )
cout << "Impossible";
else
cout << MaxVal;
}