# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1043693 | guagua0407 | Jobs (BOI24_jobs) | C++17 | 184 ms | 33992 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=3e5+5;
vector<int> adj[mxn];
vector<ll> x(mxn);
vector<ll> mn(mxn);
vector<ll> pre(mxn);
vector<int> cand(mxn);
vector<int> pos(mxn);
int main() {_
int n;
ll S;
cin>>n>>S;
vector<int> p(mxn);
for(int i=1;i<=n;i++){
cin>>x[i]>>p[i];
adj[p[i]].push_back(i);
}
set<pair<int,int>> A,B;
function<void(int)> doo=[&](int root){
pre[pos[root]]=pre[p[pos[root]]]+x[pos[root]];
mn[pos[root]]=min(mn[p[pos[root]]],pre[pos[root]]);
while(S+mn[pos[root]]>=0){
if(cand[root]==-1 or pre[pos[root]]>pre[cand[root]]){
cand[root]=pos[root];
}
if(adj[pos[root]].empty()){
pos[root]=-1;
break;
}
pos[root]=adj[pos[root]][0];
pre[pos[root]]=pre[p[pos[root]]]+x[pos[root]];
mn[pos[root]]=min(mn[p[pos[root]]],pre[pos[root]]);
}
if(cand[root]!=-1) A.insert({pre[cand[root]],root});
if(pos[root]!=-1) B.insert({mn[pos[root]],root});
};
for(auto root:adj[0]){
cand[root]=-1;
pos[root]=root;
doo(root);
}
ll prv=S;
while(true){
if(A.empty() or (*A.rbegin()).f<0) break;
S+=(*A.rbegin()).f;
int root=(*A.rbegin()).s;
A.erase(prev(A.end()));
if(pos[root]!=-1){
B.erase({mn[pos[root]],root});
pre[cand[root]]=mn[cand[root]]=0;
pos[root]=adj[cand[root]][0];
cand[root]=pos[root];
doo(root);
}
while(!B.empty() and S+(*B.rbegin()).f>=0){
int root=(*B.rbegin()).s;
B.erase(prev(B.end()));
if(cand[root]!=-1) A.erase({pre[cand[root]],root});
doo(root);
}
}
cout<<S-prv<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
# | 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... |