이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pqi priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>
#define F first
#define S second
using namespace std;
const ll maxn=1e18;
ll n,s,pp[300010],p[300010];
vector<int> e[300010];
pqi dfs(int now){
pqi pq;
for(auto x:e[now]){
pqi pq2=dfs(x);
if(pq.size()<pq2.size())swap(pq,pq2);
while(!pq2.empty()){
pq.push(pq2.top());
pq2.pop();
}
}
ll need=max(0ll,-pp[now]),pf=pp[now];
while(!pq.empty()&&pf<=0){
auto [x,y]=pq.top();
pq.pop();
need=max(need,x-pf);
pf+=y;
}
while(!pq.empty()&&pq.top().F<=need+pf){
auto [x,y]=pq.top();
need=max(need,x-pf);
pf+=y;
pq.pop();
}
if(pf>0)pq.push({need,pf});
return pq;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s;
for(int i=1;i<=n;i++){
cin>>pp[i]>>p[i];
e[p[i]].push_back(i);
}
pqi pq=dfs(0);
ll ans=s;
while(!pq.empty()&&ans>=pq.top().F){
ans+=pq.top().S;
pq.pop();
}
cout<<ans-s<<"\n";
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... |