#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ar array
#define fr first
#define sc second
#define vec vector
#define ret return
#define ins insert
#define mk make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define int long long
#define pii pair<int,int>
#define all(s) s.begin(), s.end()
#define allr(s) s.rbegin(), s.rend()
#define NeedForSpeed ios::sync_with_stdio(0),cin.tie(0);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const int N = 1e6+12,INF=1e9+7;
vector <int> g[N];
int tout[N],o=1,n,k,b[N],a[N],dp[200001][501];
void dfs(int v){
int t = o;
for(int to : g[v]){
dfs(to);
}
a[o] = b[v];
tout[o++] = t;
}
main(){
cin>>n>>k;
for(int i=1; i<=n; i++){
int x;
cin>>x>>b[i];
g[x].pb(i);
}
dfs(0);
for(int i=0; i<=n; i++){
for(int j=1; j<=k; j++){
dp[i][j] = -INF;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=k; j++){
dp[i][j] = max(dp[i-1][j], dp[tout[i]-1][j-1] + a[i]);
}
}
cout<<dp[n][k];
}
Compilation message
biochips.cpp:41:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
41 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
24140 KB |
Output is correct |
4 |
Correct |
34 ms |
58896 KB |
Output is correct |
5 |
Correct |
34 ms |
63372 KB |
Output is correct |
6 |
Correct |
36 ms |
63308 KB |
Output is correct |
7 |
Runtime error |
258 ms |
524292 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |