#include <bits/stdc++.h>
using namespace std;
#define dbg(x) x
#define prt(x) dbg(cerr << x)
#define pv(x) dbg(cerr << #x << " = " << x << '\n')
#define parr(x) dbg(cerr << #x << " = "; for (auto y : x) {cerr << y << ' ';} cerr << '\n';)
#define parr2d(x) dbg(cerr << #x << " = \n"; for (auto _ : x) {parr(_);} cerr << '\n';)
/*
if there are cycles, just don't consider them
they will stand out and not be connected to node 0
note that this is like a tree
you start from node 0 & can have multiple traversors, but you must go down the tree step by step
answer to subtask 1 is just the maximum smth idk
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; long long s;
cin >> n >> s;
vector<long long> a(n + 1), par(n + 1);
vector<vector<int>> edges(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i] >> par[i];
edges[par[i]].push_back(i);
}
vector<long long> dp(n + 1, 0);
function<void(int)> dfs = [&] (int node) {
dp[node] = a[node];
for (auto next : edges[node]) {
dp[node] += dp[next];
}
dp[node] = max(dp[node], 0ll);
};
dfs(0);
cout << dp[0] << '\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... |