# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
523694 | knightron0 | Biochips (IZhO12_biochips) | C++14 | 4 ms | 4940 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 pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double
const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 2e5 + 5;
vector<int> adj[MAXN];
int val[MAXN], timer = 0, tin[MAXN], tout[MAXN], sz[MAXN], arr[MAXN], idx[MAXN];
void dfs(int s){
tin[s] = ++timer;
arr[timer] = val[s];
idx[s] = timer;
for(auto u: adj[s]){
dfs(u);
}
tout[s] = timer;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin>>n>>m;
int root = -1;
for(int i= 1;i<=n;i++){
int t1, t2;
cin>>t1>>t2;
val[i] = t2;
if(t1 == 0){
root = i;
continue;
} else {
adj[t1].pb(i);
}
}
dfs(root);
for(int i= 1;i<=n;i++){
sz[i] = tout[idx[i]]-tin[idx[i]]+1;
}
int dp[n+3][m+3];
for(int i =1;i<=n+1;i++){
dp[i][0] = 0;
}
for(int i =1;i<=m;i++){
dp[n+1][i] = -INF;
for(int j= n;j>=1;j--){
dp[j][i] = max(dp[j+1][i], dp[j+sz[j]][i-1] + arr[j]);
}
}
cout<<dp[1][m]<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |