46732023-03-30 20:39:22ZsofiaKeresztelyTevefarmcpp14Wrong answer 38/5093ms12072 KiB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
vector<vector<int> > g;
vector<int> camel;
vector<bool> chosen;
vector<int> op;

int dfs(int v){
    int 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
base38/50
1Accepted0/03ms1876 KiB
2Accepted0/03ms2248 KiB
3Accepted4/43ms2280 KiB
4Accepted4/43ms2500 KiB
5Accepted4/43ms2620 KiB
6Accepted4/43ms3124 KiB
7Accepted4/443ms7896 KiB
8Accepted6/650ms8996 KiB
9Accepted6/659ms10204 KiB
10Accepted6/668ms11148 KiB
11Wrong answer0/682ms11052 KiB
12Wrong answer0/693ms12072 KiB