# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
284992 | Kastanda | Fireworks (APIO16_fireworks) | C++11 | 281 ms | 52608 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.
// M
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300005;
int n, m, P[N], C[N];
vector < int > Ch[N];
priority_queue < ll > PQ[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 2; i <= n + m; i ++)
scanf("%d%d", &P[i], &C[i]), Ch[P[i]].push_back(i);
for (int v = n + m; v; v --)
{
if (!Ch[v].size())
{
PQ[v].push(C[v]);
PQ[v].push(C[v]);
continue;
}
for (int u : Ch[v])
{
if (PQ[v].size() < PQ[u].size())
PQ[v].swap(PQ[u]);
while (PQ[u].size())
PQ[v].push(PQ[u].top()), PQ[u].pop();
}
for (int i = 0; i < (int)Ch[v].size() - 1; i ++)
PQ[v].pop(); // Note that now there are extra positive slopes other than +1.
// Only the intervals with slope equal to 0 or 1 shift C[v] units to the right.
ll slope1 = PQ[v].top(); PQ[v].pop();
ll slope0 = PQ[v].top(); PQ[v].pop();
PQ[v].push(slope0 + C[v]);
PQ[v].push(slope1 + C[v]);
}
PQ[1].pop(); // Getting rid of the interval with slope +1.
ll SM = 0; // SM is the value of F(0).
for (int v = 1; v <= n + m; v ++)
SM += C[v];
vector < ll > V;
while (PQ[1].size())
V.push_back(PQ[1].top()), PQ[1].pop();
V.push_back(0);
reverse(V.begin(), V.end());
for (int i = 1; i < (int)V.size(); i ++)
SM -= (V[i] - V[i - 1]) * ((int)V.size() - i);
return !printf("%lld\n", SM);
}
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... |