#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define all(x) begin(x), end(x)
using ll = long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
template<typename T>
using vec = vector<T>;
template<typename T, size_t N>
using arr = array<T, N>;
const ll INF = 1e17;
const int MOD = 1e9 + 7;
int N;
ll s;
vec<int> x, p;
vec<vec<int>> adj;
ll dfs(int u) {
ll res = 0;
for (int v : adj[u]) {
res = max(res, res + dfs(v));
}
return res + x[u];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> s;
x = p = vec<int>(N + 1);
adj.resize(N + 1);
vec<int> v;
for (int i = 1; i <= N; i++) {
cin >> x[i] >> p[i];
if (p[i] != 0) adj[p[i]].PB(i);
else v.PB(i);
}
ll ans = 0;
for (int i : v) ans += max(0ll, 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... |