// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
const ll inf = 1e18 + 10;
vector<pii> v[maxn];
int n, m;
vector<ll> peb[maxn];
int infs[maxn];
void dfs(int u, int S = 0){
if(u >= n){
peb[u].PB(S);
peb[u].PB(S);
return;
}
for(auto [y, x] : v[u]){
dfs(y, x);
infs[u]+= infs[y];
if(sz(peb[u]) < sz(peb[y]))
swap(peb[u], peb[y]);
for(int x : peb[y])
peb[u].PB(x);
}
sort(peb[u].begin(), peb[u].end());
int N = sz(peb[u]) + infs[u];
assert(sz(peb[u]) >= (N/2)+1);
ll L = peb[u][(N/2)-1], R = peb[u][(N/2)];
infs[u]+= sz(peb[u]) - (N/2) - 1;
peb[u].resize((N/2) + 1);
peb[u].pop_back(), peb[u].pop_back();
peb[u].PB(L + S);
peb[u].PB(R + S);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
ll sm = 0;
cin >> n >> m;
for(int i = 1; i < n+m; i++){
int a, b;
cin >> a >> b;
v[--a].PB({i, b});
sm+= b;
}
dfs(0);
sort(peb[0].begin(), peb[0].end());
int N = sz(peb[0]) + infs[0];
ll bst = peb[0][(N/2)-1];
ll df = 0;
for(ll x : peb[0]){
df+= abs(x - bst);
df-= x;
}
df+= -bst * infs[0];
ll ans = df + sm *2;
return cout << ans/2 << endl, 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int, int)':
fireworks.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for(auto [y, x] : v[u]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5092 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
5 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5016 KB |
Output is correct |
11 |
Correct |
5 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5092 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5092 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
5016 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
3 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5092 KB |
Output is correct |
26 |
Correct |
4 ms |
4992 KB |
Output is correct |
27 |
Correct |
4 ms |
4992 KB |
Output is correct |
28 |
Correct |
4 ms |
4992 KB |
Output is correct |
29 |
Correct |
4 ms |
4992 KB |
Output is correct |
30 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5092 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
5016 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
3 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5092 KB |
Output is correct |
26 |
Correct |
4 ms |
4992 KB |
Output is correct |
27 |
Correct |
4 ms |
4992 KB |
Output is correct |
28 |
Correct |
4 ms |
4992 KB |
Output is correct |
29 |
Correct |
4 ms |
4992 KB |
Output is correct |
30 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |