N Ural University has a staff of No. 1 ~ N.
Their relationship is like a tree rooted to the principal, the parent node is the direct supervisor of child nodes.
Each employee has a happiness index, an integer Hi
given, which 1≤i≤N
。
Now held an anniversary banquet, however, no direct supervisor and staff willing participants together.
Under the premise of meeting this condition, the organizers would like to invite participants part of the staff, so that the sum of all participants happiness index largest staff, seeking the maximum.
Input Format
A first line integer N.
Next N lines, i-th row i represents the number of staff happiness index Hi
。
Then N-1 lines, each pair of input integer L, K, L represents a direct supervisor K.
Output Format
The maximum output of the happiness index.
data range
1≤N≤6000
,
−128≤Hi≤127
Sample input:
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
Sample output:
5
#include<bits/stdc++.h>
using namespace std;
const int N=6010;
int f[N][2];//0代表不选 1代表选
int w[N];
int n;
int h[N],idx,ne[N],e[N];
bool st[N];
bool st2[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
f[u][1]=w[u];
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j);
f[u][0]+=max(f[j][0],f[j][1]);
f[u][1]+=f[j][0];
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n;
for(int i=1;i<=n;i++)
cin>>w[i];
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
add(b,a);
st[a]=1;
}
int t=1;
while(st[t])
{
t++;
}
// cout<<t<<endl;
dfs(t);
cout<<max(f[t][0],f[t][1])<<endl;
return 0;
}