#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#include "boxes.h"
int back[200001][501];
int n,m;
int dp[501];
vector<int> adj[200001];
int it[200001];
int cur=0;
void dfs(int no,int par2=-1){
for(int i=1;i<=m;i++){
if(dp[i-1]==-1){
back[no][i]=-1;
continue;
}
back[no][i]=it[no]+dp[i-1];
}
for(auto j:adj[no]){
dfs(j,no);
}
for(int i=1;i<=m;i++){
dp[i]=max(dp[i],back[no][i]);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
int ind=0;
for(int i=1;i<=m;i++){
dp[i]=-1;
}
for(int i=0;i<n;i++){
int aa;
cin>>aa>>it[i];
aa--;
if(aa>=0){
adj[aa].pb(i);
}
else{
ind=i;
}
}
dfs(ind);
cout<<dp[m]<<endl;
return 0;
}
/*
g++ -o aa -O2 box.cpp grader.cpp -std=c++14
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
5248 KB |
Output is correct |
4 |
Correct |
15 ms |
22656 KB |
Output is correct |
5 |
Correct |
24 ms |
24824 KB |
Output is correct |
6 |
Correct |
18 ms |
24832 KB |
Output is correct |
7 |
Correct |
292 ms |
297468 KB |
Output is correct |
8 |
Correct |
270 ms |
297324 KB |
Output is correct |
9 |
Correct |
331 ms |
361080 KB |
Output is correct |
10 |
Correct |
372 ms |
398584 KB |
Output is correct |