#include <iostream>
#include <cstring>
#include <cassert>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 200050
using ll = long long;
int n, m, a[N], timer, dp2[501][N], sz[N], hi[N];
vector<int> g[N], g2[N];
vector<tuple<int, int, int>> v;
void dfs1(int u)
{
++sz[u];
for (auto v : g[u])
{
dfs1(v);
for (int i = min(m, sz[u]); i--;)
for (int j = 0; j <= min(m-i, sz[v]); ++j)
dp2[i+j][u] = max(dp2[i+j][u], dp2[i][u] + dp2[j][v]);
sz[u] += sz[v];
}
dp2[1][u] = max(dp2[1][u], a[u]);
}
int main()
{
ShinLena;
cin >> n >> m;
for (int p, i = 1; i <= n; ++i) cin >> p >> a[i], g[p].push_back(i);
dfs1(0);
cout << dp2[m][0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
6 ms |
13476 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
7 ms |
15440 KB |
Output is correct |
7 |
Correct |
216 ms |
179808 KB |
Output is correct |
8 |
Correct |
211 ms |
181080 KB |
Output is correct |
9 |
Correct |
258 ms |
17488 KB |
Output is correct |
10 |
Correct |
369 ms |
18780 KB |
Output is correct |