39872023-03-07 16:39:23eokorTevefarmcpp11Time limit exceeded 0/50518ms107592 KiB
#include <bits/stdc++.h>

using namespace std;

vector  < int > suly(1,-1);
vector  < int > os(1,-1);
vector  < bool > level  (100001,true);
set <int> meg;

int main()
{
    ifstream cin("in.txt");
    long long int n;
    cin>>n;
    int x;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        suly.push_back(x);
    }
    os.push_back(0);
    for(int i=2;i<=n;i++)
    {
        cin>>x;
        os.push_back(x);
        level[x]=false;
    }
    int ert=0;
    int ind;

    for(int i=n;i>=2;i--)
        {
            ind=i;

            while(level[ind] && os[i]==os[ind])
            {
//                cout<<os[ind]<<" ";
                ert+=suly[ind];
//                cout<<ind<<" ";
                ind--;

            }

            if(ert>suly[os[i]])
                {
                    suly[os[i]]=ert;
//                    cout<<ind<<" "<<suly[i]<<endl;
                }
            level[os[i]]=true;
//            cout<<os[i]<<" "<<suly[os[i]]<<endl;

            i=ind;
            i++;
            ert=0;
        }
    cout<<suly[1];
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Time limit exceeded0/0518ms107592 KiB
2Runtime error0/090ms64744 KiB
3Runtime error0/479ms64520 KiB
4Runtime error0/479ms64280 KiB
5Runtime error0/490ms64040 KiB
6Runtime error0/479ms63804 KiB
7Runtime error0/479ms63564 KiB
8Runtime error0/679ms63536 KiB
9Runtime error0/678ms63548 KiB
10Runtime error0/678ms63436 KiB
11Runtime error0/678ms63204 KiB
12Runtime error0/678ms62968 KiB