0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-25 20:32:11 +00:00
OI-codes/Luogu/P2014/P2014.cpp

52 lines
814 B
C++
Raw Normal View History

2022-04-09 02:25:10 +00:00
#include <iostream>
2022-06-01 01:45:41 +00:00
#include <cstring>
2022-04-09 02:25:10 +00:00
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 305;
int n, m, s[N], f[N][N];
std::vector<int> g[N];
void dfs(int u) {
f[u][0] = 0;
for (int v : g[u]) {
dfs(v);
for (int i = m; i >= 0; i--) {
for (int j = i; j >= 0; j--) {
f[u][i] = std::max(f[u][i], f[u][i - j] + f[v][j]);
}
}
}
if (u) {
for (int i = m; i; i--) {
f[u][i] = f[u][i - 1] + s[u];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
memset(f, 0xcf, sizeof(f));
cin >> n >> m;
for (int i = 1, x; i <= n; i++) {
cin >> x >> s[i];
g[x].push_back(i);
}
dfs(0);
cout << f[0][m] << endl;
return 0;
}