153942025-02-19 11:23:15zsebiTevefarmcpp17Partially correct 48/50111ms7596 KiB
// tevefarm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <queue>
#define ll long long
using namespace std;
struct adat
{
    ll teve = 0, tmax = 0;
    bool gyerek = false;
    vector<ll>sz;
};
vector<adat>x;
vector<ll>ans;
ll n;
void ltev(ll p)
{
    if (x[p].sz.size() == 0)
    {
        x[p].tmax = x[p].teve;
        return;
    }
    
        ll s = 0;
        for (auto& e : x[p].sz)
        {
            ltev(e);
            s += x[e].tmax;
        }if (p == 1)
        {
            x[p].tmax = s;
        }
        if (s > x[p].teve || p==1)x[p].gyerek = true;
        x[p].tmax = max(s, x[p].teve);
    
    return;
}
void melys(ll p)
{
    if (!x[p].gyerek)
    {
        ans.push_back(p);
        return;
    }
    for (auto& e : x[p].sz)melys(e);
}
int main()
{
    cin >> n;
    x.resize(n + 1);
    for (int i = 1; i <= n; ++i)cin >> x[i].teve;
    for (int i = 2; i <= n; ++i)
    {
        ll a;
        cin >> a;
        x[a].sz.push_back(i);
    }
    ltev(1);
    cout << x[1].tmax<<"\n";
    melys(1);
    cout << ans.size()<<"\n";
    for (auto& e : ans)cout << e << " ";
    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base48/50
1Accepted0/01ms316 KiB
2Accepted0/02ms508 KiB
3Partially correct2/41ms508 KiB
4Accepted4/41ms316 KiB
5Accepted4/41ms316 KiB
6Accepted4/42ms316 KiB
7Accepted4/445ms4040 KiB
8Accepted6/652ms4704 KiB
9Accepted6/664ms5624 KiB
10Accepted6/671ms6080 KiB
11Accepted6/697ms6828 KiB
12Accepted6/6111ms7596 KiB