#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
struct Node {
long long startingValue;
long long startingSlope;
priority_queue<int> pq;
};
int n, m, c[N], p[N];
vector<int> G[N];
Node a[N];
void dfs(int u) {
if (u > n) return;
int mx = 0;
for (auto v : G[u]) {
dfs(v);
if (!mx || a[v].pq.size() > a[mx].pq.size()) mx = v;
}
swap(a[mx], a[u]);
for (auto v : G[u]) {
if (v == mx) continue;
while (a[v].pq.size()) {
a[u].pq.push(a[v].pq.top()), a[v].pq.pop();
}
a[u].startingValue += a[v].startingValue;
a[u].startingSlope += a[v].startingSlope;
}
while (a[u].startingSlope + a[u].pq.size() > 1) a[u].pq.pop();
int x1 = a[u].pq.top(); a[u].pq.pop();
int x0 = a[u].pq.top(); a[u].pq.pop();
a[u].startingValue += c[u];
a[u].pq.push(x0 + c[u]), a[u].pq.push(x1 + c[u]);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 2; i <= n + m; ++i) {
cin >> p[i] >> c[i];
G[p[i]].push_back(i);
}
for (int i = n + 1; i <= n + m; ++i) {
a[i].startingSlope = -1;
a[i].startingValue = c[i];
a[i].pq.push(c[i]), a[i].pq.push(c[i]);
}
dfs(1);
vector<int> v;
while (a[1].pq.size()) {
v.push_back(a[1].pq.top()), a[1].pq.pop();
}
reverse(v.begin(), v.end());
long long cur = 0;
long long curSlope = a[1].startingSlope;
long long res = a[1].startingValue;
// cout << curSlope << ' ' << res << '\n';
// for (auto i : v) cout << i << ' '; cout << '\n';
for (int i = 0; i < v.size(); ++i) {
res += (v[i] - cur) * curSlope, curSlope++, cur = v[i];
}
cout << res;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:62:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); ++i) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25616 KB |
Output is correct |
2 |
Correct |
0 ms |
25616 KB |
Output is correct |
3 |
Correct |
7 ms |
25616 KB |
Output is correct |
4 |
Correct |
6 ms |
25616 KB |
Output is correct |
5 |
Correct |
10 ms |
25616 KB |
Output is correct |
6 |
Correct |
10 ms |
25616 KB |
Output is correct |
7 |
Correct |
0 ms |
25616 KB |
Output is correct |
8 |
Correct |
5 ms |
25616 KB |
Output is correct |
9 |
Correct |
6 ms |
25616 KB |
Output is correct |
10 |
Correct |
3 ms |
25616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25616 KB |
Output is correct |
2 |
Correct |
9 ms |
25616 KB |
Output is correct |
3 |
Correct |
3 ms |
25616 KB |
Output is correct |
4 |
Correct |
3 ms |
25616 KB |
Output is correct |
5 |
Correct |
7 ms |
25616 KB |
Output is correct |
6 |
Correct |
7 ms |
25616 KB |
Output is correct |
7 |
Correct |
3 ms |
25616 KB |
Output is correct |
8 |
Correct |
3 ms |
25616 KB |
Output is correct |
9 |
Correct |
3 ms |
25616 KB |
Output is correct |
10 |
Correct |
3 ms |
25616 KB |
Output is correct |
11 |
Correct |
3 ms |
25616 KB |
Output is correct |
12 |
Correct |
6 ms |
25616 KB |
Output is correct |
13 |
Correct |
0 ms |
25616 KB |
Output is correct |
14 |
Correct |
3 ms |
25616 KB |
Output is correct |
15 |
Correct |
0 ms |
25616 KB |
Output is correct |
16 |
Correct |
7 ms |
25616 KB |
Output is correct |
17 |
Correct |
6 ms |
25616 KB |
Output is correct |
18 |
Correct |
3 ms |
25616 KB |
Output is correct |
19 |
Correct |
9 ms |
25616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25616 KB |
Output is correct |
2 |
Correct |
0 ms |
25616 KB |
Output is correct |
3 |
Correct |
7 ms |
25616 KB |
Output is correct |
4 |
Correct |
6 ms |
25616 KB |
Output is correct |
5 |
Correct |
10 ms |
25616 KB |
Output is correct |
6 |
Correct |
10 ms |
25616 KB |
Output is correct |
7 |
Correct |
0 ms |
25616 KB |
Output is correct |
8 |
Correct |
5 ms |
25616 KB |
Output is correct |
9 |
Correct |
6 ms |
25616 KB |
Output is correct |
10 |
Correct |
3 ms |
25616 KB |
Output is correct |
11 |
Correct |
7 ms |
25616 KB |
Output is correct |
12 |
Correct |
9 ms |
25616 KB |
Output is correct |
13 |
Correct |
3 ms |
25616 KB |
Output is correct |
14 |
Correct |
3 ms |
25616 KB |
Output is correct |
15 |
Correct |
7 ms |
25616 KB |
Output is correct |
16 |
Correct |
7 ms |
25616 KB |
Output is correct |
17 |
Correct |
3 ms |
25616 KB |
Output is correct |
18 |
Correct |
3 ms |
25616 KB |
Output is correct |
19 |
Correct |
3 ms |
25616 KB |
Output is correct |
20 |
Correct |
3 ms |
25616 KB |
Output is correct |
21 |
Correct |
3 ms |
25616 KB |
Output is correct |
22 |
Correct |
6 ms |
25616 KB |
Output is correct |
23 |
Correct |
0 ms |
25616 KB |
Output is correct |
24 |
Correct |
3 ms |
25616 KB |
Output is correct |
25 |
Correct |
0 ms |
25616 KB |
Output is correct |
26 |
Correct |
7 ms |
25616 KB |
Output is correct |
27 |
Correct |
6 ms |
25616 KB |
Output is correct |
28 |
Correct |
3 ms |
25616 KB |
Output is correct |
29 |
Correct |
9 ms |
25616 KB |
Output is correct |
30 |
Incorrect |
6 ms |
25616 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25616 KB |
Output is correct |
2 |
Correct |
0 ms |
25616 KB |
Output is correct |
3 |
Correct |
7 ms |
25616 KB |
Output is correct |
4 |
Correct |
6 ms |
25616 KB |
Output is correct |
5 |
Correct |
10 ms |
25616 KB |
Output is correct |
6 |
Correct |
10 ms |
25616 KB |
Output is correct |
7 |
Correct |
0 ms |
25616 KB |
Output is correct |
8 |
Correct |
5 ms |
25616 KB |
Output is correct |
9 |
Correct |
6 ms |
25616 KB |
Output is correct |
10 |
Correct |
3 ms |
25616 KB |
Output is correct |
11 |
Correct |
7 ms |
25616 KB |
Output is correct |
12 |
Correct |
9 ms |
25616 KB |
Output is correct |
13 |
Correct |
3 ms |
25616 KB |
Output is correct |
14 |
Correct |
3 ms |
25616 KB |
Output is correct |
15 |
Correct |
7 ms |
25616 KB |
Output is correct |
16 |
Correct |
7 ms |
25616 KB |
Output is correct |
17 |
Correct |
3 ms |
25616 KB |
Output is correct |
18 |
Correct |
3 ms |
25616 KB |
Output is correct |
19 |
Correct |
3 ms |
25616 KB |
Output is correct |
20 |
Correct |
3 ms |
25616 KB |
Output is correct |
21 |
Correct |
3 ms |
25616 KB |
Output is correct |
22 |
Correct |
6 ms |
25616 KB |
Output is correct |
23 |
Correct |
0 ms |
25616 KB |
Output is correct |
24 |
Correct |
3 ms |
25616 KB |
Output is correct |
25 |
Correct |
0 ms |
25616 KB |
Output is correct |
26 |
Correct |
7 ms |
25616 KB |
Output is correct |
27 |
Correct |
6 ms |
25616 KB |
Output is correct |
28 |
Correct |
3 ms |
25616 KB |
Output is correct |
29 |
Correct |
9 ms |
25616 KB |
Output is correct |
30 |
Incorrect |
6 ms |
25616 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |