#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
#define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define nl "\n"
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll mxn = 1e4L;
const ll mxm = 1e2L;
#warning mxn, mxm are set wrong
const ll inf = 1e12L;
ll n, m;
ll root;
ll p[mxn + 1];
vl adj[mxn + 1];
ll s[mxn + 1];
ll dp[mxn + 1][mxm + 1];
void dfs(ll cur, ll par) {
ll one = -inf;
for(auto u : adj[cur]) {
if(u == par)
continue;
dfs(u, cur);
one = max(one, dp[u][1]);
}
ll k = adj[cur].size();
ll a[k + 1][m + 1];
a[0][0] = 0;
fur(y, 1, m) {
a[0][y] = -inf;
}
fur(x, 1, k) {
a[x][0] = 0;
fur(y, 1, m) {
a[x][y] = -inf;
fur(z, 0, y) {
a[x][y] = max(a[x][y], dp[adj[cur][x - 1]][z] + a[x - 1][y - z]);
}
}
}
dp[cur][0] = 0;
dp[cur][1] = max(s[cur], dp[cur][1]);
fur(j, 2, m) {
dp[cur][j] = a[k][j];
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
fur(i, 1, n) {
cin >> p[i] >> s[i];
if(p[i] != 0) {
adj[i].pb(p[i]);
adj[p[i]].pb(i);
} else {
root = i;
}
}
fur(i, 0, n) {
fur(j, 0, m) {
dp[i][j] = -inf;
}
}
dfs(root, -1);
cout << dp[root][m] << nl;
}
Compilation message
biochips.cpp:24:2: warning: #warning mxn, mxm are set wrong [-Wcpp]
24 | #warning mxn, mxm are set wrong
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Incorrect |
45 ms |
8060 KB |
Output isn't correct |
5 |
Incorrect |
74 ms |
9016 KB |
Output isn't correct |
6 |
Incorrect |
118 ms |
9016 KB |
Output isn't correct |
7 |
Runtime error |
1 ms |
852 KB |
Execution killed with signal 11 |
8 |
Runtime error |
1 ms |
980 KB |
Execution killed with signal 11 |
9 |
Runtime error |
4 ms |
2164 KB |
Execution killed with signal 11 |
10 |
Runtime error |
4 ms |
2132 KB |
Execution killed with signal 11 |