# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
90727 | mirbek01 | Biochips (IZhO12_biochips) | C++17 | 8 ms | 7416 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 = 3e5 + 2;
int n, m, rt, a[N], tin[N], tout[N], timer;
long long dp[2][N], x[N];
vector <int> g[N];
void dfs(int v){
tin[v] = ++ timer;
for(int to : g[v]){
dfs(to);
}
tout[v] = timer;
}
int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i ++){
int p;
scanf("%d %lld", &p, x + i);
if(!p)
rt = i;
else {
g[p].push_back(i);
}
}
dfs(rt);
for(int i = 1; i <= n; i ++){
dp[1][tout[i]] = max(x[i], dp[1][tout[i]]);
}
for(int i = 2; i <= m; i ++){
int cur = i % 2;
int pre = cur ^ 1;
fill(dp[cur], dp[cur] + n + 2, -1e18);
for(int j = 1; j <= n; j ++){
dp[cur][tout[j]] = max(dp[cur][tout[j]], dp[pre][tin[j] - 1] + x[j]);
}
}
long long ans = 0;
for(int i = 1; i <= n; i ++)
ans = max(ans, dp[m % 2][i]);
cout << ans << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |