#include <bits/stdc++.h>
using namespace std;
#define int int64_t
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int N, M; cin >> N >> M;
N += M;
int c[N];
priority_queue<int> dp[N];
vector<int> g[N];
for(int i = 1; i < N; ++i) {
int p; cin >> p >> c[i];
g[p-1].push_back(i);
}
for(int u = N; u--; ) {
if(!empty(g[u])) {
for(int v : g[u]) {
assert(size(dp[v]) > size(g[v]));
for(int i = size(g[v]); --i > 0; )
dp[v].pop();
assert(size(dp[v]) > 1);
int x = dp[v].top(); dp[v].pop();
int y = dp[v].top(); dp[v].pop();
dp[v].push(x + c[v]);
dp[v].push(y + c[v]);
if(size(dp[v]) > size(dp[u]))
dp[u].swap(dp[v]);
}
for(int v : g[u])
while(!empty(dp[v]))
dp[u].push(dp[v].top()), dp[v].pop();
} else {
dp[u].push(0);
dp[u].push(0);
}
}
int ans = accumulate(c + 1, c + N, 0), d = -M, x {};
vector<int> res;
while(!empty(dp[0]))
res.push_back(dp[0].top()), dp[0].pop();
for(int i = size(res) - 1; d < 0; --i, ++d) {
ans += d * (res[i] - x);
x = res[i];
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |