func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n, m, k, t;   long long cnm[N];   long long h[N], w[N], e[N], ne[N], idx;   vector<long long> g[N];   int vis[N];   long long ans;   vector<int> res;   void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }   void dfs(int u, int s) {     if (s == 1) {       if (cnm[u] >= 0) {         ans += cnm[u];         res.push_back(u);         vis[u] = true;         for (int i = h[u]; i != -1; i = ne[i]) {           int j = e[i];           cnm[j] += cnm[u];           dfs(j, 2);         }       } else {         vis[u] = true;         for (int i = h[u]; i != -1; i = ne[i]) {           int j = e[i];           dfs(j, 2);         }         ans += cnm[u];         res.push_back(u);       }     } else {       bool ok = true;       for (auto it : g[u]) {         if (!vis[it]) {           ok = false;           dfs(it, 2);         }       }       if (ok) {         dfs(u, 1);       }     }   }   void solve() {     memset(h, -1, sizeof(h));     cin >> n;     for (int i = 1; i <= n; i++) cin >> cnm[i];     for (int i = 1; i <= n; i++) {       int a;       cin >> a;       if (a == -1) continue;       add(i, a);       g[a].push_back(i);     }     for (int i = 1; i <= n; i++) {       if (!vis[i] && g[i].size() == 0) {         dfs(i, 2);       }     }     cout << ans << endl;     for (auto it : res) cout << it <<    ;   }   int main() {     solve();     return 0;   }   