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;
using ll = long long;
int main() {
ll n, s;
cin >> n >> s;
vector<vector<int>> graph(n);
vector<int> val(n), up(n);
for (int i = 0; i < n; i++) {
cin >> val[i] >> up[i];
if (up[i]--)
graph[up[i]].push_back(i);
}
function<ll(int)> dfs = [&](int v) {
ll best = val[v];
for (auto ne : graph[v])
best += dfs(ne);
return max(0LL, best);
};
ll ans = 0;
for (int i = 0; i < n; i++)
if (up[i] == -1)
ans += dfs(i);
cout << ans << endl;
}
# | 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... |