// 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