#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = a; i < b; i++)
typedef vector<int> vi;
const int INF=1e9;
const int MAXN = 200005;
int n,m,root;
vi g[MAXN];
int par[MAXN],a[MAXN],st[MAXN],fin[MAXN];
int tim = 0;
int dp[MAXN][505];
void dfs(int u){
st[u] = tim;
for(auto v:g[u]) dfs(v);
for(int i=1;i<=m;i++){
dp[tim+1][i] = max(dp[tim][i],dp[st[u]][i-1]+a[u]);
}
tim++;
}
signed main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
cin >> par[i] >> a[i];
if(!par[i]) root = i;
else g[par[i]].push_back(i);
}
for(int i=1;i<=m;i++) dp[0][i] = -INF;
dfs(root);
cout << dp[n][m] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |