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;
#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 1e6;
const ll inf = 1e18;
vector<int> g[N];
ll dp[N], x[N], p[N];
void dfs(int u) {
dp[u] = x[u];
for (int to: g[u]) {
dfs(to);
dp[u] += dp[to];
}
dp[u] = max(dp[u], 0ll);
}
void solve() {
int n;
ll s;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> x[i] >> p[i];
g[p[i]].push_back(i);
}
x[0] = 0;
dfs(0);
cout << dp[0] + s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
# | 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... |