253322026-02-19 11:13:17KevinRaktárba szállítás (45 pont)cpp17Wrong answer 3/4514ms3248 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    ll C;
    cin >> N >> K >> C;

    vector<int> parent(N + 1);
    vector<ll> A(N + 1);

    for (int i = 1; i <= N; i++) {
        cin >> parent[i] >> A[i];
    }

    // FordĂ­tott fa (gyereklista)
    vector<vector<int>> children(N + 1);
    for (int i = 1; i <= N; i++) {
        if (i != 1) {
            children[parent[i]].push_back(i);
        }
    }

    // Részfaösszegek kiszámítása (postorder DFS)
    vector<ll> subtree(N + 1, 0);

    function<void(int)> dfs = [&](int u) {
        subtree[u] = A[u];
        for (int v : children[u]) {
            dfs(v);
            subtree[u] += subtree[v];
        }
    };

    dfs(1);

    // Max-heap (subtree_sum, node)
    priority_queue<pair<ll,int>> pq;
    for (int i = 1; i <= N; i++) {
        pq.push({subtree[i], i});
    }

    ll result = 0;

    // Aktuális értékek (levonások miatt változik)
    vector<ll> current = subtree;

    for (int used = 0; used < K && !pq.empty(); ) {
        auto [value, u] = pq.top();
        pq.pop();

        // Ha már elavult érték
        if (value != current[u]) continue;

        if (current[u] == 0) break;

        // Kiválasztjuk ezt a részfát
        ll taken = min(current[u], C);
        result += taken;
        used++;

        ll full = current[u];

        // Levonjuk az egész részfát az ősökből
        int x = u;
        while (x != 0) {
            current[x] -= full;
            x = parent[x];
        }
    }

    cout << result << "\n";

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base3/45
1Wrong answer0/01ms316 KiB
2Wrong answer0/07ms1332 KiB
3Wrong answer0/11ms316 KiB
4Wrong answer0/11ms316 KiB
5Wrong answer0/11ms316 KiB
6Wrong answer0/11ms316 KiB
7Wrong answer0/11ms564 KiB
8Wrong answer0/12ms316 KiB
9Accepted1/14ms1332 KiB
10Accepted1/14ms1316 KiB
11Accepted1/14ms1332 KiB
12Wrong answer0/11ms380 KiB
13Wrong answer0/11ms500 KiB
14Wrong answer0/11ms316 KiB
15Wrong answer0/12ms316 KiB
16Wrong answer0/12ms544 KiB
17Wrong answer0/12ms316 KiB
18Wrong answer0/11ms316 KiB
19Wrong answer0/12ms316 KiB
20Wrong answer0/11ms316 KiB
21Wrong answer0/27ms1332 KiB
22Wrong answer0/27ms1400 KiB
23Wrong answer0/27ms1404 KiB
24Wrong answer0/310ms2432 KiB
25Wrong answer0/312ms2268 KiB
26Wrong answer0/36ms1772 KiB
27Wrong answer0/36ms1908 KiB
28Wrong answer0/39ms3184 KiB
29Wrong answer0/39ms3248 KiB
30Wrong answer0/314ms3248 KiB