/* 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
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 |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
5 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
4 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7396 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Incorrect |
6 ms |
7424 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
5 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
4 ms |
7424 KB |
Output is correct |
11 |
Correct |
6 ms |
7396 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Incorrect |
6 ms |
7424 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
5 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
4 ms |
7424 KB |
Output is correct |
11 |
Correct |
6 ms |
7396 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Incorrect |
6 ms |
7424 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |