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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const ll linf = 9e18;
priority_queue<ll> pq[N];
int deg[N], par[N];
ll len[N], dp[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 2; i <= n + m; i++) {
cin >> par[i] >> len[i];
deg[par[i]]++;
dp[par[i]] -= len[i];
if (i > n) {
pq[par[i]].push(len[i]);
pq[par[i]].push(len[i]);
}
}
for (int i = n; i > 1; i--) {
for (int j = 0; j < deg[i] - 1; j++) {
dp[i] += pq[i].top();
pq[i].pop();
}
pq[par[i]].push(pq[i].top() + len[i]);
pq[i].pop();
pq[par[i]].push(pq[i].top() + len[i]);
pq[i].pop();
dp[par[i]] += dp[i];
if (pq[par[i]].size() < pq[i].size()) swap(pq[par[i]], pq[i]);
while (pq[i].size()) pq[par[i]].push(pq[i].top()), pq[i].pop();
}
ll ans = 0;
for (int j = 0; j < deg[1]; j++) {
ans += pq[1].top();
pq[1].pop();
}
cout << dp[1] + ans << en;
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... |