#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define int long long
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cerr<<#x << " : " << x << "\n";
using namespace std ;
const int maxn = 4e5 + 10 , mod = 1e9 + 7 , inf =1e14 ;
int pr[maxn] , ted[maxn] ;
vector <int> G[maxn] ;
int W[maxn] ;
priority_queue <int> q[maxn] ;
int N, M;
void dfs(int v) {
if(v<N) {
for(int u:G[v]) {
dfs(u);
if(q[v].size()<q[u].size()) swap(q[v], q[u]);
while(q[u].size()) q[v].push(q[u].top()), q[u].pop();
}
rep(j, 1, G[v].size()-1) q[v].pop();
int x=q[v].top()+W[v]; q[v].pop();
int y=q[v].top()+W[v]; q[v].pop();
q[v].push(x), q[v].push(y);
} else q[v].push(W[v]), q[v].push(W[v]);
}
signed main(){
ios::sync_with_stdio(0) ;cin.tie(0);
int r=0;
cin>>N>>M;
rep(i, 1, N+M-1) {
int j;
cin>>j>>W[i];
G[j-1].pb(i);
r+=W[i];
}
dfs(0);
int p=q[0].top(), k=0; q[0].pop();
while(q[0].size()){
r-=k++*(p-q[0].top()), p=q[0].top(), q[0].pop();
}
cout<<r-k*p;
}
Compilation message
fireworks.cpp: In function 'void dfs(long long int)':
fireworks.cpp:12:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | #define rep(i , a , b) for(int i=a;i <= (b);i++)
| ^
fireworks.cpp:30:3: note: in expansion of macro 'rep'
30 | rep(j, 1, G[v].size()-1) q[v].pop();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
27224 KB |
Output is correct |
2 |
Correct |
12 ms |
27228 KB |
Output is correct |
3 |
Correct |
7 ms |
27324 KB |
Output is correct |
4 |
Correct |
8 ms |
27228 KB |
Output is correct |
5 |
Correct |
8 ms |
27224 KB |
Output is correct |
6 |
Correct |
7 ms |
27224 KB |
Output is correct |
7 |
Correct |
8 ms |
27224 KB |
Output is correct |
8 |
Correct |
7 ms |
27228 KB |
Output is correct |
9 |
Correct |
7 ms |
27312 KB |
Output is correct |
10 |
Correct |
8 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
27224 KB |
Output is correct |
2 |
Correct |
7 ms |
27228 KB |
Output is correct |
3 |
Correct |
7 ms |
27320 KB |
Output is correct |
4 |
Correct |
7 ms |
27228 KB |
Output is correct |
5 |
Correct |
7 ms |
27260 KB |
Output is correct |
6 |
Correct |
7 ms |
27228 KB |
Output is correct |
7 |
Correct |
7 ms |
27264 KB |
Output is correct |
8 |
Correct |
8 ms |
27324 KB |
Output is correct |
9 |
Correct |
8 ms |
27228 KB |
Output is correct |
10 |
Correct |
7 ms |
27332 KB |
Output is correct |
11 |
Correct |
7 ms |
27224 KB |
Output is correct |
12 |
Correct |
7 ms |
27228 KB |
Output is correct |
13 |
Correct |
8 ms |
27308 KB |
Output is correct |
14 |
Correct |
8 ms |
27228 KB |
Output is correct |
15 |
Correct |
9 ms |
27484 KB |
Output is correct |
16 |
Correct |
9 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27224 KB |
Output is correct |
18 |
Correct |
8 ms |
27228 KB |
Output is correct |
19 |
Correct |
7 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
27224 KB |
Output is correct |
2 |
Correct |
12 ms |
27228 KB |
Output is correct |
3 |
Correct |
7 ms |
27324 KB |
Output is correct |
4 |
Correct |
8 ms |
27228 KB |
Output is correct |
5 |
Correct |
8 ms |
27224 KB |
Output is correct |
6 |
Correct |
7 ms |
27224 KB |
Output is correct |
7 |
Correct |
8 ms |
27224 KB |
Output is correct |
8 |
Correct |
7 ms |
27228 KB |
Output is correct |
9 |
Correct |
7 ms |
27312 KB |
Output is correct |
10 |
Correct |
8 ms |
27228 KB |
Output is correct |
11 |
Correct |
8 ms |
27224 KB |
Output is correct |
12 |
Correct |
7 ms |
27228 KB |
Output is correct |
13 |
Correct |
7 ms |
27320 KB |
Output is correct |
14 |
Correct |
7 ms |
27228 KB |
Output is correct |
15 |
Correct |
7 ms |
27260 KB |
Output is correct |
16 |
Correct |
7 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27264 KB |
Output is correct |
18 |
Correct |
8 ms |
27324 KB |
Output is correct |
19 |
Correct |
8 ms |
27228 KB |
Output is correct |
20 |
Correct |
7 ms |
27332 KB |
Output is correct |
21 |
Correct |
7 ms |
27224 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
8 ms |
27308 KB |
Output is correct |
24 |
Correct |
8 ms |
27228 KB |
Output is correct |
25 |
Correct |
9 ms |
27484 KB |
Output is correct |
26 |
Correct |
9 ms |
27228 KB |
Output is correct |
27 |
Correct |
7 ms |
27224 KB |
Output is correct |
28 |
Correct |
8 ms |
27228 KB |
Output is correct |
29 |
Correct |
7 ms |
27228 KB |
Output is correct |
30 |
Correct |
8 ms |
27228 KB |
Output is correct |
31 |
Correct |
8 ms |
27228 KB |
Output is correct |
32 |
Correct |
9 ms |
27280 KB |
Output is correct |
33 |
Correct |
9 ms |
27228 KB |
Output is correct |
34 |
Correct |
9 ms |
27260 KB |
Output is correct |
35 |
Correct |
10 ms |
27484 KB |
Output is correct |
36 |
Correct |
9 ms |
27488 KB |
Output is correct |
37 |
Correct |
9 ms |
27484 KB |
Output is correct |
38 |
Correct |
10 ms |
27484 KB |
Output is correct |
39 |
Correct |
10 ms |
27484 KB |
Output is correct |
40 |
Correct |
10 ms |
27996 KB |
Output is correct |
41 |
Correct |
9 ms |
27996 KB |
Output is correct |
42 |
Correct |
9 ms |
27484 KB |
Output is correct |
43 |
Correct |
10 ms |
27796 KB |
Output is correct |
44 |
Correct |
10 ms |
27740 KB |
Output is correct |
45 |
Correct |
11 ms |
27740 KB |
Output is correct |
46 |
Correct |
11 ms |
27740 KB |
Output is correct |
47 |
Correct |
10 ms |
27520 KB |
Output is correct |
48 |
Correct |
10 ms |
27484 KB |
Output is correct |
49 |
Correct |
9 ms |
27484 KB |
Output is correct |
50 |
Correct |
9 ms |
27484 KB |
Output is correct |
51 |
Correct |
9 ms |
27484 KB |
Output is correct |
52 |
Correct |
10 ms |
27588 KB |
Output is correct |
53 |
Correct |
9 ms |
27484 KB |
Output is correct |
54 |
Correct |
9 ms |
27484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
27224 KB |
Output is correct |
2 |
Correct |
12 ms |
27228 KB |
Output is correct |
3 |
Correct |
7 ms |
27324 KB |
Output is correct |
4 |
Correct |
8 ms |
27228 KB |
Output is correct |
5 |
Correct |
8 ms |
27224 KB |
Output is correct |
6 |
Correct |
7 ms |
27224 KB |
Output is correct |
7 |
Correct |
8 ms |
27224 KB |
Output is correct |
8 |
Correct |
7 ms |
27228 KB |
Output is correct |
9 |
Correct |
7 ms |
27312 KB |
Output is correct |
10 |
Correct |
8 ms |
27228 KB |
Output is correct |
11 |
Correct |
8 ms |
27224 KB |
Output is correct |
12 |
Correct |
7 ms |
27228 KB |
Output is correct |
13 |
Correct |
7 ms |
27320 KB |
Output is correct |
14 |
Correct |
7 ms |
27228 KB |
Output is correct |
15 |
Correct |
7 ms |
27260 KB |
Output is correct |
16 |
Correct |
7 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27264 KB |
Output is correct |
18 |
Correct |
8 ms |
27324 KB |
Output is correct |
19 |
Correct |
8 ms |
27228 KB |
Output is correct |
20 |
Correct |
7 ms |
27332 KB |
Output is correct |
21 |
Correct |
7 ms |
27224 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
8 ms |
27308 KB |
Output is correct |
24 |
Correct |
8 ms |
27228 KB |
Output is correct |
25 |
Correct |
9 ms |
27484 KB |
Output is correct |
26 |
Correct |
9 ms |
27228 KB |
Output is correct |
27 |
Correct |
7 ms |
27224 KB |
Output is correct |
28 |
Correct |
8 ms |
27228 KB |
Output is correct |
29 |
Correct |
7 ms |
27228 KB |
Output is correct |
30 |
Correct |
8 ms |
27228 KB |
Output is correct |
31 |
Correct |
8 ms |
27228 KB |
Output is correct |
32 |
Correct |
9 ms |
27280 KB |
Output is correct |
33 |
Correct |
9 ms |
27228 KB |
Output is correct |
34 |
Correct |
9 ms |
27260 KB |
Output is correct |
35 |
Correct |
10 ms |
27484 KB |
Output is correct |
36 |
Correct |
9 ms |
27488 KB |
Output is correct |
37 |
Correct |
9 ms |
27484 KB |
Output is correct |
38 |
Correct |
10 ms |
27484 KB |
Output is correct |
39 |
Correct |
10 ms |
27484 KB |
Output is correct |
40 |
Correct |
10 ms |
27996 KB |
Output is correct |
41 |
Correct |
9 ms |
27996 KB |
Output is correct |
42 |
Correct |
9 ms |
27484 KB |
Output is correct |
43 |
Correct |
10 ms |
27796 KB |
Output is correct |
44 |
Correct |
10 ms |
27740 KB |
Output is correct |
45 |
Correct |
11 ms |
27740 KB |
Output is correct |
46 |
Correct |
11 ms |
27740 KB |
Output is correct |
47 |
Correct |
10 ms |
27520 KB |
Output is correct |
48 |
Correct |
10 ms |
27484 KB |
Output is correct |
49 |
Correct |
9 ms |
27484 KB |
Output is correct |
50 |
Correct |
9 ms |
27484 KB |
Output is correct |
51 |
Correct |
9 ms |
27484 KB |
Output is correct |
52 |
Correct |
10 ms |
27588 KB |
Output is correct |
53 |
Correct |
9 ms |
27484 KB |
Output is correct |
54 |
Correct |
9 ms |
27484 KB |
Output is correct |
55 |
Correct |
14 ms |
27992 KB |
Output is correct |
56 |
Correct |
29 ms |
30412 KB |
Output is correct |
57 |
Correct |
47 ms |
32604 KB |
Output is correct |
58 |
Correct |
60 ms |
34140 KB |
Output is correct |
59 |
Correct |
78 ms |
36444 KB |
Output is correct |
60 |
Correct |
137 ms |
38460 KB |
Output is correct |
61 |
Correct |
122 ms |
40396 KB |
Output is correct |
62 |
Correct |
165 ms |
41932 KB |
Output is correct |
63 |
Correct |
172 ms |
44488 KB |
Output is correct |
64 |
Correct |
257 ms |
45232 KB |
Output is correct |
65 |
Correct |
108 ms |
83664 KB |
Output is correct |
66 |
Correct |
93 ms |
83600 KB |
Output is correct |
67 |
Correct |
93 ms |
41580 KB |
Output is correct |
68 |
Correct |
138 ms |
63824 KB |
Output is correct |
69 |
Correct |
158 ms |
60616 KB |
Output is correct |
70 |
Correct |
163 ms |
60752 KB |
Output is correct |
71 |
Correct |
167 ms |
55516 KB |
Output is correct |
72 |
Correct |
173 ms |
56660 KB |
Output is correct |
73 |
Correct |
172 ms |
52500 KB |
Output is correct |
74 |
Correct |
159 ms |
53116 KB |
Output is correct |
75 |
Correct |
157 ms |
50856 KB |
Output is correct |
76 |
Correct |
152 ms |
52260 KB |
Output is correct |
77 |
Correct |
153 ms |
52416 KB |
Output is correct |
78 |
Correct |
152 ms |
49912 KB |
Output is correct |
79 |
Correct |
144 ms |
53208 KB |
Output is correct |