이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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++;
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |