# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1095080 | Sunbae | Biochips (IZhO12_biochips) | C++17 | 355 ms | 13916 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>
#define z exit(0)
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
vector<int> g[N];
ll a[N];
vector<ll> cur, nxt;
int invp[N], jump[N], ti;
void dfs(int u){
int pre = ti++;
invp[pre] = u;
for(int v: g[u]) dfs(v);
jump[pre] = ti;
}
signed main(){
int n, K, rt; scanf("%d %d", &n, &K);
for(int v = 0, u; v<n; ++v){
scanf("%d %lld", &u, a+v);
if(--u >= 0) g[u].push_back(v);
else rt = v;
}
dfs(rt);
cur.resize(n+1); nxt.resize(n+1);
fill(cur.begin(), cur.end(), INT_MIN);
cur[0] = 0;
for(int k = 0; k<K; ++k){
fill(nxt.begin(), nxt.end(), INT_MIN);
for(int i = 0; i<n; ++i){
cur[i+1] = max(cur[i+1], cur[i]);
nxt[jump[i]] = max(nxt[jump[i]], cur[i] + a[invp[i]]);
}
cur = nxt;
}
printf("%lld", *max_element(cur.begin(), cur.end()));
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |