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;
using ll = long long;
#define int ll
using vi = vector<int>;
using pi = pair<ll,ll>;
#define REP(i,n) for (int i = 0; i < n; i++)
#define trav(a,x) for (auto& a : x)
#define D(x) //cerr << #x << ": " << x << endl;
#define all(x) (x).begin(), (x).end()
const int MAX_N = 3*1e5+3;
struct Info {
ll off = 0;
map<ll, ll> rew;
void add(int r) {
if (r == 0) return;
if (r > 0) {
rew[-off] += r;
} else {
ll rr = r;
r*=-1;
//off += r;
// go erasing
while (rew.size() && rr) {
if (rew.begin()->second > rr) {
rew.begin()->second -= rr;
rr = 0;
} else {
rr -= rew.begin()->second;
rew.erase(rew.begin());
}
}
// move
ll stole = 0;
ll bef = 0;
while (rew.size() && stole == 0) {
bef = rew.begin()->first;
stole += rew.begin()->second;
rew.erase(rew.begin());
}
bef += r;
while (rew.size() && rew.begin()->first < bef) {
stole += rew.begin()->second;
rew.erase(rew.begin());
}
if (stole > 0) rew[bef-off] += stole;
else assert(rew.size() == 0);
}
}
void merge(Info* other) {
trav(o, other->rew) {
ll re = o.first + other->off;
rew[re-off] += o.second;
}
delete other;
}
~Info() {
rew.clear();
}
};
int n; ll s;
vi children[MAX_N];
int values[MAX_N];
bool done[MAX_N];
Info* dfs(int i) {
done[i] = 1;
Info* info = new Info;
trav(ch, children[i]) {
Info* other = dfs(ch);
if (info->rew.size() < other->rew.size()) swap(info, other);
info->merge(other);
}
info->add(values[i]);
return info;
}
ll triv_dfs(int i) {
done[i] = 1;
ll tot = values[i];
trav(ch, children[i]) tot += triv_dfs(ch);
return max(tot, 0ll);
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
priority_queue<pi, vector<pi>, greater<pi>> q;
cin >> n >> s;
ll ss = s;
REP(i,n) {
int p; cin >> values[i] >> p;
if (p == 0) continue;
p--;
children[p].push_back(i);
}
REP(i,n) {
if (done[i]) continue;
Info* info = dfs(i);
trav(r, info->rew) {
q.push({r.first+info->off, r.second});
}
}
while (q.size()) {
auto r = q.top(); q.pop();
D(r.first);
D(r.second);
if (r.first <= s) s += r.second;
assert(r.second > 0);
//else break;
}
assert(s >= ss);
cout << s - ss << endl;
return 0;
}
# | 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... |