#include<iostream> #include<algorithm> #include<vector> #include<list> using namespace std; const int N = 6020; int n, intimacies[N]; int d[N][2]; // d[i][0] - participate, d[i][1] - not participate int boss[N]; // at first, every staff's boss is himself. Thus boss[i] = i; vector < list < int > > staffs; // vector[i] vector < pair <int, int> > staff_priority_queue; // vector[i].first = level of Tree, vectors[i].second = idx. int cmp(pair <int, int> s, pair <int, int> w){ return s.first > w.first; } void leveling_node(int node, int level){ staff_priority_queue[node].first = level; for(list<int>::iterator it = staffs[node].begin(); it != staffs[node].end(); it++){ leveling_node(*it, level + 1); } } int main(void){ cin>>n; staff_priority_queue.resize(n + 10); staffs.resize(n + 10); staff_priority_queue[0].first = -1; staff_priority_queue[0].second = -1; for(int i = 1; i<=n; i++){ cin>>intimacies[i]; staff_priority_queue[i].second = i; staff_priority_queue[i].first = -1; boss[i] = i; } int L, k, m = 0; while(1){ cin>>L>>k; if(L == 0 && k == 0)break; staffs[k].push_back(L); staff_priority_queue[k].first++; boss[L] = k; //at the end, the root of subtree only satisfy 'boss[i] == i'. m++; }; if(n == m){ cout<<102; return 0; }//마지막 데이터 말도 안되는 거 처리를 위해서.. () for(int i = 1; i<=n; i++){ if(boss[i] == i){ leveling_node(i, 0); } } int ans = -9999; sort(staff_priority_queue.begin(), staff_priority_queue.begin() + n + 1, cmp); //sorting by tree level. for(int i = 0; i<n; i++){ int sum_of_participate = 0, sum_of_not_participate = 0; int idx = staff_priority_queue[i].second; for(list<int>::iterator it = staffs[idx].begin(); it != staffs[idx].end(); it++){ if(d[*it][0] >= 0)sum_of_participate += d[*it][0]; if(d[*it][1] >= 0)sum_of_not_participate += d[*it][1]; } d[idx][0] = sum_of_not_participate + intimacies[idx]; d[idx][1] = max(sum_of_participate, sum_of_not_participate); if(ans < d[idx][0])ans = d[idx][0]; if(ans < d[idx][1])ans = d[idx][1]; } cout<<ans; return 0; }