20272022-12-15 10:41:32BenceTevefarmcpp11Accepted 50/50101ms12536 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

//ifstream cin("file.in");
//ofstream cout("file.out");

struct adat
{
    bool jo,lat;
    int teve;
    vector <int> sz;
};
queue <int> s;
vector <adat> x;
int n,i,a,db;

long long rekurzio (int pont)
{
    long long ss=0;
    if(!x[pont].sz.empty())
    {
        for(auto &e:x[pont].sz)
        {
            ss+=rekurzio(e);
        }
        if(x[pont].teve>=ss)
        {
            x[pont].jo=1;
            return x[pont].teve;
        }else{
            return ss;
        }
    }else{
        x[pont].jo=1;
        return x[pont].teve;
    }
}

void rekur (int pont)
{
    if(x[pont].jo==1)
    {
        x[pont].lat=1;
        ++db;
    }else{
        for(auto &e:x[pont].sz)
            rekur(e);
    }
}

int main()
{
    cin>>n;
    x.resize(n+1);
    for(i=1; i<=n; ++i)
    {
        cin>>x[i].teve;
    }
    for(i=2; i<=n; ++i)
    {
        cin>>a;
        x[a].sz.push_back(i);
    }

    cout<<rekurzio(1)<<'\n';
    rekur(1);
    cout<<db<<'\n';
    for(i=1; i<=n; ++i)
    {
        if(x[i].lat)
            cout<<i<<' ';
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/03ms2236 KiB
3Accepted4/42ms2256 KiB
4Accepted4/42ms2416 KiB
5Accepted4/42ms2640 KiB
6Accepted4/43ms2740 KiB
7Accepted4/441ms7164 KiB
8Accepted6/652ms8608 KiB
9Accepted6/659ms9424 KiB
10Accepted6/667ms10596 KiB
11Accepted6/692ms11320 KiB
12Accepted6/6101ms12536 KiB