#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 300'005;
const ll inf = 2e18;
void print(priority_queue<ll> pq) {
cout << "[ ";
while (pq.size()) {
cout << pq.top() << ' ';
pq.pop();
}
cout << "]\n";
}
vector<array<int, 2>> adj[N];
priority_queue<ll> pq[N];
ll dp0[N];
//ll dp[N][N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
n += m;
for (int i = 2; i <= n; i++) {
int p, c;
cin >> p >> c;
adj[p].push_back({i, c});
}
for (int i = n; i >= 1; i--) {
if (adj[i].empty()) {
dp0[i] = 0;
pq[i].push(0);
pq[i].push(0);
} else {
int cur = 0;
for (auto [nxt, w] : adj[i]) {
dp0[i] += dp0[nxt] + w;
ll x = pq[nxt].top() + w;
pq[nxt].pop();
ll y = pq[nxt].top() + w;
pq[nxt].pop();
pq[nxt].push(y);
pq[nxt].push(x);
cur++;
if (pq[nxt].size() > pq[i].size()) {
swap(pq[nxt], pq[i]);
}
while (pq[nxt].size()) {
x = pq[nxt].top();
pq[nxt].pop();
pq[i].push(x);
}
}
while (cur > 1) {
cur--;
pq[i].pop();
}
}
// cout << dp0[i] << ' ';
// print(pq[i]);
// for (auto [nxt, w] : adj[i]) {
// cout << i << " -> " << nxt << ' ' << w << '\n';
// for (int j = 0; j < N; j++) {
// cout << dp[nxt][j] << ' ';
// }
// cout << '\n';
// for (int j = 0; j < N; j++) {
// ll mn = 1e18;
// for (int k = 0; k <= j; k++) {
// mn = min(mn, dp[nxt][k] + abs(j - k - w));
// }
// cout << mn << ' ';
// dp[i][j] += mn;
// dp[i][j] = min(dp[i][j], inf);
// }
// cout << "\n\n";
// }
// ll mn = *min_element(dp[i], dp[i] + N);
// for (int j = N - 1; j >= 0; j--) {
// if (dp[i][j] == mn) {
// break;
// }
// dp[i][j] = inf;
// }
}
ll height = 0;
ll cur = 0, lst = pq[1].top();
pq[1].pop();
while (pq[1].size()) {
height += (lst - pq[1].top()) * cur;
cur++;
lst = pq[1].top();
pq[1].pop();
}
height += lst * cur;
cout << dp0[1] - height << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16828 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Correct |
6 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
5 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
5 ms |
17244 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
4 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
4 ms |
16988 KB |
Output is correct |
17 |
Correct |
4 ms |
16988 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16828 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Correct |
6 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
4 ms |
16988 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
5 ms |
17244 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
4 ms |
16988 KB |
Output is correct |
23 |
Correct |
4 ms |
16988 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
4 ms |
16988 KB |
Output is correct |
28 |
Correct |
4 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
16988 KB |
Output is correct |
31 |
Correct |
4 ms |
16988 KB |
Output is correct |
32 |
Correct |
4 ms |
16984 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16988 KB |
Output is correct |
35 |
Correct |
6 ms |
17244 KB |
Output is correct |
36 |
Correct |
5 ms |
17244 KB |
Output is correct |
37 |
Correct |
5 ms |
17312 KB |
Output is correct |
38 |
Correct |
6 ms |
17244 KB |
Output is correct |
39 |
Correct |
5 ms |
17240 KB |
Output is correct |
40 |
Correct |
5 ms |
17088 KB |
Output is correct |
41 |
Correct |
5 ms |
16984 KB |
Output is correct |
42 |
Correct |
4 ms |
16988 KB |
Output is correct |
43 |
Correct |
6 ms |
17164 KB |
Output is correct |
44 |
Correct |
6 ms |
17244 KB |
Output is correct |
45 |
Correct |
5 ms |
17244 KB |
Output is correct |
46 |
Correct |
6 ms |
17500 KB |
Output is correct |
47 |
Correct |
6 ms |
17500 KB |
Output is correct |
48 |
Correct |
7 ms |
17244 KB |
Output is correct |
49 |
Correct |
6 ms |
17244 KB |
Output is correct |
50 |
Correct |
5 ms |
17244 KB |
Output is correct |
51 |
Correct |
6 ms |
17232 KB |
Output is correct |
52 |
Correct |
6 ms |
17244 KB |
Output is correct |
53 |
Correct |
6 ms |
17352 KB |
Output is correct |
54 |
Correct |
5 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16828 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Correct |
6 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
5 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
4 ms |
16988 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
5 ms |
17244 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
4 ms |
16988 KB |
Output is correct |
23 |
Correct |
4 ms |
16988 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
4 ms |
16988 KB |
Output is correct |
28 |
Correct |
4 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
16988 KB |
Output is correct |
31 |
Correct |
4 ms |
16988 KB |
Output is correct |
32 |
Correct |
4 ms |
16984 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16988 KB |
Output is correct |
35 |
Correct |
6 ms |
17244 KB |
Output is correct |
36 |
Correct |
5 ms |
17244 KB |
Output is correct |
37 |
Correct |
5 ms |
17312 KB |
Output is correct |
38 |
Correct |
6 ms |
17244 KB |
Output is correct |
39 |
Correct |
5 ms |
17240 KB |
Output is correct |
40 |
Correct |
5 ms |
17088 KB |
Output is correct |
41 |
Correct |
5 ms |
16984 KB |
Output is correct |
42 |
Correct |
4 ms |
16988 KB |
Output is correct |
43 |
Correct |
6 ms |
17164 KB |
Output is correct |
44 |
Correct |
6 ms |
17244 KB |
Output is correct |
45 |
Correct |
5 ms |
17244 KB |
Output is correct |
46 |
Correct |
6 ms |
17500 KB |
Output is correct |
47 |
Correct |
6 ms |
17500 KB |
Output is correct |
48 |
Correct |
7 ms |
17244 KB |
Output is correct |
49 |
Correct |
6 ms |
17244 KB |
Output is correct |
50 |
Correct |
5 ms |
17244 KB |
Output is correct |
51 |
Correct |
6 ms |
17232 KB |
Output is correct |
52 |
Correct |
6 ms |
17244 KB |
Output is correct |
53 |
Correct |
6 ms |
17352 KB |
Output is correct |
54 |
Correct |
5 ms |
17244 KB |
Output is correct |
55 |
Correct |
8 ms |
17756 KB |
Output is correct |
56 |
Correct |
24 ms |
22368 KB |
Output is correct |
57 |
Correct |
38 ms |
23876 KB |
Output is correct |
58 |
Correct |
49 ms |
25228 KB |
Output is correct |
59 |
Correct |
64 ms |
27116 KB |
Output is correct |
60 |
Correct |
96 ms |
31908 KB |
Output is correct |
61 |
Correct |
95 ms |
34084 KB |
Output is correct |
62 |
Correct |
105 ms |
35268 KB |
Output is correct |
63 |
Correct |
136 ms |
38460 KB |
Output is correct |
64 |
Correct |
137 ms |
38992 KB |
Output is correct |
65 |
Correct |
60 ms |
33404 KB |
Output is correct |
66 |
Correct |
60 ms |
33476 KB |
Output is correct |
67 |
Correct |
59 ms |
33364 KB |
Output is correct |
68 |
Correct |
129 ms |
35372 KB |
Output is correct |
69 |
Correct |
131 ms |
35812 KB |
Output is correct |
70 |
Correct |
135 ms |
35780 KB |
Output is correct |
71 |
Correct |
170 ms |
51588 KB |
Output is correct |
72 |
Correct |
193 ms |
50112 KB |
Output is correct |
73 |
Correct |
155 ms |
46208 KB |
Output is correct |
74 |
Correct |
151 ms |
47332 KB |
Output is correct |
75 |
Correct |
176 ms |
46224 KB |
Output is correct |
76 |
Correct |
153 ms |
45824 KB |
Output is correct |
77 |
Correct |
156 ms |
45864 KB |
Output is correct |
78 |
Correct |
162 ms |
44164 KB |
Output is correct |
79 |
Correct |
154 ms |
46248 KB |
Output is correct |