#include <bits/stdc++.h>
using namespace std;
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define type(x) __typeof(x.begin())
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define pb push_back
#define mp make_pair
#define nd second
#define st first
#define endl '\n'
typedef pair < int ,int > pii;
typedef long long ll;
const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9 + 7;
const int N = 2e5 + 10;
int w[N], val[N], n, m, x, y, start[N], finish[N], T, dp[N][600], root, p[N];
vector< int > v[N];
int dfs(int node) {
start[node] = ++T;
w[T] = node;
foreach(it, v[node])
dfs(*it);
finish[node] = T;
}
int main() {
scanf("%d %d", &n, &m);
FOR(i, 1, n) {
scanf("%d %d", &x, &val[i]);
if(!x) root = i;
else v[x].pb(i);
} dfs(root);
FOR(i, 1, m) dp[n+1][i] = -inf;
ROF(i, n, 1)
FOR(j, 0, m)
dp[i][j] = max(dp[i+1][j], (j != 0) * (dp[finish[w[i]] + 1][j-1] + val[w[i]]));
printf("%d\n", dp[1][m]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
479088 KB |
Output is correct |
2 |
Correct |
2 ms |
479088 KB |
Output is correct |
3 |
Correct |
0 ms |
479088 KB |
Output is correct |
4 |
Correct |
5 ms |
479220 KB |
Output is correct |
5 |
Correct |
7 ms |
479088 KB |
Output is correct |
6 |
Correct |
3 ms |
479088 KB |
Output is correct |
7 |
Correct |
132 ms |
480144 KB |
Output is correct |
8 |
Correct |
126 ms |
480144 KB |
Output is correct |
9 |
Correct |
184 ms |
480012 KB |
Output is correct |
10 |
Correct |
235 ms |
480012 KB |
Output is correct |