# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|
1025500 | | pcc | Jobs (BOI24_jobs) | C++17 | | 1456 ms | 61380 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.
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pll pair<ll,ll>
#define fs first
#define sc second
const ll mxn = 3e5+10;
ll N,S;
ll arr[mxn];
vector<int> tree[mxn],tree2[mxn],tree3[mxn];
vector<int> rts;
ll dp[mxn];
priority_queue<pll,vector<pll>,less<pll>> son[mxn];
ll shift[mxn];
ll dist[mxn];
void dfs(int now,int top = 0){
dp[top] += arr[now];
for(auto nxt:tree[now]){
if(arr[nxt]<0){
dist[nxt] = dist[top]+arr[nxt];
tree2[top].push_back(nxt);
dfs(nxt,nxt);
}
else{
dfs(nxt,top);
}
}
}
void dfs2(int now){
for(auto nxt:tree2[now]){
dfs2(nxt);
//cerr<<"TREE2: "<<now<<','<<nxt<<':'<<arr[nxt]<<' '<<dp[nxt]<<endl;
son[now].push(pll(arr[nxt]+arr[now],nxt));
}
ll tmp = arr[now];
while(dp[now]<=0&&!son[now].empty()){
auto [dep,tar] = son[now].top();son[now].pop();
if(dp[tar]<0)continue;
arr[now] = min(arr[now],dep);
dp[now] += dp[tar];
while(!son[tar].empty()){
auto [__,nxt] = son[tar].top();
son[tar].pop();
son[now].push(pll(dep+arr[nxt],nxt));
}
}
return;
}
void dfs3(int now){
cerr<<now<<":"<<arr[now]<<','<<dp[now]<<endl;
while(!son[now].empty()){
tree3[now].push_back(son[now].top().sc);
son[now].pop();
}
for(auto nxt:tree3[now]){
cerr<<now<<','<<nxt<<endl;
dfs3(nxt);
}
return;
}
priority_queue<pll,vector<pll>,less<pll>> pq;
main(){
cin>>N>>S;
for(int i = 1;i<=N;i++){
ll x,p;
cin>>x>>p;
tree[p].push_back(i);
arr[i] = x;
}
dfs(0,0);
//cerr<<"DFS1 DONE!"<<endl;
dfs2(0);
//cerr<<"DFS2 DONE!"<<endl;
pq.push(pll(arr[0],0));
ll ans = 0;
cerr<<"DFS3 start!"<<endl;dfs3(0);
while(!pq.empty()&&S+pq.top().fs>=0){
auto [val,id] = pq.top();pq.pop();
if(dp[id]<0)continue;
ans += dp[id];
S += dp[id];
while(!son[id].empty()){
auto [__,nxt] = son[id].top();
son[id].pop();
pq.push(pll(arr[nxt],nxt));
}
}
cout<<ans<<'\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void dfs2(long long int)':
Main.cpp:40:5: warning: unused variable 'tmp' [-Wunused-variable]
40 | ll tmp = arr[now];
| ^~~
Main.cpp: At global scope:
Main.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main(){
| ^~~~
# | 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... |