# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
281727 | jun6873 | Biochips (IZhO12_biochips) | C++11 | 66 ms | 8908 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 maxn = 200004;
int N, K, a[maxn], o;
vector<int> g[maxn];
vector<int> dp(int x) {
vector<int> res;
for (int y : g[x]) {
vector<int> now = dp(y);
for (int i=now.size()-1; i>=1; i--) now[i] -= now[i-1];
res.insert(res.end(), now.begin(), now.end());
}
sort(res.begin(), res.end());
reverse(res.begin(), res.end());
if (res.size() >= K) res.resize(K);
for (int i=1; i<res.size(); i++) res[i] += res[i-1];
if (res.empty()) res.push_back(a[x]);
else res[0] = max(res[0], a[x]);
return res;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
for (int i=1; i<=N; i++) {
int p, x;
cin >> p >> x;
if (p == 0) o = i;
else g[p].push_back(i);
a[i] = x;
}
cout << dp(o)[K-1] << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |