#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <queue>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
class Slope{
public:
ll a;
ll b;
std::priority_queue<ll> pq;
Slope(){
a = b = 0;
}
int size(){
return pq.size();
}
ll top(){
return pq.top();
}
void pop(){
pq.pop();
}
void push(ll val){
pq.push(val);
}
void flatten(){
while(1 < a){
a--;
b += pq.top();
pq.pop();
}
}
void shift(ll val){
assert(a == 1);
ll p1 = pq.top();
pq.pop();
ll p2 = pq.top();
pq.pop();
pq.push(p2 + val);
pq.push(p1 + val);
b -= val;
}
};
void _add(Slope &x, Slope &y){
if(x.size() < y.size())
std::swap(x, y);
x.a += y.a;
x.b += y.b;
while(0 < y.size()){
x.push(y.top());
y.pop();
}
}
int const nmax = 300000;
std::vector<int> g[1 + nmax];
int upper[1 + nmax];
Slope dp[1 + nmax];
void dfs(int node){
if(g[node].size() == 0){
dp[node].push(0);
dp[node].push(0);
dp[node].a = 1;
dp[node].b = 0;
}
for(int h = 0; h < g[node].size(); h++){
int to = g[node][h];
dfs(to);
_add(dp[node], dp[to]);
}
dp[node].flatten();
dp[node].shift(upper[node]);
}
int main()
{
int n, m;
std::cin >> n >> m;
for(int i = 2;i <= n + m; i++){
int far;
std::cin >> far >> upper[i];
g[far].push_back(i);
}
dfs(1);
std::cout << dp[1].top() + dp[1].b;
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:77:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
19 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
19 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
18 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
18 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
18 ms |
21480 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
22 ms |
21504 KB |
Output is correct |
7 |
Correct |
20 ms |
21504 KB |
Output is correct |
8 |
Correct |
18 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
17 ms |
21504 KB |
Output is correct |
11 |
Correct |
18 ms |
21504 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
18 ms |
21504 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
18 ms |
21504 KB |
Output is correct |
18 |
Correct |
18 ms |
21456 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
19 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
19 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
18 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21504 KB |
Output is correct |
12 |
Correct |
18 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
18 ms |
21480 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
22 ms |
21504 KB |
Output is correct |
17 |
Correct |
20 ms |
21504 KB |
Output is correct |
18 |
Correct |
18 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
18 ms |
21504 KB |
Output is correct |
22 |
Correct |
17 ms |
21504 KB |
Output is correct |
23 |
Correct |
17 ms |
21504 KB |
Output is correct |
24 |
Correct |
18 ms |
21504 KB |
Output is correct |
25 |
Correct |
17 ms |
21504 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
18 ms |
21504 KB |
Output is correct |
28 |
Correct |
18 ms |
21456 KB |
Output is correct |
29 |
Correct |
17 ms |
21504 KB |
Output is correct |
30 |
Correct |
18 ms |
21504 KB |
Output is correct |
31 |
Correct |
19 ms |
21504 KB |
Output is correct |
32 |
Correct |
20 ms |
21504 KB |
Output is correct |
33 |
Correct |
20 ms |
21632 KB |
Output is correct |
34 |
Correct |
21 ms |
21632 KB |
Output is correct |
35 |
Correct |
21 ms |
21624 KB |
Output is correct |
36 |
Correct |
23 ms |
21760 KB |
Output is correct |
37 |
Correct |
25 ms |
21760 KB |
Output is correct |
38 |
Correct |
26 ms |
21888 KB |
Output is correct |
39 |
Correct |
26 ms |
21752 KB |
Output is correct |
40 |
Correct |
22 ms |
22272 KB |
Output is correct |
41 |
Correct |
25 ms |
22272 KB |
Output is correct |
42 |
Correct |
23 ms |
21760 KB |
Output is correct |
43 |
Correct |
23 ms |
22016 KB |
Output is correct |
44 |
Correct |
29 ms |
22144 KB |
Output is correct |
45 |
Correct |
24 ms |
22016 KB |
Output is correct |
46 |
Correct |
23 ms |
22016 KB |
Output is correct |
47 |
Correct |
23 ms |
21888 KB |
Output is correct |
48 |
Correct |
25 ms |
21760 KB |
Output is correct |
49 |
Correct |
23 ms |
21760 KB |
Output is correct |
50 |
Correct |
22 ms |
21760 KB |
Output is correct |
51 |
Correct |
21 ms |
21760 KB |
Output is correct |
52 |
Correct |
23 ms |
21760 KB |
Output is correct |
53 |
Correct |
25 ms |
21888 KB |
Output is correct |
54 |
Correct |
24 ms |
21808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
19 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
19 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
18 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21504 KB |
Output is correct |
12 |
Correct |
18 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
18 ms |
21480 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
22 ms |
21504 KB |
Output is correct |
17 |
Correct |
20 ms |
21504 KB |
Output is correct |
18 |
Correct |
18 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
18 ms |
21504 KB |
Output is correct |
22 |
Correct |
17 ms |
21504 KB |
Output is correct |
23 |
Correct |
17 ms |
21504 KB |
Output is correct |
24 |
Correct |
18 ms |
21504 KB |
Output is correct |
25 |
Correct |
17 ms |
21504 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
18 ms |
21504 KB |
Output is correct |
28 |
Correct |
18 ms |
21456 KB |
Output is correct |
29 |
Correct |
17 ms |
21504 KB |
Output is correct |
30 |
Correct |
18 ms |
21504 KB |
Output is correct |
31 |
Correct |
19 ms |
21504 KB |
Output is correct |
32 |
Correct |
20 ms |
21504 KB |
Output is correct |
33 |
Correct |
20 ms |
21632 KB |
Output is correct |
34 |
Correct |
21 ms |
21632 KB |
Output is correct |
35 |
Correct |
21 ms |
21624 KB |
Output is correct |
36 |
Correct |
23 ms |
21760 KB |
Output is correct |
37 |
Correct |
25 ms |
21760 KB |
Output is correct |
38 |
Correct |
26 ms |
21888 KB |
Output is correct |
39 |
Correct |
26 ms |
21752 KB |
Output is correct |
40 |
Correct |
22 ms |
22272 KB |
Output is correct |
41 |
Correct |
25 ms |
22272 KB |
Output is correct |
42 |
Correct |
23 ms |
21760 KB |
Output is correct |
43 |
Correct |
23 ms |
22016 KB |
Output is correct |
44 |
Correct |
29 ms |
22144 KB |
Output is correct |
45 |
Correct |
24 ms |
22016 KB |
Output is correct |
46 |
Correct |
23 ms |
22016 KB |
Output is correct |
47 |
Correct |
23 ms |
21888 KB |
Output is correct |
48 |
Correct |
25 ms |
21760 KB |
Output is correct |
49 |
Correct |
23 ms |
21760 KB |
Output is correct |
50 |
Correct |
22 ms |
21760 KB |
Output is correct |
51 |
Correct |
21 ms |
21760 KB |
Output is correct |
52 |
Correct |
23 ms |
21760 KB |
Output is correct |
53 |
Correct |
25 ms |
21888 KB |
Output is correct |
54 |
Correct |
24 ms |
21808 KB |
Output is correct |
55 |
Correct |
37 ms |
22392 KB |
Output is correct |
56 |
Correct |
86 ms |
24704 KB |
Output is correct |
57 |
Correct |
150 ms |
27132 KB |
Output is correct |
58 |
Correct |
191 ms |
28656 KB |
Output is correct |
59 |
Correct |
249 ms |
30960 KB |
Output is correct |
60 |
Correct |
317 ms |
33392 KB |
Output is correct |
61 |
Correct |
360 ms |
35512 KB |
Output is correct |
62 |
Correct |
393 ms |
36720 KB |
Output is correct |
63 |
Correct |
490 ms |
39532 KB |
Output is correct |
64 |
Correct |
539 ms |
40376 KB |
Output is correct |
65 |
Correct |
392 ms |
69752 KB |
Output is correct |
66 |
Correct |
398 ms |
69752 KB |
Output is correct |
67 |
Correct |
395 ms |
36984 KB |
Output is correct |
68 |
Correct |
440 ms |
54796 KB |
Output is correct |
69 |
Correct |
474 ms |
51876 KB |
Output is correct |
70 |
Correct |
463 ms |
51948 KB |
Output is correct |
71 |
Correct |
446 ms |
49508 KB |
Output is correct |
72 |
Correct |
454 ms |
49892 KB |
Output is correct |
73 |
Correct |
395 ms |
45920 KB |
Output is correct |
74 |
Correct |
413 ms |
46024 KB |
Output is correct |
75 |
Correct |
433 ms |
44900 KB |
Output is correct |
76 |
Correct |
395 ms |
45008 KB |
Output is correct |
77 |
Correct |
423 ms |
44904 KB |
Output is correct |
78 |
Correct |
412 ms |
42596 KB |
Output is correct |
79 |
Correct |
370 ms |
43744 KB |
Output is correct |