# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1037037 | pera | Biochips (IZhO12_biochips) | C++17 | 480 ms | 487456 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;
const int N = 2e5 + 100 , M = 500 + 100;
int n , m , root , T = 0;
vector<int> e(N) , in(N) , out(N) , ord = {0};
vector<vector<int>> g(N) , dp(N , vector<int>(M));
void dfs(int u){
in[u] = ++T;
ord.push_back(u);
for(int v : g[u]){
dfs(v);
}
out[u] = T;
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i ++){
int p;
cin >> p >> e[i];
if(p > 0){
g[p].push_back(i);
}else{
root = i;
}
}
dfs(root);
for(int i = 0;i < N;i ++){
for(int j = 0;j < M;j ++){
dp[i][j] = -1e9;
}
}
dp[n + 1][0] = 0;
for(int i = n;i >= 1;i --){
dp[i][0] = 0;
for(int x = 1;x <= m;x ++){
dp[i][x] = max(dp[i + 1][x] , dp[out[ord[i]] + 1][x - 1] + e[ord[i]]);
}
}
cout << dp[1][m] << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |