# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
68537 | evpipis | Fireworks (APIO16_fireworks) | C++11 | 10 ms | 7716 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;
#define pb push_back
typedef long long ll;
const int len = 3e5+5;
int par[len], val[len];
vector<int> adj[len];
ll a[len], b[len];
priority_queue<int> *pq[len];
int main(){
int n, m;
scanf("%d %d", &n, &m);
for (int i = 2; i <= n+m; i++){
scanf("%d %d", &par[i], &val[i]);
adj[par[i]].pb(i);
}
for (int u = n+m; u >= 1; u--){
if (u > n){
pq[u] = new priority_queue<int>();
(*pq[u]).push(val[u]);
(*pq[u]).push(val[u]);
a[u] = 1;
b[u] = -val[u];
}
else{
int mx = 0, big;
for (int j = 0; j < adj[u].size(); j++){
int v = adj[u][j];
if ((*pq[v]).size() > mx)
mx = (*pq[v]).size(), big = v;
}
pq[u] = pq[big];
a[u] = a[big];
b[u] = b[big];
for (int j = 0; j < adj[u].size(); j++){
int v = adj[u][j];
if (v == big) continue;
a[u] += a[v];
b[u] += b[v];
while (!(*pq[v]).empty()){
(*pq[u]).push((*pq[v]).top());
(*pq[v]).pop();
}
}
for (int j = 1; j < adj[u].size(); j++){
int x = (*pq[u]).top();
(*pq[u]).pop();
a[u] -= 1;
b[u] += x;
}
int p2, p1;
p2 = (*pq[u]).top(), (*pq[u]).pop();
p1 = (*pq[u]).top(), (*pq[u]).pop();
// a[u] = a[u]
b[u] = p2*a[u]+b[u] - p2-val[u];
(*pq[u]).push(p1+val[u]);
(*pq[u]).push(p2+val[u]);
}
}
printf("%lld\n", a[1]*(*pq[1]).top() + b[1]);
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... |