48712023-04-05 12:05:39tamasmarkTevefarmcpp17Accepted 50/50108ms15228 KiB
// tevefarm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#define ll long long

using namespace std;

ll n, i, m, a, b;
struct adat
{
    bool lat,ok;
    ll db;
    vector<ll>sz;
};
vector<adat>x;
deque<ll>megold;
ll melysegi(ll csp)
{
    ll maxi = 0;
    if (!x[csp].sz.empty())
    {
        for (auto& e : x[csp].sz)
        {
            maxi+=melysegi(e);
        }
        if (x[csp].db >= maxi)
        {
            x[csp].ok = true;
            return x[csp].db;
        }
        else return maxi;
    }
    else
    {
        x[csp].ok = true;
        return x[csp].db;
    }
}
void rekur(ll csp)
{
    if (x[csp].ok)
    {
        megold.push_back(csp);
    }
    else
    {
        for (auto& e : x[csp].sz) rekur(e);
    }
}
int main()
{
    cin >> n;
    x.resize(n + 1);
    for (i = 1; i <= n; ++i)
    {
        cin >> x[i].db;
    }
    for (i = 1; i <= n - 1; ++i)
    {
        cin >> a;
        x[a].sz.push_back(i+1);
    }

    cout<<melysegi(1)<<"\n";
    rekur(1);
    cout << megold.size() << "\n";
    sort(megold.begin(), megold.end());
    for (auto& e : megold) cout << e << " ";
    return 0;
}
/*
8
5 5 7 1 2 3 2 4
1
1
2
2
2
3
3

*/
// 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
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/03ms2240 KiB
3Accepted4/42ms2236 KiB
4Accepted4/42ms2320 KiB
5Accepted4/43ms2340 KiB
6Accepted4/43ms2724 KiB
7Accepted4/446ms9008 KiB
8Accepted6/654ms10120 KiB
9Accepted6/664ms11368 KiB
10Accepted6/674ms12892 KiB
11Accepted6/697ms14104 KiB
12Accepted6/6108ms15228 KiB