#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 200'005;
const ll MOD = 1'000'000'007;
const int INF = 0x3f3f3f3f;
int dp[N][505];
vector<int> ch[N];
int cnt = 1, w[N], ww[N], l[N];
void dfs(int v){
int t = cnt;
for(auto& x : ch[v])
dfs(x);
ww[cnt] = w[v];
l[cnt++] = t;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
int root;
for(int p, i = 1; i <= n; ++i){
cin>>p>>w[i];
if(p)
ch[p].push_back(i);
else
root = i;
}
dfs(root);
for(int i = 0; i <= n; ++i)
for(int j = 1; j <= m; ++j)
dp[i][j] = -0x3f3f3f3f;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
dp[i][j] = max(dp[i - 1][j], dp[l[i] - 1][j - 1] + ww[i]);
cout<<dp[n][m];
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:42:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | dfs(root);
| ~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5228 KB |
Output is correct |
4 |
Correct |
16 ms |
22892 KB |
Output is correct |
5 |
Correct |
17 ms |
25216 KB |
Output is correct |
6 |
Correct |
18 ms |
25196 KB |
Output is correct |
7 |
Correct |
244 ms |
300872 KB |
Output is correct |
8 |
Correct |
254 ms |
300924 KB |
Output is correct |
9 |
Correct |
301 ms |
365420 KB |
Output is correct |
10 |
Correct |
363 ms |
403308 KB |
Output is correct |