This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** Baltic Olympiad in Informatics 2024 **/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 300001;
vector<int> adj[maxn];
int a[maxn];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> v[maxn];
void dfs(int x) {
for(int i : adj[x]) {
dfs(i);
if(v[i].size() > v[x].size()) {
swap(v[i], v[x]);
}
while(v[i].size()) {
v[x].push(v[i].top());
v[i].pop();
}
}
if(a[x] >= 0) {
v[x].push({0, a[x]});
} else {
pair<int, int> cur = {-a[x], a[x]};
while(v[x].size() && cur.second < 0) {
pair<int, int> tmp = v[x].top();
v[x].pop();
cur.first = max(cur.first, tmp.first - cur.second);
cur.second += tmp.second;
}
if(cur.second >= 0) {
while(v[x].size() && v[x].top().first <= cur.first) {
cur.second += v[x].top().second;
v[x].pop();
}
v[x].push(cur);
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, s, x, ans = 0;
cin >> n >> s;
a[0] = s;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> x;
adj[x].push_back(i);
}
dfs(0);
while(v[0].size() && v[0].top().first <= ans) {
ans += v[0].top().second;
v[0].pop();
}
cout << ans-s;
}
# | 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... |