// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 2e5 + 5;
const int maxM = 505;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, m;
vector<int> adj[maxN];
int a[maxN];
int root;
int f[maxN][maxM];
int sz[maxN];
int tmp[maxM];
void ReadInput()
{
cin >> n >> m;
for(int i=1; i<=n; i++)
{
int p;
cin >> p >> a[i];
if(!p) root = i;
else
{
adj[p].pb(i);
adj[i].pb(p);
}
}
}
void dfs(int u, int par)
{
// cout << u << " ";
f[u][0] = 0;
sz[u] = 1;
for(int v : adj[u])
{
if(v == par) continue;
dfs(v, u);
fill(tmp, tmp + m + 1, 0);
for(int i=0; i<=min(m, sz[u]); i++)
for(int j=0; j<=min(m - i, sz[v]); j++)
tmp[i + j] = max(tmp[i + j], f[u][i] + f[v][j]);
sz[u] += sz[v];
for(int i=1; i<=min(m, sz[u]); i++)
f[u][i] = tmp[i];
}
f[u][1] = max(f[u][1], a[u]);
}
void Solve()
{
dfs(root, 0);
cout << f[root][m];
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
5160 KB |
Output is correct |
4 |
Correct |
13 ms |
22996 KB |
Output is correct |
5 |
Correct |
16 ms |
25300 KB |
Output is correct |
6 |
Correct |
17 ms |
25300 KB |
Output is correct |
7 |
Correct |
280 ms |
304700 KB |
Output is correct |
8 |
Correct |
269 ms |
304900 KB |
Output is correct |
9 |
Correct |
446 ms |
370344 KB |
Output is correct |
10 |
Correct |
521 ms |
408772 KB |
Output is correct |