#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "biochips"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 3e5 + 9;
int root,n,k;
vector<int> g[MAXN];
void solve(){
cin>>n>>k;
vector<int> par(n+3),etour(n+3), in(n+3), out(n+3), value(n+3);
vector<vector<int>> dp(n+3,vector<int>(k+3,0));
int id = 0;
function<void(int,int)> dfs = [&](int u,int p){
etour[++id] = u;
in[u] = id;
for(auto v: g[u]){
if (v==p) continue;
dfs(v,u);
}
out[u] = id;
};
for(int i=1;i<=n;i++){
cin>>par[i]>>value[i];
if (par[i] == 0) root = i;
else {
g[i].pb(par[i]);
g[par[i]].pb(i);
}
}
dfs(root,-1);
for(int i=1;i<=k;i++){
dp[n+1][i] = -INF;
}
dp[n+1][0] = 0;
for(int i=n;i>=1;i--){
for(int j=0;j<=k;j++){
int u = etour[i];
maximize(dp[i][j],dp[i+1][j]);
if (j>=1) maximize(dp[i][j],dp[out[u]+1][j-1] + value[u]);
}
}
cout<<dp[1][k]<<endl;
}
main()
{
fast;
// freopen(TASKNAME".inp","r",stdin);
// freopen(TASKNAME".out","w",stdout);
solve();
}
Compilation message
biochips.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
63 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7260 KB |
Output isn't correct |
3 |
Correct |
2 ms |
7512 KB |
Output is correct |
4 |
Correct |
7 ms |
12908 KB |
Output is correct |
5 |
Correct |
9 ms |
14684 KB |
Output is correct |
6 |
Correct |
10 ms |
16732 KB |
Output is correct |
7 |
Correct |
267 ms |
375732 KB |
Output is correct |
8 |
Correct |
271 ms |
375684 KB |
Output is correct |
9 |
Runtime error |
175 ms |
524288 KB |
Execution killed with signal 9 |
10 |
Runtime error |
183 ms |
524288 KB |
Execution killed with signal 9 |