# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
24138 | khsoo01 | Fireworks (APIO16_fireworks) | C++11 | 673 ms | 53576 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;
typedef long long ll;
ll n, m, p[300005], l[300005];
struct hull {
ll p, i;
multiset<ll> s;
} h[300005];
ll Pop (hull &A) {
auto T = A.s.end(); T--;
ll ret = *T;
A.s.erase(T);
return ret;
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=2;i<=n;i++) {
scanf("%lld%lld",&p[i],&l[i]);
}
for(int i=1;i<=m;i++) {
ll P, L;
scanf("%lld%lld",&P,&L);
h[P].i--;
h[P].p += L;
h[P].s.insert(L);
h[P].s.insert(L);
}
for(int i=n;i>=2;i--) {
while(h[i].s.size() > 1-h[i].i) Pop(h[i]);
h[i].p += l[i];
ll A = Pop(h[i]), B = Pop(h[i]);
h[i].s.insert(A + l[i]);
h[i].s.insert(B + l[i]);
if(p[i] != 1 && h[p[i]].s.size() < h[i].s.size()) swap(h[p[i]], h[i]);
while(!h[i].s.empty()) h[p[i]].s.insert(Pop(h[i]));
h[p[i]].p += h[i].p;
h[p[i]].i += h[i].i;
}
ll ans = h[1].p, cur = 0, grd = h[1].i;
while(grd < 0) {
ll nxt = (*h[1].s.begin());
h[1].s.erase(h[1].s.begin());
ans += grd * (nxt - cur);
cur = nxt; grd++;
}
printf("%lld\n",ans);
}
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... |