| 25337 | 2026-02-19 11:26:16 | Kevin | Raktárba szállítás (45 pont) | cpp17 | Forditási hiba |
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N, K, C;
cin >> N >> K >> C;
vector<int> parent(N + 1);
vector<int> alkatresz(N + 1);
vector<vector<int>> children(N + 1);
for (int i = 1; i <= N; i++) {
int p, a;
cin >> p >> a;
parent[i] = p;
alkatresz[i] = a;
if (p != 0) {
children[p].push_back(i);
}
}
vector<int> dp(N + 1, 0);
// Post-order bejárás (rekurzívan)
// Mivel N <= 20000, rekurzióval is mehet, de a veremméret miatt lehet, hogy iteratív kell.
// Itt egyszerűbb rekurzióval, de nagyobb N-re iteratív DFS kellene.
function<void(int)> dfs = [&](int v) {
if (children[v].empty()) {
dp[v] = alkatresz[v];
return;
}
vector<int> childVals;
for (int u : children[v]) {
dfs(u);
childVals.push_back(dp[u]);
}
// Rendezzük csökkenőbe
sort(childVals.rbegin(), childVals.rend());
int capacity = C;
int total = alkatresz[v];
capacity -= alkatresz[v];
for (int val : childVals) {
if (capacity <= 0) break;
int take = min(val, capacity);
total += take;
capacity -= take;
}
dp[v] = total;
};
dfs(1);
// A gyökérből induló kamion értéke is számít? Igen, mert a gyökérből induló kamion viheti a gyerekekből jövő alkatrészeket.
// De mivel a gyökérben nincs saját alkatrész (A[1]=0), csak a gyerekekből szed.
vector<int> values;
for (int i = 1; i <= N; i++) {
values.push_back(dp[i]);
}
sort(values.rbegin(), values.rend());
long long ans = 0;
for (int i = 0; i < K && i < N; i++) {
ans += values[i];
}
cout << ans << "\n";
return 0;
}open /var/local/lib/isolate/401/box/a.out: no such file or directory
main.cpp: In function 'int main()':
main.cpp:35:5: error: 'function' was not declared in this scope
35 | function<void(int)> dfs = [&](int v) {
| ^~~~~~~~
main.cpp:5:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
4 | #include <queue>
+++ |+#include <functional>
5 |
main.cpp:35:14: error: expected primary-expression before 'void'
35 | function<void(int)> dfs = [&](int v) {
| ^~~~
main.cpp:61:5: error: 'dfs' was not declared in this scope
61 | dfs(1);
| ^~~