#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define SZ(x) ll(x.size())
const ll MAXN = 3e5 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;
ll n , m , ans , cur , C[MAXN];
vector<int> adj[MAXN];
priority_queue<ll> pq[MAXN];
void DFS(int v){
for(int u : adj[v]){
DFS(u);
if(SZ(pq[u]) > SZ(pq[v])) pq[v].swap(pq[u]);
while(SZ(pq[u])){
pq[v].push(pq[u].top());
pq[u].pop();
}
}
ll x = 0 , y = 0;
for(int u : adj[v]){
x = pq[v].top();
pq[v].pop();
}
if(SZ(pq[v])){
y = pq[v].top();
pq[v].pop();
}
pq[v].push(y + C[v]);
pq[v].push(x + C[v]);
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> m; n += m;
for(int i = 2 ; i <= n ; i++){
int p; cin >> p >> C[i];
adj[p].push_back(i); ans += C[i];
}
DFS(1);
pq[1].push(0);
while(SZ(pq[1]) > 1){
ll x = pq[1].top(); pq[1].pop();
// cout << x << endl;
ans -= cur * (x - pq[1].top());
cur++;
}
// cout << pq[1].top() << endl;
cout << ans << endl;
return 0;
}
/*
*/
Compilation message
fireworks.cpp: In function 'void DFS(int)':
fireworks.cpp:34:10: warning: unused variable 'u' [-Wunused-variable]
34 | for(int u : adj[v]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16748 KB |
Output is correct |
2 |
Correct |
12 ms |
16748 KB |
Output is correct |
3 |
Correct |
11 ms |
16748 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16876 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
10 ms |
16748 KB |
Output is correct |
8 |
Correct |
10 ms |
16748 KB |
Output is correct |
9 |
Correct |
11 ms |
16748 KB |
Output is correct |
10 |
Correct |
11 ms |
16748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16748 KB |
Output is correct |
3 |
Correct |
11 ms |
16748 KB |
Output is correct |
4 |
Correct |
11 ms |
16748 KB |
Output is correct |
5 |
Correct |
11 ms |
16748 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
11 ms |
16748 KB |
Output is correct |
8 |
Correct |
11 ms |
16748 KB |
Output is correct |
9 |
Correct |
11 ms |
16748 KB |
Output is correct |
10 |
Correct |
11 ms |
16748 KB |
Output is correct |
11 |
Correct |
11 ms |
16748 KB |
Output is correct |
12 |
Correct |
11 ms |
16748 KB |
Output is correct |
13 |
Correct |
11 ms |
16876 KB |
Output is correct |
14 |
Correct |
11 ms |
16876 KB |
Output is correct |
15 |
Correct |
11 ms |
16748 KB |
Output is correct |
16 |
Correct |
11 ms |
16748 KB |
Output is correct |
17 |
Correct |
11 ms |
16748 KB |
Output is correct |
18 |
Correct |
11 ms |
16876 KB |
Output is correct |
19 |
Correct |
11 ms |
16748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16748 KB |
Output is correct |
2 |
Correct |
12 ms |
16748 KB |
Output is correct |
3 |
Correct |
11 ms |
16748 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16876 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
10 ms |
16748 KB |
Output is correct |
8 |
Correct |
10 ms |
16748 KB |
Output is correct |
9 |
Correct |
11 ms |
16748 KB |
Output is correct |
10 |
Correct |
11 ms |
16748 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16748 KB |
Output is correct |
13 |
Correct |
11 ms |
16748 KB |
Output is correct |
14 |
Correct |
11 ms |
16748 KB |
Output is correct |
15 |
Correct |
11 ms |
16748 KB |
Output is correct |
16 |
Correct |
11 ms |
16748 KB |
Output is correct |
17 |
Correct |
11 ms |
16748 KB |
Output is correct |
18 |
Correct |
11 ms |
16748 KB |
Output is correct |
19 |
Correct |
11 ms |
16748 KB |
Output is correct |
20 |
Correct |
11 ms |
16748 KB |
Output is correct |
21 |
Correct |
11 ms |
16748 KB |
Output is correct |
22 |
Correct |
11 ms |
16748 KB |
Output is correct |
23 |
Correct |
11 ms |
16876 KB |
Output is correct |
24 |
Correct |
11 ms |
16876 KB |
Output is correct |
25 |
Correct |
11 ms |
16748 KB |
Output is correct |
26 |
Correct |
11 ms |
16748 KB |
Output is correct |
27 |
Correct |
11 ms |
16748 KB |
Output is correct |
28 |
Correct |
11 ms |
16876 KB |
Output is correct |
29 |
Correct |
11 ms |
16748 KB |
Output is correct |
30 |
Correct |
11 ms |
16748 KB |
Output is correct |
31 |
Correct |
11 ms |
16876 KB |
Output is correct |
32 |
Correct |
11 ms |
16876 KB |
Output is correct |
33 |
Correct |
12 ms |
16876 KB |
Output is correct |
34 |
Correct |
12 ms |
17004 KB |
Output is correct |
35 |
Correct |
13 ms |
17004 KB |
Output is correct |
36 |
Correct |
15 ms |
17024 KB |
Output is correct |
37 |
Correct |
13 ms |
17132 KB |
Output is correct |
38 |
Correct |
14 ms |
17132 KB |
Output is correct |
39 |
Correct |
15 ms |
17132 KB |
Output is correct |
40 |
Correct |
13 ms |
17644 KB |
Output is correct |
41 |
Correct |
12 ms |
17644 KB |
Output is correct |
42 |
Correct |
12 ms |
17004 KB |
Output is correct |
43 |
Correct |
13 ms |
17388 KB |
Output is correct |
44 |
Correct |
13 ms |
17260 KB |
Output is correct |
45 |
Correct |
13 ms |
17260 KB |
Output is correct |
46 |
Correct |
14 ms |
17260 KB |
Output is correct |
47 |
Correct |
14 ms |
17260 KB |
Output is correct |
48 |
Correct |
13 ms |
17132 KB |
Output is correct |
49 |
Correct |
14 ms |
17132 KB |
Output is correct |
50 |
Correct |
13 ms |
17132 KB |
Output is correct |
51 |
Correct |
13 ms |
17132 KB |
Output is correct |
52 |
Correct |
13 ms |
17132 KB |
Output is correct |
53 |
Correct |
16 ms |
17132 KB |
Output is correct |
54 |
Correct |
14 ms |
17132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16748 KB |
Output is correct |
2 |
Correct |
12 ms |
16748 KB |
Output is correct |
3 |
Correct |
11 ms |
16748 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16876 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
10 ms |
16748 KB |
Output is correct |
8 |
Correct |
10 ms |
16748 KB |
Output is correct |
9 |
Correct |
11 ms |
16748 KB |
Output is correct |
10 |
Correct |
11 ms |
16748 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16748 KB |
Output is correct |
13 |
Correct |
11 ms |
16748 KB |
Output is correct |
14 |
Correct |
11 ms |
16748 KB |
Output is correct |
15 |
Correct |
11 ms |
16748 KB |
Output is correct |
16 |
Correct |
11 ms |
16748 KB |
Output is correct |
17 |
Correct |
11 ms |
16748 KB |
Output is correct |
18 |
Correct |
11 ms |
16748 KB |
Output is correct |
19 |
Correct |
11 ms |
16748 KB |
Output is correct |
20 |
Correct |
11 ms |
16748 KB |
Output is correct |
21 |
Correct |
11 ms |
16748 KB |
Output is correct |
22 |
Correct |
11 ms |
16748 KB |
Output is correct |
23 |
Correct |
11 ms |
16876 KB |
Output is correct |
24 |
Correct |
11 ms |
16876 KB |
Output is correct |
25 |
Correct |
11 ms |
16748 KB |
Output is correct |
26 |
Correct |
11 ms |
16748 KB |
Output is correct |
27 |
Correct |
11 ms |
16748 KB |
Output is correct |
28 |
Correct |
11 ms |
16876 KB |
Output is correct |
29 |
Correct |
11 ms |
16748 KB |
Output is correct |
30 |
Correct |
11 ms |
16748 KB |
Output is correct |
31 |
Correct |
11 ms |
16876 KB |
Output is correct |
32 |
Correct |
11 ms |
16876 KB |
Output is correct |
33 |
Correct |
12 ms |
16876 KB |
Output is correct |
34 |
Correct |
12 ms |
17004 KB |
Output is correct |
35 |
Correct |
13 ms |
17004 KB |
Output is correct |
36 |
Correct |
15 ms |
17024 KB |
Output is correct |
37 |
Correct |
13 ms |
17132 KB |
Output is correct |
38 |
Correct |
14 ms |
17132 KB |
Output is correct |
39 |
Correct |
15 ms |
17132 KB |
Output is correct |
40 |
Correct |
13 ms |
17644 KB |
Output is correct |
41 |
Correct |
12 ms |
17644 KB |
Output is correct |
42 |
Correct |
12 ms |
17004 KB |
Output is correct |
43 |
Correct |
13 ms |
17388 KB |
Output is correct |
44 |
Correct |
13 ms |
17260 KB |
Output is correct |
45 |
Correct |
13 ms |
17260 KB |
Output is correct |
46 |
Correct |
14 ms |
17260 KB |
Output is correct |
47 |
Correct |
14 ms |
17260 KB |
Output is correct |
48 |
Correct |
13 ms |
17132 KB |
Output is correct |
49 |
Correct |
14 ms |
17132 KB |
Output is correct |
50 |
Correct |
13 ms |
17132 KB |
Output is correct |
51 |
Correct |
13 ms |
17132 KB |
Output is correct |
52 |
Correct |
13 ms |
17132 KB |
Output is correct |
53 |
Correct |
16 ms |
17132 KB |
Output is correct |
54 |
Correct |
14 ms |
17132 KB |
Output is correct |
55 |
Correct |
18 ms |
17772 KB |
Output is correct |
56 |
Correct |
44 ms |
20332 KB |
Output is correct |
57 |
Correct |
72 ms |
22764 KB |
Output is correct |
58 |
Correct |
101 ms |
24292 KB |
Output is correct |
59 |
Correct |
133 ms |
26852 KB |
Output is correct |
60 |
Correct |
175 ms |
29412 KB |
Output is correct |
61 |
Correct |
198 ms |
31480 KB |
Output is correct |
62 |
Correct |
217 ms |
32848 KB |
Output is correct |
63 |
Correct |
282 ms |
36104 KB |
Output is correct |
64 |
Correct |
282 ms |
36832 KB |
Output is correct |
65 |
Correct |
124 ms |
71020 KB |
Output is correct |
66 |
Correct |
153 ms |
71020 KB |
Output is correct |
67 |
Correct |
136 ms |
33644 KB |
Output is correct |
68 |
Correct |
187 ms |
53472 KB |
Output is correct |
69 |
Correct |
219 ms |
50400 KB |
Output is correct |
70 |
Correct |
221 ms |
50528 KB |
Output is correct |
71 |
Correct |
237 ms |
45784 KB |
Output is correct |
72 |
Correct |
233 ms |
46300 KB |
Output is correct |
73 |
Correct |
215 ms |
42456 KB |
Output is correct |
74 |
Correct |
225 ms |
42584 KB |
Output is correct |
75 |
Correct |
214 ms |
41436 KB |
Output is correct |
76 |
Correct |
214 ms |
41564 KB |
Output is correct |
77 |
Correct |
224 ms |
41204 KB |
Output is correct |
78 |
Correct |
219 ms |
39132 KB |
Output is correct |
79 |
Correct |
212 ms |
40200 KB |
Output is correct |