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 set multiset
int N;
vector<int> root;
int h,h1;
vector<int> adjlst[300100];
long long int lst[300100];
int parent[300100];
const long long int inf = 2e18;
long long int money;
priority_queue<pair<long long int,long long int>,vector<pair<long long int,long long int>>,greater<pair<long long int,long long int>>> pq;
set<pair<long long int,long long int>>* dfs(int i){
//printf("%d",i);
pair<int,int> ii = {-1,-1};
vector<set<pair<long long int,long long int>>*> v;
for(int j : adjlst[i]){
v.push_back(dfs(j));
ii = max({(int)(v.back() -> size()),v.size() - 1},ii);
}
//printf("%d\n",i);
set<pair<long long int,long long int>>* sat;
if(ii.first == -1){
sat = new set<pair<long long int,long long int>>();
}
else{
sat = v[ii.second];
}
if(lst[i] > 0) sat -> insert({0,lst[i]});
for(int i = 0; i < v.size(); i++){
if(i == ii.second) continue;
for(pair<long long int, long long int> pp : *(v[i]) ){
sat -> insert(pp);
}
}
//offset -= min(0LL,va);
long long int num = 0;
if(lst[i] < 0){
long long int balls = -lst[i];
num = -lst[i];
while(num != 0 && !(sat -> empty())){
auto it = sat -> begin();
long long int temp = min(num, it -> second);
balls = max((sat -> begin() -> first) + num, balls);
num -= temp;
if(it -> second == temp){
sat -> erase(it);
}
else{
pair<long long int, long long int> ii = *it;
ii.second = ii.second - temp ;
sat -> erase(it);
sat -> insert(ii);
}
}
if(num != 0) return sat;
while(sat -> lower_bound({balls,inf}) != sat -> begin()){
auto it = sat -> lower_bound({balls,inf});
advance(it,-1);
num += it -> second;
sat -> erase(it);
}
if(num != 0) sat -> insert({balls, num});
return sat;
}
else{
return sat;
}
}
int main(){
//freopen("jobs.17-2345.in","r",stdin);
scanf(" %d",&N);
scanf(" %lld",&money);
long long int init = money;
for(int i = 1; i <= N; i++){
scanf(" %d",&h);
scanf(" %d",&h1);
if(h1 == 0){
root.push_back(i);
}
else{
adjlst[h1].push_back(i);
}
lst[i] = h;
}
//printf("\n");
for(int i : root){
set<pair<long long int,long long int>>* ii = dfs(i);
for(pair<long long int, long long int> pp : *ii){
//printf("%d %d\n",pp.first,pp.second);
pq.push(pp);
}
}
while(!pq.empty()){
if(money < pq.top().first) break;
money += pq.top().second;
pq.pop();
}
printf("%lld",money - init);
}
/*
5 104
-103 0
102 1
-103 2
55 3
50 3
1 0
-5 0
*/
Compilation message (stderr)
Main.cpp: In function 'std::multiset<std::pair<long long int, long long int> >* dfs(int)':
Main.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::multiset<std::pair<long long int, long long int> >*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
Main.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf(" %lld",&money);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf(" %d",&h);
| ~~~~~^~~~~~~~~~
Main.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf(" %d",&h1);
| ~~~~~^~~~~~~~~~~
# | 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... |