이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
* author : Apiram
* created: 11.08.2024 14:11:10
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n,s;cin>>n>>s;
vector<pair<long long,long long>>arr(n + 1);
vector<vector<long long>>adj(n + 1);
for (long long i = 1;i<=n;++i){
cin>>arr[i].first>>arr[i].second;
adj[arr[i].second].push_back(i);
}
vector<pair<long long,long long>>pos;
function<int(long long,long long,long long,long long)>dfs = [&](long long u,long long mn,long long cost,long long got){
int ans = 0;
if (cost - got > 0){
pos.push_back({mn - got,cost - got});
got = cost;
ans++;
}
for(auto x:adj[u]){
if (ans){
got = cost;
}
ans += dfs(x,min(mn,cost + arr[x].first),cost + arr[x].first,got);
}
return ans;
};
dfs(0,0,0,0);
sort(pos.rbegin(),pos.rend());
long long k = s;
for (auto x:pos){
//cout<<x.first<<" "<<x.second<<'\n';
if (x.first + k < 0)continue;
k+=x.second;
}
cout<<k - 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... |