Submission #954123

#TimeUsernameProblemLanguageResultExecution timeMemory
954123adaawf바이오칩 (IZhO12_biochips)C++17
100 / 100
513 ms403664 KiB
#include <iostream>
#include <vector>
using namespace std;
vector<int> g[200005];
int f[200005][505], a[200005], k;
void dfs(int x) {
    for (int w : g[x]) {
        dfs(w);
        for (int i = k; i >= 0; i--) {
            for (int j = 0; j <= k - i; j++) {
                if (j != 0 && f[w][j] == 0) break;
                f[x][i + j] = max(f[x][i + j], f[x][i] + f[w][j]);
            }
        }
    }
    f[x][1] = max(f[x][1], a[x]);
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, r = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        int x, y;
        cin >> x >> y;
        a[i] = y;
        if (x == 0) {
            r = i;
        }
        else {
            g[x].push_back(i);
        }
    }
    dfs(r);
    cout << f[r][k];
}
#Verdict Execution timeMemoryGrader output
Fetching results...