#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1 , LOG = 31;
int n , m , root = -1;
vector<int> mx(N);
vector<vector<int>> g(N);
void dfs(int u){
for(int x = 0;x < (int)g[u].size();x ++){
dfs(g[u][x]);
mx[u] = max(mx[u] , mx[g[u][x]]);
}
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i ++){
int p;
cin >> p >> mx[i];
if(p > 0){
g[p].push_back(i);
}else{
root = i;
}
}
dfs(root);
vector<int> v;
for(int x = 0;x < (int)g[root].size();x ++){
v.push_back(mx[g[root][x]]);
}
sort(v.begin() , v.end());
int s = 0;
for(int x = 1;x <= m;x ++){
s += v.back();
v.pop_back();
}
if(m == 1){
s = max(s , mx[root]);
}
cout << s << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5724 KB |
Output isn't correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
1 ms |
5768 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
6120 KB |
Output isn't correct |
7 |
Incorrect |
46 ms |
8264 KB |
Output isn't correct |
8 |
Incorrect |
46 ms |
8476 KB |
Output isn't correct |
9 |
Incorrect |
45 ms |
8276 KB |
Output isn't correct |
10 |
Incorrect |
51 ms |
8340 KB |
Output isn't correct |