#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 305;
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++;
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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
396 KB |
Output is correct |
3 |
Correct |
0 ms |
484 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
396 KB |
Output is correct |
13 |
Correct |
0 ms |
484 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
1 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 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
396 KB |
Output is correct |
13 |
Correct |
0 ms |
484 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
1 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 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |