#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], dp2[501][N], sz[N];
vector<int> g[N];
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 |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
5 ms |
11100 KB |
Output is correct |
5 |
Correct |
6 ms |
12100 KB |
Output is correct |
6 |
Correct |
7 ms |
13148 KB |
Output is correct |
7 |
Correct |
214 ms |
176208 KB |
Output is correct |
8 |
Correct |
205 ms |
177488 KB |
Output is correct |
9 |
Correct |
258 ms |
14376 KB |
Output is correct |
10 |
Correct |
354 ms |
15260 KB |
Output is correct |