# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
106301 | alexpetrescu | Fireworks (APIO16_fireworks) | C++14 | 20 ms | 16896 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];
std::priority_queue < ll > q[MAXN + 1];
void dfs(int x) {
for (auto &y : g[x]) {
dfs(y);
d[x] += d[y];
while (!q[y].empty()) {
q[x].push(q[y].top());
q[y].pop();
}
}
for (int i = g[x].size(); i > bool(c[x]); i--) {
d[x] += q[x].top();
q[x].pop();
}
if (g[x].empty()) {
q[x].push(c[x]);
q[x].push(c[x]);
d[x] = -c[x];
} else if (c[x]) {
int a = q[x].top();
q[x].pop();
int b = q[x].top();
q[x].pop();
q[x].push(a + c[x]);
q[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... |