#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;
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:57:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen("biorobot.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:58:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | freopen("biorobot.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
5076 KB |
Output isn't correct |
7 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
9 |
Incorrect |
6 ms |
5104 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
5100 KB |
Output isn't correct |