#include<bits/stdc++.h>
using namespace std;
struct slope {
priority_queue<long long> cp;
long long initslope;
long long inity;
slope () {
cp = priority_queue<long long>();
initslope = 0;
inity = 0;
}
};
void merge (slope &a, slope &b) {
if (a.cp.size() < b.cp.size()) swap(a, b);
a.initslope += b.initslope;
a.inity += b.inity;
while (!b.cp.empty()) {
a.cp.push(b.cp.top());
b.cp.pop();
}
}
vector<int> g[300100];
long long weight[300100];
slope dfs (int u) { // rightmost slope always 1
slope now;
if (g[u].size() == 0) {
now.inity = weight[u];
now.initslope = -1;
now.cp.push(weight[u]);
now.cp.push(weight[u]);
return now;
}
for (auto x : g[u]) {
slope child = dfs(x);
merge(now, child);
}
// rightmost slope of now is g[u].size()
for (int i = 1; i < g[u].size(); i++) now.cp.pop();
// right most slope is now 1
// shift graph with weight[u]
now.inity += weight[u];
auto a = now.cp.top(); now.cp.pop();
auto b = now.cp.top(); now.cp.pop();
now.cp.push(a+weight[u]);
now.cp.push(b+weight[u]);
return now;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m; cin >> n >> m;
for (int i = 2; i <= n+m; i++) {
int a, b; cin >> a >> b;
g[a].push_back(i);
weight[i] = b;
}
slope res = dfs(1);
long long ans = res.inity;
res.cp.pop();
long long last = res.cp.top();
res.cp.pop();
int i = 1;
while (!res.cp.empty()) {
ans -= i*(last - res.cp.top());
last = res.cp.top();
res.cp.pop();
i++;
}
ans -= i*last;
cout << ans << endl;
return 0;
}
Compilation message
fireworks.cpp: In function 'slope dfs(int)':
fireworks.cpp:42:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < g[u].size(); i++) now.cp.pop();
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
11 ms |
7424 KB |
Output is correct |
4 |
Correct |
8 ms |
7424 KB |
Output is correct |
5 |
Correct |
10 ms |
7424 KB |
Output is correct |
6 |
Correct |
8 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7424 KB |
Output is correct |
12 |
Correct |
10 ms |
7424 KB |
Output is correct |
13 |
Correct |
9 ms |
7424 KB |
Output is correct |
14 |
Correct |
9 ms |
7424 KB |
Output is correct |
15 |
Correct |
10 ms |
7424 KB |
Output is correct |
16 |
Correct |
10 ms |
7424 KB |
Output is correct |
17 |
Correct |
10 ms |
7424 KB |
Output is correct |
18 |
Correct |
9 ms |
7424 KB |
Output is correct |
19 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7424 KB |
Output is correct |
12 |
Correct |
10 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7424 KB |
Output is correct |
14 |
Correct |
8 ms |
7424 KB |
Output is correct |
15 |
Correct |
10 ms |
7424 KB |
Output is correct |
16 |
Correct |
8 ms |
7424 KB |
Output is correct |
17 |
Correct |
8 ms |
7424 KB |
Output is correct |
18 |
Correct |
9 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
20 |
Correct |
9 ms |
7424 KB |
Output is correct |
21 |
Correct |
10 ms |
7424 KB |
Output is correct |
22 |
Correct |
10 ms |
7424 KB |
Output is correct |
23 |
Correct |
9 ms |
7424 KB |
Output is correct |
24 |
Correct |
9 ms |
7424 KB |
Output is correct |
25 |
Correct |
10 ms |
7424 KB |
Output is correct |
26 |
Correct |
10 ms |
7424 KB |
Output is correct |
27 |
Correct |
10 ms |
7424 KB |
Output is correct |
28 |
Correct |
9 ms |
7424 KB |
Output is correct |
29 |
Correct |
8 ms |
7424 KB |
Output is correct |
30 |
Correct |
9 ms |
7424 KB |
Output is correct |
31 |
Correct |
9 ms |
7424 KB |
Output is correct |
32 |
Correct |
9 ms |
7424 KB |
Output is correct |
33 |
Correct |
10 ms |
7552 KB |
Output is correct |
34 |
Correct |
12 ms |
7552 KB |
Output is correct |
35 |
Correct |
12 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
7552 KB |
Output is correct |
37 |
Correct |
12 ms |
7680 KB |
Output is correct |
38 |
Correct |
11 ms |
7680 KB |
Output is correct |
39 |
Correct |
15 ms |
7680 KB |
Output is correct |
40 |
Correct |
12 ms |
8320 KB |
Output is correct |
41 |
Correct |
11 ms |
8320 KB |
Output is correct |
42 |
Correct |
12 ms |
7680 KB |
Output is correct |
43 |
Correct |
13 ms |
7936 KB |
Output is correct |
44 |
Correct |
13 ms |
7808 KB |
Output is correct |
45 |
Correct |
13 ms |
7808 KB |
Output is correct |
46 |
Correct |
14 ms |
7680 KB |
Output is correct |
47 |
Correct |
14 ms |
7680 KB |
Output is correct |
48 |
Correct |
13 ms |
7680 KB |
Output is correct |
49 |
Correct |
12 ms |
7680 KB |
Output is correct |
50 |
Correct |
13 ms |
7680 KB |
Output is correct |
51 |
Correct |
11 ms |
7552 KB |
Output is correct |
52 |
Correct |
12 ms |
7680 KB |
Output is correct |
53 |
Correct |
13 ms |
7680 KB |
Output is correct |
54 |
Correct |
13 ms |
7680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7424 KB |
Output is correct |
12 |
Correct |
10 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7424 KB |
Output is correct |
14 |
Correct |
8 ms |
7424 KB |
Output is correct |
15 |
Correct |
10 ms |
7424 KB |
Output is correct |
16 |
Correct |
8 ms |
7424 KB |
Output is correct |
17 |
Correct |
8 ms |
7424 KB |
Output is correct |
18 |
Correct |
9 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
20 |
Correct |
9 ms |
7424 KB |
Output is correct |
21 |
Correct |
10 ms |
7424 KB |
Output is correct |
22 |
Correct |
10 ms |
7424 KB |
Output is correct |
23 |
Correct |
9 ms |
7424 KB |
Output is correct |
24 |
Correct |
9 ms |
7424 KB |
Output is correct |
25 |
Correct |
10 ms |
7424 KB |
Output is correct |
26 |
Correct |
10 ms |
7424 KB |
Output is correct |
27 |
Correct |
10 ms |
7424 KB |
Output is correct |
28 |
Correct |
9 ms |
7424 KB |
Output is correct |
29 |
Correct |
8 ms |
7424 KB |
Output is correct |
30 |
Correct |
9 ms |
7424 KB |
Output is correct |
31 |
Correct |
9 ms |
7424 KB |
Output is correct |
32 |
Correct |
9 ms |
7424 KB |
Output is correct |
33 |
Correct |
10 ms |
7552 KB |
Output is correct |
34 |
Correct |
12 ms |
7552 KB |
Output is correct |
35 |
Correct |
12 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
7552 KB |
Output is correct |
37 |
Correct |
12 ms |
7680 KB |
Output is correct |
38 |
Correct |
11 ms |
7680 KB |
Output is correct |
39 |
Correct |
15 ms |
7680 KB |
Output is correct |
40 |
Correct |
12 ms |
8320 KB |
Output is correct |
41 |
Correct |
11 ms |
8320 KB |
Output is correct |
42 |
Correct |
12 ms |
7680 KB |
Output is correct |
43 |
Correct |
13 ms |
7936 KB |
Output is correct |
44 |
Correct |
13 ms |
7808 KB |
Output is correct |
45 |
Correct |
13 ms |
7808 KB |
Output is correct |
46 |
Correct |
14 ms |
7680 KB |
Output is correct |
47 |
Correct |
14 ms |
7680 KB |
Output is correct |
48 |
Correct |
13 ms |
7680 KB |
Output is correct |
49 |
Correct |
12 ms |
7680 KB |
Output is correct |
50 |
Correct |
13 ms |
7680 KB |
Output is correct |
51 |
Correct |
11 ms |
7552 KB |
Output is correct |
52 |
Correct |
12 ms |
7680 KB |
Output is correct |
53 |
Correct |
13 ms |
7680 KB |
Output is correct |
54 |
Correct |
13 ms |
7680 KB |
Output is correct |
55 |
Correct |
19 ms |
8064 KB |
Output is correct |
56 |
Correct |
52 ms |
9976 KB |
Output is correct |
57 |
Correct |
89 ms |
11656 KB |
Output is correct |
58 |
Correct |
88 ms |
13004 KB |
Output is correct |
59 |
Correct |
133 ms |
14636 KB |
Output is correct |
60 |
Correct |
154 ms |
16120 KB |
Output is correct |
61 |
Correct |
174 ms |
17840 KB |
Output is correct |
62 |
Correct |
259 ms |
18652 KB |
Output is correct |
63 |
Correct |
299 ms |
20464 KB |
Output is correct |
64 |
Correct |
241 ms |
20952 KB |
Output is correct |
65 |
Correct |
134 ms |
61560 KB |
Output is correct |
66 |
Correct |
144 ms |
61584 KB |
Output is correct |
67 |
Correct |
142 ms |
24184 KB |
Output is correct |
68 |
Correct |
212 ms |
40168 KB |
Output is correct |
69 |
Correct |
265 ms |
36972 KB |
Output is correct |
70 |
Correct |
247 ms |
37028 KB |
Output is correct |
71 |
Correct |
239 ms |
20412 KB |
Output is correct |
72 |
Correct |
245 ms |
20272 KB |
Output is correct |
73 |
Correct |
220 ms |
19564 KB |
Output is correct |
74 |
Correct |
212 ms |
19672 KB |
Output is correct |
75 |
Correct |
217 ms |
19376 KB |
Output is correct |
76 |
Correct |
224 ms |
19404 KB |
Output is correct |
77 |
Correct |
222 ms |
20176 KB |
Output is correct |
78 |
Correct |
218 ms |
20044 KB |
Output is correct |
79 |
Correct |
246 ms |
22752 KB |
Output is correct |