-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Sum_in_the_tree.cpp
62 lines (47 loc) · 1.03 KB
/
A_Sum_in_the_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// RunAt - Green
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+1;
vector<vector<long long>> children(N);
vector<long long> sum(N);
vector<long long> values(N);
void dfs (long long curr, long long par) {
long long sum_parent = 0;
if (par != -1) sum_parent = sum[par];
if (sum[curr] == -1) {
long long mini = 2e9;
for (auto child: children[curr]) {
mini = min(mini, sum[child]);
}
if (children[curr].empty()) mini = sum_parent;
sum[curr] = mini;
}
values[curr] = sum[curr] - sum_parent;
for (auto child: children[curr]) {
dfs(child, curr);
}
}
void solve() {
int n; cin >> n;
for (int i=2; i<=n; i++) {
int parent; cin >> parent;
children[parent].push_back(i);
}
for (int i=1; i<=n; i++) {
cin >> sum[i];
}
dfs(1,-1);
long long total = 0;
for (int i=1; i<=n; i++) {
if (values[i] < 0) {
cout << "-1\n";
return;
}
total += values[i];
}
cout << total << "\n";
}
int main() {
cin.sync_with_stdio(0); cin.tie(0);
solve();
}