93782024-02-21 11:22:46szilTevefarmcpp17Accepted 50/5048ms13624 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 100'001;

ll a[MAXN];
bool mark[MAXN];
vector<int> g[MAXN];
vector<int> ans;

ll dfs(int u, int p = -1) {
    ll res = 0;
    for (int v : g[u]) {
        res += dfs(v, u);
    }
    if (a[u] > res) mark[u] = 1;
    return max(res, a[u]);
}

void dfs2(int u) {
    if (mark[u]) ans.emplace_back(u);
    else for (int v : g[u]) dfs2(v);
}

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 2; i <= n; i++) {
        int p; cin >> p;
        g[p].emplace_back(i);
    }
    cout << dfs(1) << "\n";
    dfs2(1);
    cout << ans.size() << "\n";
    for (int i : ans) cout << i << " ";
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms6676 KiB
2Accepted0/04ms7008 KiB
3Accepted4/44ms6948 KiB
4Accepted4/44ms6928 KiB
5Accepted4/44ms6940 KiB
6Accepted4/44ms7360 KiB
7Accepted4/424ms10168 KiB
8Accepted6/628ms10992 KiB
9Accepted6/632ms11836 KiB
10Accepted6/635ms12556 KiB
11Accepted6/643ms13016 KiB
12Accepted6/648ms13624 KiB