# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
40845 | ssnsarang2023 | Fireworks (APIO16_fireworks) | C++14 | 245 ms | 132484 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;
typedef unsigned long long ull;
typedef pair<int, int> ii;
#define SZ(x) ((int)x.size())
const int N = (int)3e5+5;
struct data {
ll a, b;
priority_queue<ll> *pq;
} a[N];
data merge(const data &x, const data &y) {
data re;
re.a = x.a + y.a;
re.b = x.b + y.b;
if (x.pq->size() > y.pq->size()) {
re.pq = x.pq;
while (y.pq->size()) {
re.pq->push(y.pq->top());
y.pq->pop();
}
} else {
re.pq = y.pq;
while (x.pq->size()) {
re.pq->push(x.pq->top());
x.pq->pop();
}
}
return re;
}
int n, m, p[N];
ll c[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 2; i <= n+m; ++i) scanf("%d%lld", &p[i], &c[i]);
for (int i = 1; i <= n+m; ++i) {
a[i].pq = new priority_queue<ll>();
a[i].a = a[i].b = 0;
}
for (int i = n+m; i >= n+1; --i) {
a[i].a = 1;
a[i].b = -c[i];
a[i].pq->push(c[i]);
a[i].pq->push(c[i]);
a[p[i]] = merge(a[p[i]], a[i]);
}
for (int i = n; i >= 2; --i) {
while (a[i].a > 1) {
--a[i].a;
a[i].b += a[i].pq->top();
a[i].pq->pop();
}
ll x = a[i].pq->top();
a[i].pq->pop();
ll y = a[i].pq->top();
a[i].pq->pop();
a[i].pq->push(x + c[i]);
a[i].pq->push(y + c[i]);
a[i].b -= a[i].a*c[i];
a[p[i]] = merge(a[p[i]], a[i]);
}
while (a[1].a > 0) {
--a[1].a;
a[1].b += a[1].pq->top();
a[1].pq->pop();
}
printf("%lld", a[1].b);
return 0;
}
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... |