| Task: | Tree game |
| Sender: | Dereden |
| Submission time: | 2025-10-06 17:49:36 +0300 |
| Language: | C++ (C++17) |
| Status: | COMPILE ERROR |
Compiler report
input/code.cpp: In function 'int main()':
input/code.cpp:32:5: error: 'stack' was not declared in this scope
32 | stack<int> st;
| ^~~~~
input/code.cpp:6:1: note: 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'?
5 | #include <queue>
+++ |+#include <stack>
6 |
input/code.cpp:32:11: error: expected primary-expression before 'int'
32 | stack<int> st;
| ^~~
input/code.cpp:33:5: error: 'st' was not declared in this scope; did you mean 'std'?
33 | st.push(1);
| ^~
| stdCode
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("input.txt", "r", stdin); // TODO: REMOVE THIS YOU STUPID ****
int n;
cin >> n;
vector<ll> a(n+1);
for (int i = 1; i <= n; ++i) cin >> a[i];
vector<vector<int>> children(n+1);
for (int i = 1; i <= n; ++i) {
int p; cin >> p;
if (i == 1) continue;
children[p].push_back(i);
}
vector<int> order;
order.reserve(n);
stack<int> st;
st.push(1);
while (!st.empty()) {
int v = st.top(); st.pop();
order.push_back(v);
for (int u : children[v]) st.push(u);
}
vector<ll> f(n+1, 0);
for (int idx = (int)order.size() - 1; idx >= 0; --idx) {
int v = order[idx];
int k = (int)children[v].size();
if (k <= 1) {
f[v] = a[v];
} else {
ll max1 = 0, max2 = 0;
for (int u : children[v]) {
ll val = f[u];
if (val > max1) {
max2 = max1;
max1 = val;
} else if (val > max2) {
max2 = val;
}
}
f[v] = a[v] + max2;
}
}
cout << f[1];
return 0;
}
