89972024-02-11 13:47:09xxxTevefarmcpp17Accepted 50/50108ms24268 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int mxN = 1e5+1;
vector<vector<int> > adj;
int n, m;
int c[mxN];
bool vis[mxN], joe[mxN];
vector<int> ans;

int dfs(int v, int p) {
    vis[v] = 1;
    
    int mxsum = c[v];
    int sum = 0;
    for(auto u : adj[v]) {
        if(u != p) {
            sum += dfs(u, v);
        }
    }
    if (mxsum < sum) {
        mxsum = sum;
        joe[v] = 1;
    }
    
    return mxsum;
}

void dfs2(int v, int p) {
    vis[v] = 1;
    
    if (joe[v]) {
        for(auto u : adj[v]) {
            if (u != p) {
                dfs2(u, v);
            }
        }
    } else {
        ans.push_back(v);
    }
    
    return;
}

signed main() {
    cin >> n;
    adj.resize(n+1);
    for(int i = 1; i <= n; i++) {
        cin >> c[i];
    }

    for(int i = 2; i <= n; i++) {
        int x;
        cin >> x;
        adj[i].push_back(x);
        adj[x].push_back(i);
    }

    cout << dfs(1, 0) << endl;
    
    memset(vis, 0, mxN);
    dfs2(1, 0);

    cout << ans.size() << '\n';
    for(auto x : ans) {
        cout << x << ' ';
    }
	/*for(int i = 1; i <= n; i++) {
        cout << i << ' ' << dp[i] << ' ' << joe[i] << endl;
    }*/


    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2108 KiB
2Accepted0/03ms2636 KiB
3Accepted4/43ms2420 KiB
4Accepted4/43ms2532 KiB
5Accepted4/43ms2580 KiB
6Accepted4/43ms2620 KiB
7Accepted4/446ms10740 KiB
8Accepted6/654ms13052 KiB
9Accepted6/668ms15860 KiB
10Accepted6/674ms18240 KiB
11Accepted6/6100ms21244 KiB
12Accepted6/6108ms24268 KiB