func_code_string: #include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( fast-math )   #pragma GCC optimize( move-loop-invariants )   std::mt19937 rnd(237);   using namespace std;   using ll = long long;   using ld = long double;   const int inf = 1e9 + 8;   const ll linf = 4e18 + 18;   const int N = 1e6 + 6;   const double pi = acos(-1.0);   int n;   vector<int> a, b, p, pp;   vector<vector<int>> g;   vector<ll> sum;   ll ans = 0;   void dfs(int v, int par = -1) {     sum[v] = a[v];     for (auto &u : g[v]) {       if (u != par) dfs(u, v);     }     if (sum[v] > 0) {       if (~par) sum[par] += sum[v];       p.push_back(v);     } else       pp.push_back(v);     ans += sum[v];   }   void solve() {     cin >> n;     a.resize(n);     b = a;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     g.assign(n, {});     vector<int> indeg(n, 0);     for (int i = 0; i < n; ++i) {       cin >> b[i];       if (~b[i]) {         g[b[i] - 1].push_back(i);         indeg[i]++;       }     }     sum.resize(n);     for (int i = 0; i < n; ++i) {       if (!indeg[i]) dfs(i);     }     cout << ans <<   n ;     for (auto &x : p) cout << x + 1 <<    ;     for (int i = (int)pp.size() - 1; ~i; --i) cout << pp[i] + 1 <<    ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int test_case = 1;     for (int t = 0; t < test_case; ++t) {       solve();       cout <<   n ;     }   }   