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"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
const int N = 2e3 + 5;
const int INF = 1e18;
int n,s;
vector<int> v[N];
int par[N],ar[N],vis[N];
int bak=-1,xd=0;
void dfs(int c=0LL,int mini=0LL,int cur=0LL){
if(mini<-s) return;
if(mini>=-s && cur>=xd && !vis[c]){
xd=cur;
bak=c;
}
for(int x:v[c]) dfs(x,min(mini,cur+ar[x]),cur+ar[x]);
}
void _(){
cin >> n >> s;
int lol=s;
for(int i=1;i<=n;i++){
cin >> ar[i] >> par[i];
v[par[i]].push_back(i);
}
vis[0]=1;
while(true){
bak=-1;xd=0;
dfs();
if(bak==-1) break;
int c=bak;
while(c>0){
s+=ar[c];
ar[c]=0;
vis[c]=1;
c=par[c];
}
}
cout << s-lol << '\n';
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int tc=1;//cin >> tc;
while(tc--) _();
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... |