#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MAXN = (int)3e5 + 5;
ll h0[MAXN], a0[MAXN];
priority_queue<ll> S[MAXN];
int par[MAXN], edge_len[MAXN];
vector<int> adj[MAXN];
int n, m;
int main() {
cin >> n >> m;
for (int i = 2; i <= n + m; i++) {
cin >> par[i] >> edge_len[i];
adj[par[i]].push_back(i);
}
for (int v = n + m; v > n; v--) {
h0[v] = edge_len[v];
a0[v] = -1;
S[v].push(edge_len[v]);
S[v].push(edge_len[v]);
}
for (int v = n; v > 0; v--) {
for (int to : adj[v]) {
h0[v] += h0[to];
a0[v] += a0[to];
if (sz(S[to]) > sz(S[v])) {
S[v].swap(S[to]);
}
while (!S[to].empty()) {
S[v].push(S[to].top());
S[to].pop();
}
}
while (!S[v].empty() && a0[v] + sz(S[v]) > 1) {
S[v].pop();
}
h0[v] += edge_len[v];
ll P = S[v].top();
S[v].pop();
ll Q = S[v].top();
S[v].pop();
S[v].push(Q + edge_len[v]);
S[v].push(P + edge_len[v]);
}
vector<ll> slopes;
while (!S[1].empty()) {
slopes.push_back(S[1].top());
S[1].pop();
}
reverse(all(slopes));
ll val = h0[1], slope = a0[1], pos = 0;
for (ll pt : slopes) {
val += (pt - pos) * slope;
pos = pt;
slope++;
if (slope == 0) break;
}
cout << val << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16720 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
9 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16716 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16716 KB |
Output is correct |
8 |
Correct |
12 ms |
16752 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
10 ms |
16716 KB |
Output is correct |
4 |
Correct |
12 ms |
16716 KB |
Output is correct |
5 |
Correct |
10 ms |
16716 KB |
Output is correct |
6 |
Correct |
12 ms |
16680 KB |
Output is correct |
7 |
Correct |
10 ms |
16748 KB |
Output is correct |
8 |
Correct |
10 ms |
16716 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16716 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
9 ms |
16716 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
10 ms |
16848 KB |
Output is correct |
15 |
Correct |
10 ms |
16716 KB |
Output is correct |
16 |
Correct |
12 ms |
16740 KB |
Output is correct |
17 |
Correct |
10 ms |
16716 KB |
Output is correct |
18 |
Correct |
10 ms |
16740 KB |
Output is correct |
19 |
Correct |
10 ms |
16744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16720 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
9 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16716 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16716 KB |
Output is correct |
8 |
Correct |
12 ms |
16752 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16704 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16716 KB |
Output is correct |
15 |
Correct |
10 ms |
16716 KB |
Output is correct |
16 |
Correct |
12 ms |
16680 KB |
Output is correct |
17 |
Correct |
10 ms |
16748 KB |
Output is correct |
18 |
Correct |
10 ms |
16716 KB |
Output is correct |
19 |
Correct |
9 ms |
16716 KB |
Output is correct |
20 |
Correct |
9 ms |
16716 KB |
Output is correct |
21 |
Correct |
9 ms |
16716 KB |
Output is correct |
22 |
Correct |
9 ms |
16716 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
10 ms |
16848 KB |
Output is correct |
25 |
Correct |
10 ms |
16716 KB |
Output is correct |
26 |
Correct |
12 ms |
16740 KB |
Output is correct |
27 |
Correct |
10 ms |
16716 KB |
Output is correct |
28 |
Correct |
10 ms |
16740 KB |
Output is correct |
29 |
Correct |
10 ms |
16744 KB |
Output is correct |
30 |
Correct |
10 ms |
16716 KB |
Output is correct |
31 |
Correct |
10 ms |
16820 KB |
Output is correct |
32 |
Correct |
11 ms |
16844 KB |
Output is correct |
33 |
Correct |
13 ms |
16844 KB |
Output is correct |
34 |
Correct |
13 ms |
16904 KB |
Output is correct |
35 |
Correct |
13 ms |
16972 KB |
Output is correct |
36 |
Correct |
15 ms |
16972 KB |
Output is correct |
37 |
Correct |
16 ms |
17100 KB |
Output is correct |
38 |
Correct |
15 ms |
17100 KB |
Output is correct |
39 |
Correct |
15 ms |
17104 KB |
Output is correct |
40 |
Correct |
13 ms |
16924 KB |
Output is correct |
41 |
Correct |
13 ms |
17028 KB |
Output is correct |
42 |
Correct |
14 ms |
17020 KB |
Output is correct |
43 |
Correct |
14 ms |
17108 KB |
Output is correct |
44 |
Correct |
15 ms |
17100 KB |
Output is correct |
45 |
Correct |
14 ms |
17004 KB |
Output is correct |
46 |
Correct |
15 ms |
17228 KB |
Output is correct |
47 |
Correct |
15 ms |
17316 KB |
Output is correct |
48 |
Correct |
14 ms |
17100 KB |
Output is correct |
49 |
Correct |
17 ms |
17180 KB |
Output is correct |
50 |
Correct |
14 ms |
17100 KB |
Output is correct |
51 |
Correct |
13 ms |
17100 KB |
Output is correct |
52 |
Correct |
14 ms |
17124 KB |
Output is correct |
53 |
Correct |
14 ms |
17228 KB |
Output is correct |
54 |
Correct |
14 ms |
17240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16720 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
9 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16716 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16716 KB |
Output is correct |
8 |
Correct |
12 ms |
16752 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16704 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16716 KB |
Output is correct |
15 |
Correct |
10 ms |
16716 KB |
Output is correct |
16 |
Correct |
12 ms |
16680 KB |
Output is correct |
17 |
Correct |
10 ms |
16748 KB |
Output is correct |
18 |
Correct |
10 ms |
16716 KB |
Output is correct |
19 |
Correct |
9 ms |
16716 KB |
Output is correct |
20 |
Correct |
9 ms |
16716 KB |
Output is correct |
21 |
Correct |
9 ms |
16716 KB |
Output is correct |
22 |
Correct |
9 ms |
16716 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
10 ms |
16848 KB |
Output is correct |
25 |
Correct |
10 ms |
16716 KB |
Output is correct |
26 |
Correct |
12 ms |
16740 KB |
Output is correct |
27 |
Correct |
10 ms |
16716 KB |
Output is correct |
28 |
Correct |
10 ms |
16740 KB |
Output is correct |
29 |
Correct |
10 ms |
16744 KB |
Output is correct |
30 |
Correct |
10 ms |
16716 KB |
Output is correct |
31 |
Correct |
10 ms |
16820 KB |
Output is correct |
32 |
Correct |
11 ms |
16844 KB |
Output is correct |
33 |
Correct |
13 ms |
16844 KB |
Output is correct |
34 |
Correct |
13 ms |
16904 KB |
Output is correct |
35 |
Correct |
13 ms |
16972 KB |
Output is correct |
36 |
Correct |
15 ms |
16972 KB |
Output is correct |
37 |
Correct |
16 ms |
17100 KB |
Output is correct |
38 |
Correct |
15 ms |
17100 KB |
Output is correct |
39 |
Correct |
15 ms |
17104 KB |
Output is correct |
40 |
Correct |
13 ms |
16924 KB |
Output is correct |
41 |
Correct |
13 ms |
17028 KB |
Output is correct |
42 |
Correct |
14 ms |
17020 KB |
Output is correct |
43 |
Correct |
14 ms |
17108 KB |
Output is correct |
44 |
Correct |
15 ms |
17100 KB |
Output is correct |
45 |
Correct |
14 ms |
17004 KB |
Output is correct |
46 |
Correct |
15 ms |
17228 KB |
Output is correct |
47 |
Correct |
15 ms |
17316 KB |
Output is correct |
48 |
Correct |
14 ms |
17100 KB |
Output is correct |
49 |
Correct |
17 ms |
17180 KB |
Output is correct |
50 |
Correct |
14 ms |
17100 KB |
Output is correct |
51 |
Correct |
13 ms |
17100 KB |
Output is correct |
52 |
Correct |
14 ms |
17124 KB |
Output is correct |
53 |
Correct |
14 ms |
17228 KB |
Output is correct |
54 |
Correct |
14 ms |
17240 KB |
Output is correct |
55 |
Correct |
23 ms |
17792 KB |
Output is correct |
56 |
Correct |
64 ms |
20856 KB |
Output is correct |
57 |
Correct |
106 ms |
23512 KB |
Output is correct |
58 |
Correct |
143 ms |
25664 KB |
Output is correct |
59 |
Correct |
178 ms |
28516 KB |
Output is correct |
60 |
Correct |
234 ms |
31420 KB |
Output is correct |
61 |
Correct |
263 ms |
33640 KB |
Output is correct |
62 |
Correct |
303 ms |
35412 KB |
Output is correct |
63 |
Correct |
371 ms |
38956 KB |
Output is correct |
64 |
Correct |
415 ms |
39560 KB |
Output is correct |
65 |
Correct |
267 ms |
33068 KB |
Output is correct |
66 |
Correct |
290 ms |
33160 KB |
Output is correct |
67 |
Correct |
276 ms |
33136 KB |
Output is correct |
68 |
Correct |
341 ms |
37328 KB |
Output is correct |
69 |
Correct |
387 ms |
37600 KB |
Output is correct |
70 |
Correct |
400 ms |
37504 KB |
Output is correct |
71 |
Correct |
386 ms |
52984 KB |
Output is correct |
72 |
Correct |
383 ms |
52580 KB |
Output is correct |
73 |
Correct |
359 ms |
49204 KB |
Output is correct |
74 |
Correct |
363 ms |
49320 KB |
Output is correct |
75 |
Correct |
365 ms |
48640 KB |
Output is correct |
76 |
Correct |
406 ms |
48516 KB |
Output is correct |
77 |
Correct |
372 ms |
47328 KB |
Output is correct |
78 |
Correct |
365 ms |
46220 KB |
Output is correct |
79 |
Correct |
339 ms |
45448 KB |
Output is correct |