#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;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> s;
x = p = vec<int>(N + 1);
vec<int> v, nxt(N + 1);
for (int i = 1; i <= N; i++) {
cin >> x[i] >> p[i];
if (!p[i]) v.PB(i);
else nxt[p[i]] = i;
}
ll ans = 0;
for (int i : v) {
ll res = 0, sum = 0;
for (int j = i; j != 0; j = nxt[j]) {
res = max(res, sum);
sum += x[j];
}
ans = max(ans, ans + res);
}
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... |