47 lines
862 B
C++
47 lines
862 B
C++
|
#include <cstring>
|
||
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
using namespace std;
|
||
|
|
||
|
const int MAXN = 1e5 + 5;
|
||
|
|
||
|
vector<int> tree[MAXN];
|
||
|
int amount[MAXN];
|
||
|
int mem[MAXN][2];
|
||
|
int N, Rt;
|
||
|
|
||
|
int dfs(int node, int flag) {
|
||
|
if (mem[node][flag] > -1) {
|
||
|
return mem[node][flag];
|
||
|
}
|
||
|
|
||
|
mem[node][0] = 0;
|
||
|
mem[node][1] = amount[node];
|
||
|
|
||
|
for (int i = 0; i < tree[node].size(); i++) {
|
||
|
int leaf = tree[node][i];
|
||
|
mem[node][0] += max(dfs(leaf, 0), dfs(leaf, 1));
|
||
|
mem[node][1] += dfs(leaf, 0);
|
||
|
}
|
||
|
|
||
|
return mem[node][flag];
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
// Init
|
||
|
cin >> N;
|
||
|
memset(mem, -1, sizeof mem);
|
||
|
for (int i = 0; i < N; i++) {
|
||
|
int depends, idx, c;
|
||
|
cin >> depends >> idx >> c;
|
||
|
amount[idx] = c;
|
||
|
if (depends == 0) {
|
||
|
Rt = idx;
|
||
|
} else {
|
||
|
tree[depends].push_back(idx);
|
||
|
}
|
||
|
}
|
||
|
// Calculating!
|
||
|
printf("%d", max(dfs(Rt, 1), dfs(Rt, 0)));
|
||
|
}
|