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;
#ifdef DEBUG
auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X) cout<<"["#X"]"<<X<<endl;
#else
#define debug(X) {}
#endif
const long long INF = numeric_limits<long long>::max();
const int MAXN = 2001;
vector<vector<int> > graph(MAXN);
vector<long long> cost(MAXN);
vector<vector<pair<int, long long> > > pool(MAXN);
void dfs(int b)
{
priority_queue<pair<long long, int> > q;
for(auto v : graph[b]) {dfs(v); q.push({pool[v].back().second, v});};
vector<pair<int, long long> > myP = {{cost[b], 0}};
while(!q.empty())
{
auto a = q.top().second;
q.pop();
myP.push_back({pool[a].back().first, 0});
pool[a].pop_back();
if(pool[a].size() > 0 && pool[a].back().second != INF) q.push({pool[a].back().second, a});
}
long long checkP = -1, akt = 0;
for(int i=0;i<myP.size();i++)
{
akt += myP[i].first;
if(akt >= 0)
{
akt -= myP[i].first;
for(int j=checkP+1;j<=i;j++)
{
myP[j].second = akt;
if(myP[j].first >= 0) myP[j].second = 0;
akt -= myP[j].first;
}
checkP = i;
akt = 0;
}
}
for(int i=checkP+1;i<myP.size();i++) if(myP[i].first < 0) myP[i].second = -INF;
reverse(myP.begin(), myP.end());
debug(b);
debug(myP);
pool[b] = myP;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, s;
cin>>n>>s;
assert(n <= 2000);
for(int i=1;i<=n;i++)
{
long long c, p;
cin>>c>>p;
graph[p].push_back(i);
cost[i] = c;
}
dfs(0);
long long org = s;
while(pool[0].size())
{
debug(pool[0].back());
if(pool[0].back().second + s < 0) break;
if(s + pool[0].back().first >= 0) {s += pool[0].back().first; pool[0].pop_back();}
}
cout<<s - org;
}
Compilation message (stderr)
Main.cpp: In function 'void dfs(int)':
Main.cpp:29:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i=0;i<myP.size();i++)
| ~^~~~~~~~~~~
Main.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=checkP+1;i<myP.size();i++) if(myP[i].first < 0) myP[i].second = -INF;
| ~^~~~~~~~~~~
# | 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... |