#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 30, MOD = 1e9 + 7;
#define int ll
int n,m;
vector<pair<int,int>> g[N];
multiset<ll> st[N];
ll h[N],l[N],r[N];
int mx[N];
void merge(int v,int to){
// if((int)st[v].empty() < (int)st[to].size()){
// st[v].swap(st[to]);
// swap(mx[v],mx[to]);
// swap(l[v],l[to]);
// swap(r[v],r[to]);
// }
mx[v]++;
for(int j:st[to]){
st[v].insert(j);
}
}
void dfs(int v,int W = 0){
if(g[v].empty()){
st[v].insert(W);
st[v].insert(W);
mx[v] = 1;
h[v] = 0;
l[v] = r[v] = W;
return;
}
for(auto [to,w]:g[v]){
dfs(to,w);
merge(v,to);
}
while(mx[v] > 1){
mx[v]--;
st[v].erase(st[v].find(*st[v].rbegin()));
}
ll x = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
ll y = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
st[v].insert(x + W);
st[v].insert(y + W);
r[v] = (*st[v].rbegin());
l[v] = (*(++(st[v].rbegin())));
}
ll E = 0;
void test(){
cin >> n >> m;
for(int i = 2;i <= n + m;i++){
int a,b;
cin >> a >> b;
g[a].push_back({i,b});
E += b;
}
dfs(1);
st[1].erase(--st[1].end());
ll H = 0;
for(int i:st[1]){
E -= i;
}
cout << E << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
Compilation message
fireworks.cpp: In function 'void test()':
fireworks.cpp:63:8: warning: unused variable 'H' [-Wunused-variable]
63 | ll H = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28504 KB |
Output is correct |
2 |
Correct |
11 ms |
28508 KB |
Output is correct |
3 |
Correct |
11 ms |
28492 KB |
Output is correct |
4 |
Correct |
12 ms |
28508 KB |
Output is correct |
5 |
Correct |
10 ms |
28508 KB |
Output is correct |
6 |
Correct |
10 ms |
28508 KB |
Output is correct |
7 |
Correct |
11 ms |
28504 KB |
Output is correct |
8 |
Correct |
11 ms |
28636 KB |
Output is correct |
9 |
Correct |
11 ms |
28508 KB |
Output is correct |
10 |
Correct |
10 ms |
28448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
28508 KB |
Output is correct |
2 |
Correct |
11 ms |
28616 KB |
Output is correct |
3 |
Correct |
11 ms |
28508 KB |
Output is correct |
4 |
Correct |
11 ms |
28612 KB |
Output is correct |
5 |
Correct |
12 ms |
28504 KB |
Output is correct |
6 |
Correct |
15 ms |
28508 KB |
Output is correct |
7 |
Correct |
11 ms |
28676 KB |
Output is correct |
8 |
Correct |
11 ms |
28640 KB |
Output is correct |
9 |
Correct |
12 ms |
28504 KB |
Output is correct |
10 |
Correct |
12 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28540 KB |
Output is correct |
12 |
Correct |
12 ms |
28504 KB |
Output is correct |
13 |
Correct |
12 ms |
28504 KB |
Output is correct |
14 |
Correct |
12 ms |
28508 KB |
Output is correct |
15 |
Correct |
13 ms |
28600 KB |
Output is correct |
16 |
Correct |
13 ms |
28504 KB |
Output is correct |
17 |
Correct |
15 ms |
28880 KB |
Output is correct |
18 |
Correct |
13 ms |
28508 KB |
Output is correct |
19 |
Correct |
12 ms |
28508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28504 KB |
Output is correct |
2 |
Correct |
11 ms |
28508 KB |
Output is correct |
3 |
Correct |
11 ms |
28492 KB |
Output is correct |
4 |
Correct |
12 ms |
28508 KB |
Output is correct |
5 |
Correct |
10 ms |
28508 KB |
Output is correct |
6 |
Correct |
10 ms |
28508 KB |
Output is correct |
7 |
Correct |
11 ms |
28504 KB |
Output is correct |
8 |
Correct |
11 ms |
28636 KB |
Output is correct |
9 |
Correct |
11 ms |
28508 KB |
Output is correct |
10 |
Correct |
10 ms |
28448 KB |
Output is correct |
11 |
Correct |
10 ms |
28508 KB |
Output is correct |
12 |
Correct |
11 ms |
28616 KB |
Output is correct |
13 |
Correct |
11 ms |
28508 KB |
Output is correct |
14 |
Correct |
11 ms |
28612 KB |
Output is correct |
15 |
Correct |
12 ms |
28504 KB |
Output is correct |
16 |
Correct |
15 ms |
28508 KB |
Output is correct |
17 |
Correct |
11 ms |
28676 KB |
Output is correct |
18 |
Correct |
11 ms |
28640 KB |
Output is correct |
19 |
Correct |
12 ms |
28504 KB |
Output is correct |
20 |
Correct |
12 ms |
28508 KB |
Output is correct |
21 |
Correct |
12 ms |
28540 KB |
Output is correct |
22 |
Correct |
12 ms |
28504 KB |
Output is correct |
23 |
Correct |
12 ms |
28504 KB |
Output is correct |
24 |
Correct |
12 ms |
28508 KB |
Output is correct |
25 |
Correct |
13 ms |
28600 KB |
Output is correct |
26 |
Correct |
13 ms |
28504 KB |
Output is correct |
27 |
Correct |
15 ms |
28880 KB |
Output is correct |
28 |
Correct |
13 ms |
28508 KB |
Output is correct |
29 |
Correct |
12 ms |
28508 KB |
Output is correct |
30 |
Correct |
12 ms |
28760 KB |
Output is correct |
31 |
Correct |
13 ms |
29644 KB |
Output is correct |
32 |
Correct |
15 ms |
29788 KB |
Output is correct |
33 |
Correct |
17 ms |
30508 KB |
Output is correct |
34 |
Correct |
18 ms |
31324 KB |
Output is correct |
35 |
Correct |
24 ms |
34396 KB |
Output is correct |
36 |
Correct |
23 ms |
33624 KB |
Output is correct |
37 |
Correct |
23 ms |
33112 KB |
Output is correct |
38 |
Correct |
37 ms |
40700 KB |
Output is correct |
39 |
Correct |
25 ms |
34652 KB |
Output is correct |
40 |
Correct |
14 ms |
29932 KB |
Output is correct |
41 |
Correct |
13 ms |
29784 KB |
Output is correct |
42 |
Correct |
14 ms |
29272 KB |
Output is correct |
43 |
Correct |
289 ms |
176212 KB |
Output is correct |
44 |
Correct |
236 ms |
138320 KB |
Output is correct |
45 |
Correct |
193 ms |
136028 KB |
Output is correct |
46 |
Correct |
15 ms |
29788 KB |
Output is correct |
47 |
Correct |
15 ms |
30044 KB |
Output is correct |
48 |
Correct |
13 ms |
29600 KB |
Output is correct |
49 |
Correct |
14 ms |
29788 KB |
Output is correct |
50 |
Correct |
13 ms |
29532 KB |
Output is correct |
51 |
Correct |
13 ms |
29532 KB |
Output is correct |
52 |
Correct |
21 ms |
32604 KB |
Output is correct |
53 |
Correct |
16 ms |
30300 KB |
Output is correct |
54 |
Correct |
13 ms |
29904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28504 KB |
Output is correct |
2 |
Correct |
11 ms |
28508 KB |
Output is correct |
3 |
Correct |
11 ms |
28492 KB |
Output is correct |
4 |
Correct |
12 ms |
28508 KB |
Output is correct |
5 |
Correct |
10 ms |
28508 KB |
Output is correct |
6 |
Correct |
10 ms |
28508 KB |
Output is correct |
7 |
Correct |
11 ms |
28504 KB |
Output is correct |
8 |
Correct |
11 ms |
28636 KB |
Output is correct |
9 |
Correct |
11 ms |
28508 KB |
Output is correct |
10 |
Correct |
10 ms |
28448 KB |
Output is correct |
11 |
Correct |
10 ms |
28508 KB |
Output is correct |
12 |
Correct |
11 ms |
28616 KB |
Output is correct |
13 |
Correct |
11 ms |
28508 KB |
Output is correct |
14 |
Correct |
11 ms |
28612 KB |
Output is correct |
15 |
Correct |
12 ms |
28504 KB |
Output is correct |
16 |
Correct |
15 ms |
28508 KB |
Output is correct |
17 |
Correct |
11 ms |
28676 KB |
Output is correct |
18 |
Correct |
11 ms |
28640 KB |
Output is correct |
19 |
Correct |
12 ms |
28504 KB |
Output is correct |
20 |
Correct |
12 ms |
28508 KB |
Output is correct |
21 |
Correct |
12 ms |
28540 KB |
Output is correct |
22 |
Correct |
12 ms |
28504 KB |
Output is correct |
23 |
Correct |
12 ms |
28504 KB |
Output is correct |
24 |
Correct |
12 ms |
28508 KB |
Output is correct |
25 |
Correct |
13 ms |
28600 KB |
Output is correct |
26 |
Correct |
13 ms |
28504 KB |
Output is correct |
27 |
Correct |
15 ms |
28880 KB |
Output is correct |
28 |
Correct |
13 ms |
28508 KB |
Output is correct |
29 |
Correct |
12 ms |
28508 KB |
Output is correct |
30 |
Correct |
12 ms |
28760 KB |
Output is correct |
31 |
Correct |
13 ms |
29644 KB |
Output is correct |
32 |
Correct |
15 ms |
29788 KB |
Output is correct |
33 |
Correct |
17 ms |
30508 KB |
Output is correct |
34 |
Correct |
18 ms |
31324 KB |
Output is correct |
35 |
Correct |
24 ms |
34396 KB |
Output is correct |
36 |
Correct |
23 ms |
33624 KB |
Output is correct |
37 |
Correct |
23 ms |
33112 KB |
Output is correct |
38 |
Correct |
37 ms |
40700 KB |
Output is correct |
39 |
Correct |
25 ms |
34652 KB |
Output is correct |
40 |
Correct |
14 ms |
29932 KB |
Output is correct |
41 |
Correct |
13 ms |
29784 KB |
Output is correct |
42 |
Correct |
14 ms |
29272 KB |
Output is correct |
43 |
Correct |
289 ms |
176212 KB |
Output is correct |
44 |
Correct |
236 ms |
138320 KB |
Output is correct |
45 |
Correct |
193 ms |
136028 KB |
Output is correct |
46 |
Correct |
15 ms |
29788 KB |
Output is correct |
47 |
Correct |
15 ms |
30044 KB |
Output is correct |
48 |
Correct |
13 ms |
29600 KB |
Output is correct |
49 |
Correct |
14 ms |
29788 KB |
Output is correct |
50 |
Correct |
13 ms |
29532 KB |
Output is correct |
51 |
Correct |
13 ms |
29532 KB |
Output is correct |
52 |
Correct |
21 ms |
32604 KB |
Output is correct |
53 |
Correct |
16 ms |
30300 KB |
Output is correct |
54 |
Correct |
13 ms |
29904 KB |
Output is correct |
55 |
Correct |
62 ms |
58196 KB |
Output is correct |
56 |
Correct |
620 ms |
301832 KB |
Output is correct |
57 |
Runtime error |
918 ms |
524288 KB |
Execution killed with signal 9 |
58 |
Halted |
0 ms |
0 KB |
- |