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 ll long long
const int nx=3e5+5;
ll n, s, x[nx], pa[nx], res;
vector<ll> d[nx];
multiset<pair<ll, ll>> ms[nx];
void dfs(int u)
{
for (auto v:d[u])
{
dfs(v);
if (ms[v].size()>ms[u].size()) swap(ms[v], ms[u]);
for (auto y:ms[v]) ms[u].insert(y);
}
if (x[u]>=0) ms[u].insert({0, x[u]});
else
{
ll mn=x[u], vl=x[u];
while (vl<0&&!ms[u].empty())
{
vl-=ms[u].begin()->first;
mn=min(mn, vl);
vl+=ms[u].begin()->first+ms[u].begin()->second;
ms[u].erase(ms[u].begin());
}
if (vl>=0)
{
mn=-mn;
while (!ms[u].empty()&&ms[u].begin()->first<=mn) vl+=ms[u].begin()->second, ms[u].erase(ms[u].begin());
ms[u].insert({mn, vl});
}
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>s;
for (int i=1; i<=n; i++) cin>>x[i]>>pa[i], d[pa[i]].push_back(i);
dfs(0);
for (auto t:ms[0]) if (s+res>=t.first) res+=t.second;
cout<<res;
}
# | 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... |