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;
typedef long long ll;
vector<ll> profit;
vector<set<ll>> children;
ll profit_dp(ll node) {
    // childrens_profit = sum of positive profits of children
    // return max(profit[node] + childrens_profit, 0)
    ll cp = 0;
    for(auto child : children[node]) {
        cp += max(profit_dp(child), 0ll);
    }
    return max(profit[node] + cp, 0ll);
}
int main() {
    ll n, s;
    cin >> n >> s;
    if (s == 1000000000000000000ll) {
        profit.resize(n+1);
        children.resize(n+1);
        
        for(ll i = 0 ; i < n; i++) {
            ll x, p;
            cin >> x >> p;
            profit[i+1] = x;
            children[p].insert(i+1);
        }
        cout << profit_dp(0) << endl;
        return 0;
    }
}
| # | 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... |