답안 #875621

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875621 2023-11-20T08:19:28 Z The_Samurai 바이오칩 (IZhO12_biochips) C++17
80 / 100
2000 ms 387344 KB
// #pragma GCC optimize("Ofast,O3")
// #pragma GCC target("avx,avx2")
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const int inf = 2e9;

int n, k, root;
vector<vector<int>> dp, g;

void dfs(int u) {
    int val = dp[1][u]; dp[1][u] = -inf;
    dp[0][u] = 0;
    for (int v: g[u]) {
        dfs(v);
        vector<int> copy(k + 1);
        for (int i = 1; i <= k; i++) copy[i] = dp[i][u];
        for (int i = 1; i <= k; i++) {
            if (dp[i][v] == -inf) continue;
            for (int j = i; j <= k; j++) {
                if (copy[j - i] == -inf) continue;
                dp[j][u] = max(dp[j][u], dp[i][v] + copy[j - i]);
            }
        }
    }
    dp[1][u] = max(dp[1][u], val);
    // if (u != root) return;
}

void solve() {
    cin >> n >> k;
    g.assign(n + 1, {});
    dp.assign(k + 1, vector<int>(n + 1, -inf));
    for (int i = 1; i <= n; i++) {
        int p;
        cin >> p >> dp[1][i];
        if (!p) root = i;
        else g[p].emplace_back(i);
    }
    dfs(root);
    cout << dp[k][root];
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int q = 1;
    // cin >> q;
    while (q--) {
        solve();
        cout << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 6 ms 2908 KB Output is correct
5 Correct 7 ms 3672 KB Output is correct
6 Correct 9 ms 4700 KB Output is correct
7 Correct 1190 ms 181264 KB Output is correct
8 Correct 1210 ms 181392 KB Output is correct
9 Execution timed out 2094 ms 282884 KB Time limit exceeded
10 Execution timed out 2039 ms 387344 KB Time limit exceeded