This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* be name khoda */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define fori(i, n) forifrom(i, 0, n)
#define F first
#define S second
// ------------------------------------------------------------------
const ll maxn = 300010;
ll n, m, ans;
vector<pii> subt[maxn];
ll A[maxn * 2];
pii B[maxn];
void dfs(ll x) {
for (auto [y, w] : subt[x]) {
dfs(y);
}
ll i = 0;
for (auto [y, w] : subt[x]) {
A[i * 2] = B[y].F + w;
A[i * 2 + 1] = B[y].S + w;
++i;
}
if (!i) return;
sort(A, A + i * 2);
for (auto [y, w] : subt[x]) {
ans += max(0LL, B[y].F + w - A[i]);
ans += max(0LL, A[i] - B[y].S - w);
}
B[x] = {A[i - 1], A[i]};
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
n += m;
fori (i, n - 1) {
ll p, w; cin >> p >> w; --p;
subt[p].emplace_back(i + 1, w);
}
dfs(0);
cout << ans << '\n';
}
Compilation message (stderr)
fireworks.cpp: In function 'void dfs(ll)':
fireworks.cpp:24:20: warning: unused variable 'w' [-Wunused-variable]
for (auto [y, w] : subt[x]) {
^
# | 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... |