# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
#define pii pair<int,int>
#define debug(x) cout << #x << '=' << x << endl;
const int N = 2e5 + 5;
const int INF = 1e9;
const ll mod = 1e9+7;
int n,m,x[N],now,sz,dp[N][505],z,nilai[N][105],subtree[N],in[N],out[N],cnt;
vector<int >v[N];
vector<pii >vec[N];
void dfs(int idx){
in[idx] = ++cnt;
for(auto isi : v[idx]){
dfs(isi);
}
out[idx] = cnt;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(,r,stdin);
//freopen(,w,stdout);
cin>>n>>m;
for(int i = 1;i<=n;i++){
int p;
cin>>p>>x[i];
v[p].pb(i);
}
dfs(0);
for(int i = 1;i<=n;i++){
vec[out[i]].pb({in[i],x[i]});
}
for(int i = 0;i<=n;i++){
for(int j = 1;j<=m;j++){
dp[i][i] = -INF;
}
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
dp[i][j] = dp[i-1][j];
for(auto isi : vec[i]){
dp[i][j] = max(dp[i][j],dp[isi.fi-1][j-1] + isi.se);
}
}
}
int ans = 0;
for(int i = 1;i<=n;i++){
ans =max(ans,dp[i][m]);
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |