This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5+5;
typedef long long ll;
const ll inf = 1e18;
int n;
vector<int> nxt[MAXN];
ll val[MAXN];
ll mx_val[MAXN];
ll dfs(int cur) {
ll res = 0;
for (int v: nxt[cur]) {
res += dfs(v);
}
return max(0ll,res+val[cur]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll s;
cin >> n >> s;
assert(s == inf);
vector<int> rts;
for (int i = 0; i < n; i++) {
int p; cin >> val[i] >> p;
if (p == 0) rts.push_back(i);
else nxt[p-1].push_back(i);
}
ll ans = 0;
for (int i: rts) ans += dfs(i);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |