-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path743D
56 lines (50 loc) · 1.13 KB
/
743D
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
//I_love_StarnyC
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define f first
#define s second
#define pb push_back
#define fix cout<<std::fixed;
#define mk make_pair
#define mod 1000000007
#define time cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
vector <ll> v[200005];
ll vis[200005],val[200005],a[200005],b[200005];
ll m=-1e18;
void dfs(ll n)
{ ll g=-1e18;
if(vis[n]==1)return ;
vis[n]=1;
vector <ll> d;
for(auto i:v[n])
{
if(vis[i]==0){dfs(i);
a[n-1]+=a[i-1];
g=max(g,val[i-1]);
d.pb(val[i-1]);
}
}
sort(d.rbegin(),d.rend());
if(d.size()>=2)
m=max(m,d[0]+d[1]);
val[n-1]=max(g,a[n-1]);
}
int main()
{
ll n;cin>>n;
for(int i=0;i<n;i++)
{cin>>a[i];b[i]=a[i];}
for(int i=0;i<n-1;i++)
{
int a,b;cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
dfs(1);
if(m==-1e18)cout<<"Impossible";
else
cout<<m;
}