#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
void solve() {
int N, M; cin >> N >> M;
vector<vector<int>> adj(N);
vector<int> len(N + M, 0);
for (int i = 1; i < N + M; i++) {
int u; cin >> u; u--;
adj[u].pb(i);
cin >> len[i];
}
if (M == 0) {
cout << 0 << "\n";
return;
}
vector<priority_queue<ll>> x(N + M);
vector<ll> y(N + M, 0);
vector<ll> d(N + M, 0);
for (int u = N + M - 1; u >= 0; u--) {
if (u >= N) {
x[u].push(len[u]);
x[u].push(len[u]);
d[u] = 1;
} else {
for (int v : adj[u]) {
if (sz(x[v]) > sz(x[u])) {
swap(x[u], x[v]);
swap(y[u], y[v]);
swap(d[u], d[v]);
}
}
int slope = 1;
for (int v : adj[u]) {
if (x[v].empty()) continue;
if (x[v].top() > x[u].top()) {
y[u] += y[v] + d[u] * (x[v].top() - x[u].top());
} else {
y[u] += y[v] + d[v] * (x[u].top() - x[v].top());
}
while (!x[v].empty()) {
x[u].push(x[v].top());
x[v].pop();
}
d[u] += d[v];
}
while (d[u] > 1) {
d[u]--;
ll n = x[u].top(); x[u].pop();
y[u] -= d[u] * (n - x[u].top());
}
ll n2 = x[u].top(); x[u].pop();
ll n1 = x[u].top(); x[u].pop();
n1 += len[u];
n2 += len[u];
x[u].push(n1);
x[u].push(n2);
}
}
cout << y[0] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
Compilation message
fireworks.cpp: In function 'void solve()':
fireworks.cpp:43:17: warning: unused variable 'slope' [-Wunused-variable]
43 | int slope = 1;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
728 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
904 KB |
Output is correct |
38 |
Correct |
3 ms |
1116 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
2 ms |
1024 KB |
Output is correct |
41 |
Correct |
1 ms |
856 KB |
Output is correct |
42 |
Correct |
1 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
1116 KB |
Output is correct |
44 |
Correct |
2 ms |
856 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1116 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
2 ms |
984 KB |
Output is correct |
50 |
Correct |
2 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
860 KB |
Output is correct |
54 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
728 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
904 KB |
Output is correct |
38 |
Correct |
3 ms |
1116 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
2 ms |
1024 KB |
Output is correct |
41 |
Correct |
1 ms |
856 KB |
Output is correct |
42 |
Correct |
1 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
1116 KB |
Output is correct |
44 |
Correct |
2 ms |
856 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1116 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
2 ms |
984 KB |
Output is correct |
50 |
Correct |
2 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
860 KB |
Output is correct |
54 |
Correct |
3 ms |
1116 KB |
Output is correct |
55 |
Correct |
5 ms |
2140 KB |
Output is correct |
56 |
Correct |
18 ms |
7120 KB |
Output is correct |
57 |
Correct |
38 ms |
12120 KB |
Output is correct |
58 |
Correct |
40 ms |
15056 KB |
Output is correct |
59 |
Correct |
65 ms |
19784 KB |
Output is correct |
60 |
Correct |
75 ms |
24320 KB |
Output is correct |
61 |
Correct |
125 ms |
28124 KB |
Output is correct |
62 |
Correct |
104 ms |
30920 KB |
Output is correct |
63 |
Correct |
143 ms |
36648 KB |
Output is correct |
64 |
Correct |
145 ms |
38600 KB |
Output is correct |
65 |
Correct |
62 ms |
36948 KB |
Output is correct |
66 |
Correct |
65 ms |
37056 KB |
Output is correct |
67 |
Correct |
62 ms |
36872 KB |
Output is correct |
68 |
Correct |
105 ms |
35532 KB |
Output is correct |
69 |
Correct |
130 ms |
35500 KB |
Output is correct |
70 |
Correct |
142 ms |
35536 KB |
Output is correct |
71 |
Correct |
138 ms |
44600 KB |
Output is correct |
72 |
Correct |
134 ms |
45208 KB |
Output is correct |
73 |
Correct |
132 ms |
42460 KB |
Output is correct |
74 |
Correct |
133 ms |
41152 KB |
Output is correct |
75 |
Correct |
117 ms |
40996 KB |
Output is correct |
76 |
Correct |
119 ms |
41544 KB |
Output is correct |
77 |
Correct |
122 ms |
39776 KB |
Output is correct |
78 |
Correct |
123 ms |
39380 KB |
Output is correct |
79 |
Correct |
110 ms |
39496 KB |
Output is correct |