# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1049369 | aymanrs | Jobs (BOI24_jobs) | C++17 | 85 ms | 25972 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;
struct node {
int p;
long long x;
vector<node*> l;
};
void dfs(node* n){
for(node* c : n->l){
dfs(c);
if(c->x > 0) n->x += c->x;
}
}
void solve(){
int n;cin >> n;
long long s;cin >> s;
if(s == 1e18){
node g[n+1];
for(int i = 1;i <= n;i++){
cin >> g[i].x >> g[i].p;
g[g[i].p].l.push_back(&g[i]);
}
g[0].x=0;
dfs(&g[0]);
cout << g[0].x << '\n';
return;
}
vector<int> v[n+1];
int r[n+1];
for(int i = 1;i <= n;i++){
r[i]=i;int p,x;
cin >> x >> p;
if(p) {
r[i]=r[p];
}
v[r[i]].push_back(x);
}
fill(r,r+n+1, 0);
set<pair<int, int>> se;
long long t[n+1] = {0};
for(int i = 1;i <= n;i++){
long long mi = 0;
while(t[i] <= 0 && r[i] < v[i].size()){
t[i] += v[i][r[i]];
mi = min(mi,t[i]);
r[i]++;
}
if(t[i]>0) se.insert({-mi, i});
}
long long os = s;
while(!se.empty() && se.begin()->first<=s){
int i = se.begin()->second;
se.erase(se.begin());
s += t[i];
t[i]=0;
long long mi = 0;
while(t[i] <= 0 && r[i] < v[i].size()){
t[i] += v[i][r[i]];
mi = min(mi,t[i]);
r[i]++;
}
if(t[i]>0) se.insert({-mi, i});
}
cout << s-os << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
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... |