0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-05 06:38:48 +00:00
OI-codes/S2OJ/440/440.cpp

34 lines
633 B
C++

#include <bits/stdc++.h>
using namespace std;
int n, l, k, root, dp[6005][2], p[6005];
void dfs(int x) {
for (int i = 1; i <= n; i++) {
if (p[i] == x) {
dfs(i);
dp[x][0] += max(dp[i][0], dp[i][1]);
dp[x][1] += dp[i][0];
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> dp[i][1];
}
while (cin >> l >> k && (l != 0 && k != 0)) {
p[l] = k;
}
for (int i = 1; i <= n; i++) {
if (!p[i]) {
root = i;
}
}
dfs(root);
cout << max(dp[root][0], dp[root][1]) << endl;
return 0;
}