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 <iostream>
#include <vector>
#include <set>
using namespace std;
#define int long long
const int N = 3e5 + 10;
vector<int> vec[N];
int pr[N], erased[N], nxt[N];
set<pair<int,pair<int,int>>> st;
void dfs(int u, int sum = 0, int mx = 0, bool St = 1){
if (erased[u] and !St)
return;
mx = max(mx, -sum);
if (!St){
if (sum + pr[u] >= 0 and !erased[u]){
st.insert({mx, {sum + pr[u], u}});
return;
}
sum += pr[u];
}
for (int i : vec[u])
dfs(i, sum, mx, 0);
}
signed main(){
int n, s;
cin>>n>>s;
int init = s;
for (int i=1, dp;i<=n;i++){
cin>>pr[i]>>dp;
vec[dp].push_back(i);
nxt[i] = dp;
}
st.insert({0, {0, 0}});
while (st.size() > 0){
auto [req, p] = *begin(st);
auto [prft, ind] = p;
if (req > s)
break;
s += prft;
st.erase(begin(st));
while (1){
erased[ind] = 1;
dfs(ind);
if (erased[nxt[ind]])
break;
ind = nxt[ind];
}
}
cout<<s - init<<'\n';
}
# | 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... |