# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
508215 | sidon | Biochips (IZhO12_biochips) | C++17 | 422 ms | 396580 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;
#define maxim(X, Y) (X = max(X, Y))
const int Z = 2e5+1;
int N, M, S[Z], rt, dp[Z][501], r;
vector<int> g[Z];
void dfs(int u) {
int l = r;
if(empty(g[u])) {
++r;
for(int i = 0; i <= M; i++)
maxim(dp[r][i], dp[r-1][i]);
}
for(int v : g[u]) dfs(v);
for(int i = M + 1; u != rt && --i; )
maxim(dp[r][i], dp[l][i-1] + S[u]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
for(int i=1; i<=N; ++i) {
int p; cin >> p >> S[i];
if(p) g[p].push_back(i);
else rt = i;
}
dfs(rt);
cout << *max_element(dp[r], dp[r] + M + 1);
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |