13292022-05-14 13:10:09nkdorka1212Tevefarmcpp11Accepted 50/5087ms31248 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

int n;
vector<vector<int>>g;
vector<bool>vis;
vector<int>row;
vector<ll>teve;

void top(int v)
{
    vis[v]=true;
    for(int x:g[v])
    {
        if(!vis[x])
        {
            top(x);
        }
    }
    row.push_back(v);
    return;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n;
    teve.resize(n+1);
    vis.resize(n+1,0);
    g.resize(n+1);
    vector<vector<int>>child(n+1);
    vector<int>db(n+1);
    vector<ll>maxi(n+1,0);
    vector<bool>kifok(n+1,0);
    for(int i=1;i<=n;i++)
    {
        cin>>teve[i];
    }
    for(int i=2;i<=n;i++)
    {
        int v;
        cin>>v;
        g[v].push_back(i);
        kifok[v]=true;
    }
    top(1);
    for(int x:row)
    {
        if(!kifok[x])
        {
            maxi[x]=teve[x];
            child[x].push_back(x);
            db[x]=1;
        }else
        {
            ll num=0;
            for(int y:g[x])
            {
                num+=maxi[y];
            }
            if(teve[x]<num)
            {
                maxi[x]=num;
                for(int y:g[x])
                {
                    child[x].push_back(y);
                    db[x]+=db[y];
                }
            }else
            {
                maxi[x]=teve[x];
                child[x].push_back(x);
                db[x]=1;
            }
        }
    }
    cout<<maxi[1]<<'\n';
    cout<<db[1]<<'\n';
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        if(child[v][0]==v)
        {
            cout<<v<<" ";
        }else
        {
            for(int x:g[v])
            {
                q.push(x);
            }
        }
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1820 KiB
2Accepted0/02ms2056 KiB
3Accepted4/42ms1876 KiB
4Accepted4/41ms1896 KiB
5Accepted4/41ms1944 KiB
6Accepted4/41ms2144 KiB
7Accepted4/432ms14120 KiB
8Accepted6/639ms17304 KiB
9Accepted6/648ms20444 KiB
10Accepted6/670ms23552 KiB
11Accepted6/674ms27168 KiB
12Accepted6/687ms31248 KiB