From b62fe3741ff43d77972a58da7dd01a934ae59667 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 9 Apr 2022 10:25:10 +0800 Subject: [PATCH] =?UTF-8?q?P2014=20[CTSC1997]=20=E9=80=89=E8=AF=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R73521127 --- Luogu/P2014/P2014.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Luogu/P2014/P2014.cpp diff --git a/Luogu/P2014/P2014.cpp b/Luogu/P2014/P2014.cpp new file mode 100644 index 00000000..34c4e9e0 --- /dev/null +++ b/Luogu/P2014/P2014.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 305; + +int n, m, s[N], f[N][N]; +std::vector 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; +}