#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)1e5 + 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 |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
4 ms |
5708 KB |
Output is correct |
7 |
Correct |
3 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
4 ms |
5708 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
3 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
5 ms |
5708 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
4 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
4 ms |
5708 KB |
Output is correct |
7 |
Correct |
3 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
5708 KB |
Output is correct |
11 |
Correct |
3 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5708 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
3 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
4 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
3 ms |
5708 KB |
Output is correct |
22 |
Correct |
4 ms |
5708 KB |
Output is correct |
23 |
Correct |
5 ms |
5708 KB |
Output is correct |
24 |
Correct |
4 ms |
5708 KB |
Output is correct |
25 |
Correct |
4 ms |
5708 KB |
Output is correct |
26 |
Correct |
4 ms |
5708 KB |
Output is correct |
27 |
Correct |
4 ms |
5708 KB |
Output is correct |
28 |
Correct |
4 ms |
5708 KB |
Output is correct |
29 |
Correct |
4 ms |
5708 KB |
Output is correct |
30 |
Correct |
4 ms |
5708 KB |
Output is correct |
31 |
Correct |
4 ms |
5836 KB |
Output is correct |
32 |
Correct |
5 ms |
5836 KB |
Output is correct |
33 |
Correct |
6 ms |
5964 KB |
Output is correct |
34 |
Correct |
7 ms |
5928 KB |
Output is correct |
35 |
Correct |
7 ms |
5960 KB |
Output is correct |
36 |
Correct |
8 ms |
6004 KB |
Output is correct |
37 |
Correct |
8 ms |
6092 KB |
Output is correct |
38 |
Correct |
8 ms |
6092 KB |
Output is correct |
39 |
Correct |
8 ms |
6092 KB |
Output is correct |
40 |
Correct |
7 ms |
5964 KB |
Output is correct |
41 |
Correct |
7 ms |
5964 KB |
Output is correct |
42 |
Correct |
7 ms |
6048 KB |
Output is correct |
43 |
Correct |
10 ms |
6092 KB |
Output is correct |
44 |
Correct |
8 ms |
6048 KB |
Output is correct |
45 |
Correct |
9 ms |
6092 KB |
Output is correct |
46 |
Correct |
11 ms |
6264 KB |
Output is correct |
47 |
Correct |
9 ms |
6220 KB |
Output is correct |
48 |
Correct |
8 ms |
6216 KB |
Output is correct |
49 |
Correct |
9 ms |
6220 KB |
Output is correct |
50 |
Correct |
8 ms |
6092 KB |
Output is correct |
51 |
Correct |
7 ms |
6092 KB |
Output is correct |
52 |
Correct |
8 ms |
6192 KB |
Output is correct |
53 |
Correct |
8 ms |
6212 KB |
Output is correct |
54 |
Correct |
8 ms |
6304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
4 ms |
5708 KB |
Output is correct |
7 |
Correct |
3 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
5708 KB |
Output is correct |
11 |
Correct |
3 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5708 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
3 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
4 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
3 ms |
5708 KB |
Output is correct |
22 |
Correct |
4 ms |
5708 KB |
Output is correct |
23 |
Correct |
5 ms |
5708 KB |
Output is correct |
24 |
Correct |
4 ms |
5708 KB |
Output is correct |
25 |
Correct |
4 ms |
5708 KB |
Output is correct |
26 |
Correct |
4 ms |
5708 KB |
Output is correct |
27 |
Correct |
4 ms |
5708 KB |
Output is correct |
28 |
Correct |
4 ms |
5708 KB |
Output is correct |
29 |
Correct |
4 ms |
5708 KB |
Output is correct |
30 |
Correct |
4 ms |
5708 KB |
Output is correct |
31 |
Correct |
4 ms |
5836 KB |
Output is correct |
32 |
Correct |
5 ms |
5836 KB |
Output is correct |
33 |
Correct |
6 ms |
5964 KB |
Output is correct |
34 |
Correct |
7 ms |
5928 KB |
Output is correct |
35 |
Correct |
7 ms |
5960 KB |
Output is correct |
36 |
Correct |
8 ms |
6004 KB |
Output is correct |
37 |
Correct |
8 ms |
6092 KB |
Output is correct |
38 |
Correct |
8 ms |
6092 KB |
Output is correct |
39 |
Correct |
8 ms |
6092 KB |
Output is correct |
40 |
Correct |
7 ms |
5964 KB |
Output is correct |
41 |
Correct |
7 ms |
5964 KB |
Output is correct |
42 |
Correct |
7 ms |
6048 KB |
Output is correct |
43 |
Correct |
10 ms |
6092 KB |
Output is correct |
44 |
Correct |
8 ms |
6048 KB |
Output is correct |
45 |
Correct |
9 ms |
6092 KB |
Output is correct |
46 |
Correct |
11 ms |
6264 KB |
Output is correct |
47 |
Correct |
9 ms |
6220 KB |
Output is correct |
48 |
Correct |
8 ms |
6216 KB |
Output is correct |
49 |
Correct |
9 ms |
6220 KB |
Output is correct |
50 |
Correct |
8 ms |
6092 KB |
Output is correct |
51 |
Correct |
7 ms |
6092 KB |
Output is correct |
52 |
Correct |
8 ms |
6192 KB |
Output is correct |
53 |
Correct |
8 ms |
6212 KB |
Output is correct |
54 |
Correct |
8 ms |
6304 KB |
Output is correct |
55 |
Correct |
17 ms |
6732 KB |
Output is correct |
56 |
Correct |
61 ms |
10048 KB |
Output is correct |
57 |
Correct |
101 ms |
12524 KB |
Output is correct |
58 |
Runtime error |
96 ms |
17272 KB |
Execution killed with signal 11 |
59 |
Halted |
0 ms |
0 KB |
- |