# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
106302 | alexpetrescu | Fireworks (APIO16_fireworks) | C++14 | 455 ms | 73416 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 <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout
#define ll long long
#define MAXN 300000
std::vector < int > g[MAXN + 1];
ll d[MAXN + 1];
int c[MAXN + 1], k, u[MAXN + 1];
std::priority_queue < ll > q[MAXN + 1];
void dfs(int x) {
for (auto &y : g[x]) {
dfs(y);
d[x] += d[y];
if (q[u[y]].size() > q[u[x]].size())
u[x] = u[y];
}
for (auto &y : g[x]) {
if (u[x] != u[y]) {
while (!q[u[y]].empty()) {
q[u[x]].push(q[u[y]].top());
q[u[y]].pop();
}
}
}
for (int i = g[x].size(); i > bool(c[x]); i--) {
d[x] += q[u[x]].top();
q[u[x]].pop();
}
if (g[x].empty()) {
u[x] = ++k;
q[u[x]].push(c[x]);
q[u[x]].push(c[x]);
d[x] = -c[x];
} else if (c[x]) {
ll a = q[u[x]].top();
q[u[x]].pop();
ll b = q[u[x]].top();
q[u[x]].pop();
q[u[x]].push(a + c[x]);
q[u[x]].push(b + c[x]);
d[x] -= c[x];
}
}
int main() {
int n, m;
fscanf(fin, "%d%d", &n, &m);
n += m;
for (int i = 2; i <= n; i++) {
int x;
fscanf(fin, "%d%d", &x, &c[i]);
g[x].push_back(i);
}
dfs(1);
fprintf(fout, "%lld\n", d[1]);
fclose(fin);
fclose(fout);
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... |