46742023-03-30 20:41:03ZsofiaKeresztelyTevefarmcpp14Accepted 50/50104ms13420 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<int> > g;
vector<ll> camel;
vector<bool> chosen;
vector<int> op;

ll dfs(int v){
    ll r = 0;
    for (int x : g[v]){
        r += dfs(x);
    }
    if (camel[v] > r){
        r = camel[v];
        chosen[v] = true;
    }
    return r;
}

void dfs2(int v){
    if (chosen[v]){
        op.push_back(v);
        return;
    }
    for (int x : g[v]){
        dfs2(x);
    }
}

int main()
{
    int n;
    cin >> n;
    g.resize(n+1);
    camel.resize(n+1);
    chosen.assign(n+1, 0);
    for (int i=1; i<=n; i++){
        cin >> camel[i];
    }
    for (int i=2; i<=n; i++){
        int a;
        cin >> a;
        g[a].push_back(i);
    }
    cout << dfs(1) << "\n";
    dfs2(1);
    cout << op.size() << "\n";
    for (int x : op){
        cout << x << " ";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1748 KiB
2Accepted0/03ms2088 KiB
3Accepted4/43ms2124 KiB
4Accepted4/43ms2336 KiB
5Accepted4/43ms2600 KiB
6Accepted4/43ms2940 KiB
7Accepted4/443ms7948 KiB
8Accepted6/650ms8936 KiB
9Accepted6/659ms10304 KiB
10Accepted6/668ms11208 KiB
11Accepted6/692ms12176 KiB
12Accepted6/6104ms13420 KiB