# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
490788 | ntabc05101 | Biochips (IZhO12_biochips) | C++14 | 63 ms | 8980 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 taskname ""
const long long inf = 1e18;
const int mxN = 10002;
const int mxM = 102;
int n, m;
int a[mxN];
vector<int> adj[mxN];
long long dp[mxN][mxM];
void dfs(int u, int p = -1) {
for (int i = 1; i <= m; i++) {
dp[u][i] = -inf;
}
for (auto &to: adj[u]) {
if (to != p) {
dfs(to, u);
for (int k = m; k; k--) {
for (int j = 1; j <= k; j++) {
dp[u][k] = max(dp[u][k], dp[u][k - j] + dp[to][j]);
}
}
}
}
dp[u][1] = max(dp[u][1], (long long)a[u]);
/*cout << 1 + u << ":\n";
for (int i = 0; i <= m; i++) {
cout << dp[u][i] << " ";
}
cout << "\n";*/
}
int main() {
if (fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
else {
if (fopen(taskname".in", "r")) {
freopen(taskname".in", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
int root = -1;
for (int i = 0, x, y; i < n; i++) {
cin >> x >> y; x--;
a[i] = y;
if (!(~x)) {
root = i; continue;
}
adj[x].push_back(i);
adj[i].push_back(x);
}
dfs(root);
cout << dp[root][m] << "\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |