39892023-03-07 16:44:43eokorTevefarmcpp11Wrong answer 4/5079ms8276 KiB
#include <bits/stdc++.h>

using namespace std;

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

int main()
{
//    ifstream cin("in.txt");
    long long int n;
    cin>>n;
    long long int x;
    for(long long int i=1;i<=n;i++)
    {
        cin>>x;
        suly.push_back(x);
    }
    os.push_back(0);
    for(long long int i=2;i<=n;i++)
    {
        cin>>x;
        os.push_back(x);
        level[x]=false;
    }
    long long int ert=0;
    long long int ind;
    bool benne=0;
    for(long long 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(ind<i-1)
                benne=1;
            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;
            if(benne)
                i++;
            ert=0;
            benne=0;
        }
    cout<<suly[1];
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base4/50
1Wrong answer0/03ms1824 KiB
2Wrong answer0/03ms2072 KiB
3Partially correct2/43ms2236 KiB
4Wrong answer0/43ms2464 KiB
5Partially correct2/43ms2692 KiB
6Wrong answer0/43ms2912 KiB
7Wrong answer0/432ms4984 KiB
8Wrong answer0/639ms5488 KiB
9Wrong answer0/645ms7468 KiB
10Wrong answer0/652ms7720 KiB
11Wrong answer0/671ms8204 KiB
12Wrong answer0/679ms8276 KiB