2462021-03-05 15:58:34balintttTevefarmcpp14Accepted 50/50104ms13140 KiB
#include<bits/stdc++.h>

using namespace std;

class pont{
public:
    vector<int> hova;
    long long mx;
    bool finish = 1;
    int index = 0;
    long long dfs();
    void dfs2();
};

int n;

vector<pont> p;

long long pont::dfs(){
    long long mx2 = 0;
    for(int i:hova){
        mx2+=p[i].dfs();
    }
    if(mx2 >= mx){
        mx = mx2;
        finish = 0;
    }
    return mx;
}

stack<int> er;

void pont::dfs2(){
    if(finish){
        er.push(index);
    } else {
        for(int i : hova){
            p[i].dfs2();
        }
    }
}

int main(){
    cin >> n;
    p.resize(n);
    for(int i=0;i<n;i++){
        cin >> p[i].mx;
        p[i].index = i;
    }
    int a;
    for(int i=1;i<n;i++){
        cin >> a;
        --a;
        p[a].hova.push_back(i);
    }
    cout << p[0].dfs() << '\n';
    p[0].dfs2();
    cout << er.size() << '\n';
    while(er.size()){
        cout << er.top()+1 << ' ';
        er.pop();
    }
return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1740 KiB
2Accepted0/02ms1824 KiB
3Accepted4/42ms1824 KiB
4Accepted4/42ms1828 KiB
5Accepted4/42ms1848 KiB
6Accepted4/43ms1880 KiB
7Accepted4/443ms7360 KiB
8Accepted6/652ms8396 KiB
9Accepted6/664ms9568 KiB
10Accepted6/672ms11000 KiB
11Accepted6/693ms12036 KiB
12Accepted6/6104ms13140 KiB