2432021-03-05 15:53:11balintttTevefarmcpp14Wrong answer 38/5096ms18288 KiB
#include<bits/stdc++.h>

using namespace std;

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

int n;

vector<pont> p;

int 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
base38/50
1Accepted0/02ms1740 KiB
2Accepted0/02ms1968 KiB
3Accepted4/41ms1852 KiB
4Accepted4/41ms1864 KiB
5Accepted4/41ms1880 KiB
6Accepted4/42ms1920 KiB
7Accepted4/441ms7928 KiB
8Accepted6/650ms9604 KiB
9Accepted6/661ms11508 KiB
10Accepted6/674ms13684 KiB
11Wrong answer0/690ms15636 KiB
12Wrong answer0/696ms18288 KiB