# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
714933 | Spade1 | Fireworks (APIO16_fireworks) | C++14 | 228 ms | 75620 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>
//#include <grader.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 3e5 + 10;
vector<int> adj[N];
priority_queue<ll> q[N];
ll c[N];
void dfs(int i) {
if (adj[i].empty()) {
q[i].push(c[i]);
q[i].push(c[i]);
return;
}
for (auto j : adj[i]) {
dfs(j);
if (q[i].size() < q[j].size()) swap(q[i], q[j]);
while (!q[j].empty()) q[i].push(q[j].top()), q[j].pop();
}
for (int k = 1; k < adj[i].size(); ++k) q[i].pop();
ll a = q[i].top()+c[i]; q[i].pop();
ll b = q[i].top()+c[i]; q[i].pop();
q[i].push(a); q[i].push(b);
}
void solve() {
int n, m; cin >> n >> m;
for (int i = 2; i <= n+m; ++i) {
int p; cin >> p >> c[i];
adj[p].pb(i);
}
ll ans = 0;
for (int i = 2; i <= n+m; ++i) ans += c[i];
dfs(1);
q[1].pop();
while (!q[1].empty()) ans -= q[1].top(), q[1].pop();
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
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... |