#include <bits/stdc++.h>
using namespace std;
typedef long double LD;
typedef long long ll;
#define int ll
#define pb push_back
#define mp make_pair
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define all(v) v.begin(),v.end()
typedef map<int,int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
#define F first
#define S second
#define PQ(type) priority_queue<type>
#define PQD(type) priority_queue<type,vector<type>,greater<type> >
#define WL(t) while(t --)
#define SZ(x) ((int)(x).size())
#define runtime() ((double)clock() / CLOCKS_PER_SEC)
#define inrange(i,a,b) ((i>=min(a,b)) && (i<=max(a,b)))
const int INF=1e9;
const int MAXN = 200005;
int n,m,root;
vi adj[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:adj[u]) dfs(v);
FOR(j,1,m+1){
dp[tim+1][j] = max(dp[tim][j],dp[st[u]][j-1]+a[u]);
}
tim++;
}
signed main(){
cin >> n >> m;
FOR(i,1,n+1){
cin >> par[i] >> a[i];
if(!par[i]) root = i;
else adj[par[i]].pb(i);
}
FOR(i,1,m+1) dp[0][i] = -INF;
dfs(root);
cout << dp[n][m] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |