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 F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3e5+5;
int a[N],n,dp[N],c[N],p[N];
vector<vector<int>> ps,pm,g;
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>a[0];
int ans=a[0];
for (int i=1;i<=n;i++){
cin>>a[i]>>p[i];
c[p[i]]=i;
}
priority_queue<vector<int>> pq;
for (int i=1;i<=n;i++){
if (p[i]) continue;
vector<int> b;
b.pb(a[i]);
int x=i;
while (c[x]){
x=c[x];
b.pb(a[x]);
}
ps.pb({});pm.pb({});g.pb({});
g.back().resize(b.size(),-1);
ps.back().pb(b[0]);
pm.back().pb(b[0]);
for (int i=1;i<b.size();i++){
ps.back().pb(ps.back().back()+b[i]);
pm.back().pb(min(pm.back().back(),ps.back().back()));
}
stack<int> st;
for (int i=b.size()-1;i>=0;i--){
while (!st.empty() && ps.back()[st.top()]<=ps.back()[i]) st.pop();
if (!st.empty()) g.back()[i]=st.top();
st.push(i);
}
for (int i=0;i<b.size();i++){
if (ps.back()[i]<0) continue;
pq.push({pm.back()[i],pm.size()-1,i,0});
break;
}
/*for (auto u:ps.back()) cout<<u<<' ';cout<<endl;
for (auto u:pm.back()) cout<<u<<' ';cout<<endl;
for (auto u:g.back()) cout<<u<<' ';cout<<endl;*/
}
while (!pq.empty()){
int i=pq.top()[1],j=pq.top()[2],last=pq.top()[3];
pq.pop();
//cout<<i<<' '<<j<<' '<<pm[i][j]<<' '<<ps[i][j]<<endl;
if (ans+pm[i][j]<0) break;
ans+=ps[i][j]-last;
if (g[i][j]==-1) continue;
int k=g[i][j];
pq.push({pm[i][k],i,k,ps[i][j]});
}
cout<<ans-a[0]<<endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:41:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i=1;i<b.size();i++){
| ~^~~~~~~~~
Main.cpp:51:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i=0;i<b.size();i++){
| ~^~~~~~~~~
Main.cpp:53:44: warning: narrowing conversion of '(pm.std::vector<std::vector<long long int> >::size() - 1)' from 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
53 | pq.push({pm.back()[i],pm.size()-1,i,0});
| ~~~~~~~~~^~
Main.cpp:53:44: warning: narrowing conversion of '(pm.std::vector<std::vector<long long int> >::size() - 1)' from 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
# | 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... |