# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
913972 | trashaccount | Biochips (IZhO12_biochips) | C++17 | 198 ms | 405528 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N, M, p[200005], x[200005], rt;
vector <int> son[200005];
int timer = 0, tin[200005], tout[200005], tour[200005], dp[200005][505];
void dfs(int u){
tin[u] = ++timer;
tour[timer] = u;
for (int v : son[u]){
dfs(v);
}
tout[u] = timer;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++){
cin >> p[i] >> x[i];
if (!p[i]) rt = i;
son[p[i]].push_back(i);
}
dfs(rt);
for (int i = 1; i <= M; i++) dp[N+1][i] = -1e9;
for (int i = N; i >= 1; i--){
int u = tour[i];
for (int j = 1; j <= M; j++)
dp[i][j] = max(dp[tout[u]+1][j-1]+x[u], dp[i+1][j]);
}
cout << dp[1][M];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |