#include<cstdio>#include<vector>usingnamespace std;
vector<int> v[6005];int h[6005], flag[6005], dp[6005][5], root;intMax(int x,int y){return x > y ? x : y;}voidDp(int root){
dp[root][0]=0;
dp[root][1]= h[root];for(int i =0; i < v[root].size(); i ++){int son = v[root][i];Dp(son);
dp[root][0]+=Max(dp[son][1], dp[son][0]);
dp[root][1]+= dp[son][0];}}intmain(){int n, x, y;scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&h[i]);for(int i =1; i <= n -1; i ++){scanf("%d %d",&x,&y);
v[y].push_back(x);
flag[x]=1;}for(int i =1; i <= n; i ++){if(!flag[i]){
root = i;break;}}Dp(root);printf("%d",Max(dp[root][1], dp[root][0]));}
#include<cstdio>#include<vector>#include<iostream>usingnamespace std;int dp[105], tot, n, ans1, ans2;bool flag[105];
vector<int> v[105];intMax(int x,int y){return x > y ? x : y;}intMin(int x,int y){return x < y ? x : y;}voidDP(int root){bool f =0;
dp[root]=1;for(int i =0; i < v[root].size(); i ++){int son = v[root][i];if(!flag[son]){
flag[son]=1;DP(son);
dp[root]+= dp[son];if(dp[son]> n /2){ f =1;}}}if(n - dp[root]> n /2){ f =1;}if(!f){
tot ++;if(!ans1) ans1 = root;else ans2 = root;}}intmain(){int x, y, ans =-0x7f7f7f7f;scanf("%d",&n);for(int i =1; i <= n -1; i ++){scanf("%d %d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);}
flag[1]=1;DP(1);printf("%d\n", tot);if(tot ==1)printf("%d", ans1);elseprintf("%d\n%d",Min(ans1, ans2),Max(ans1, ans2));}
#include<cstdio>#include<vector>usingnamespace std;bool flag[200005];int dp[200005], head[200005], to[200005], next[200005], tot, ans;intMax(int x,int y){return x > y ? x : y;}intMin(int x,int y){return x < y ? x : y;}voidadd(int x,int y){
to[++tot]= y;
next[tot]= head[x];
head[x]= tot;}voidDP(int x){for(int i = head[x]; i; i = next[i]){int t = to[i];if(flag[t])continue;
flag[t]=1;DP(t);
ans =Max(ans, dp[x]+ dp[t]+1);
dp[x]=Max(dp[x], dp[t]+1);}}intmain(){int n, x, y, ansz =-1;scanf("%d",&n);for(int i =1; i <= n -1; i ++){scanf("%d %d",&x,&y);add(x, y);add(y, x);}
flag[1]=1;DP(1);printf("%d", ans);}