/* Question: the legendary Ural God school will celebrate its 80 th anniversary. The school's staff relationship is a tree where each employee has
Pleasure value, indicating the degree of fun to participate in the party. Every employee does not want to join his or her boss.
Party. Then the principal thought about how to send invitations to make the party the highest.
Idea: Define FW [I] to indicate the maximum pleasure of the subtree when the root node is added, and FN [I] to indicate that the root node is not included.
The maximum pleasure of a subtree. The transfer equation is:
FW [I] = sum (FN [son [I]);
FN [I] = sum (max (FN [son [I], FW [son [I]);
In fact, it is easy to implement and draw a picture at a glance. */
// PS: 1y, ^ _ ^
// My code:
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <vector>
Using namespace STD;
Const int n= 6010;
Vector <int> G [N];
Int FW [N], FN [N], p [N];
Bool vis [N];
Void DFS (int r ){
If (vis [R]) return;
Vis [R] = true;
Int Len = G [R]. Size ();
If (! Len ){
FN [R] = 0;
FW [R] = P [R];
Return;
}
Int I, C;
For (I = 0; I <Len; I ++ ){
C = G [r] [I];
DFS (C );
FN [R] + = max (FN [c], FW [c]);
FW [R] + = FN [c];
}
FW [R] + = P [R];
}
Int main (){
// Freopen ("data. In", "r", stdin );
Int n, x, y, I, ans;
While (~ Scanf ("% d", & N )){
For (I = 0; I <= N; I ++) g [I]. Clear ();
For (I = 1; I <= N; I ++ ){
Scanf ("% d", P + I );
}
While (scanf ("% d", & X, & Y), X | Y ){
G [Y]. push_back (X );
}
Memset (FN, 0, sizeof (FN ));
Memset (FW, 0, sizeof (FW ));
Memset (VIS, false, sizeof (VIS ));
For (I = 1; I <= N; I ++ ){
If (! Vis [I]) DFS (I );
}
For (ANS = 0, I = 1; I <= N; I ++ ){
Ans = max (ANS, max (FW [I], FN [I]);
}
Printf ("% d \ n", ANS );
}
Return 0;
}