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 <iostream>
#include <vector>
#include <queue>
#define long long long
using namespace std;
const int N = 3e5 + 2;
int n, m;
vector<pair<int, int> > g[N];
priority_queue<long> q[N];
pair<int, long> line[N];
void Merge(priority_queue<long> &a, priority_queue<long> &b)
{
if (a.size() < b.size()) a.swap(b);
while (!b.empty()) a.push(b.top()), b.pop();
}
void Dfs(int u, int cost)
{
if (u > n)
{
q[u].push(cost);
q[u].push(cost);
line[u] = make_pair(1, -cost);
return;
}
for (auto e: g[u])
{
int v = e.first, c = e.second;
Dfs(v, c);
line[u].first += line[v].first;
line[u].second += line[v].second;
Merge(q[u], q[v]);
}
while (line[u].first > 1)
{
line[u].second += q[u].top();
q[u].pop();
--line[u].first;
}
long p1 = q[u].top(); q[u].pop();
long p0 = q[u].top(); q[u].pop();
q[u].push(p1 + cost);
q[u].push(p0 + cost);
line[u].second -= cost;
}
int main()
{
cin >> n >> m;
for (int i = 2; i <= n + m; ++i)
{
int p, c; cin >> p >> c;
g[p].push_back(make_pair(i, c));
}
Dfs(1, 0);
cout << line[1].second + q[1].top();
return 0;
}
# | 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... |