#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
template<class T> bool chmin(T& a, const T& b){return a>b?a=b,true:false;}
template<class T> bool chmax(T& a, const T& b){return a<b?a=b,true:false;}
const int N = 2e5+5, M = 505;
const int inf = 1e9;
int n, m;
int dp[N][M], w[N];
vector<int> edges[N];
int timer = 1;
int v[N], l[N];
void dfs(int u){
int tmp = timer;
for(auto v : edges[u])
dfs(v);
v[timer] = w[u];
l[timer] = tmp;
timer++;
}
void Solve(){
cin >> n >> m;
int root;
for(int i=1;i<=n;i++){
int j, cost;
cin >> j >> cost;
if(j > 0)edges[j].push_back(i);
else root = i;
w[i] = cost;
}
dfs(root);
for(int i=0;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = -inf;
}
}
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]+v[i]);
}
}
cout << dp[n][m] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie();
int t=1;
while(t--)Solve();
return 0;
}
Compilation message
biochips.cpp: In function 'void Solve()':
biochips.cpp:32:6: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
32 | dfs(root);
| ~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5228 KB |
Output is correct |
4 |
Correct |
17 ms |
22892 KB |
Output is correct |
5 |
Correct |
18 ms |
25196 KB |
Output is correct |
6 |
Correct |
19 ms |
25196 KB |
Output is correct |
7 |
Correct |
255 ms |
300908 KB |
Output is correct |
8 |
Correct |
263 ms |
300900 KB |
Output is correct |
9 |
Correct |
327 ms |
365408 KB |
Output is correct |
10 |
Correct |
395 ms |
403372 KB |
Output is correct |