# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
683893 | KiriLL1ca | Biochips (IZhO12_biochips) | C++17 | 682 ms | 408028 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 fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define pw(x) (1ll << x)
#define vec vector
#define sz(x) (int)((x).size())
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
template <typename T> inline bool umin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
const int N = 2e5 + 100;
const int M = 505;
vector <int> g[N];
int dp[N][M], siz[N], m, a[N];
inline void sizes (int v, int pr) { siz[v] = 1; for (auto u : g[v]) if (u != pr) sizes(u, v), siz[v] += siz[u]; }
inline void dfs (int v, int pr) {
for (auto u : g[v]) if (u != pr) dfs(u, v);
for (auto u : g[v]) if (u != pr) {
for (int i = min(siz[v], m); i >= 0; --i) {
for (int j = min(siz[u], m - i); j >= 0 && i + j <= m; --j) {
umax(dp[v][i + j], dp[v][i] + dp[u][j]);
}
}
}
umax(dp[v][1], a[v]);
}
inline void solve () {
int n; cin >> n >> m;
int rt = -1, p;
for (int i = 0; i < n; ++i) {
cin >> p >> a[i];
if (!p) { rt = i; continue; }
--p;
g[i].pb(p); g[p].pb(i);
}
sizes(rt, rt); dfs(rt, rt);
cout << dp[rt][m];
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//freopen("biorobot.in", "r", stdin);
//freopen("biorobot.out", "w", stdout);
#endif
int t = 1; // cin >> t;
while (t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |