42942023-03-22 13:49:00horvathabelTevefarmcpp17Partially correct 48/50120ms19096 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<ll> g[100001]; 
vector<ll> c(100001);
pair<ll, ll> megero[100001];
vector<ll> penz(100001);
ll dfs(int x){
	if (c[x]==0){
		megero[x]={penz[x],-1};
		return (penz[x]);
	}
	else{
		ll ans=0; 
		for (int edge:g[x]){
			ans+=dfs(edge);
		}
		megero[x]={penz[x],ans};
	}
	return (max(megero[x].first, megero[x].second));
}
vector<int> ans; 
void dfskereso(int x){
	for (int edge:g[x]){
		if (megero[edge].first>megero[edge].second){
			ans.push_back(edge); 
		}
		else{
			dfskereso(edge);
		}
	}
}
int main() {
	int n;
	cin>>n;
	for (int i=1; i<=n;i++){
		cin>>penz[i];
	}
	for (int i=2; i<=n;i++){
		ll x;
		cin>>x;
		c[x]++; 
		g[x].push_back(i);
	}
	dfs(1);
	cout<<max(megero[1].first, megero[1].second)<<endl; 
	dfskereso(1); 
	sort(ans.begin(), ans.end());
	cout<<ans.size()<<endl; 
	for (auto x: ans) cout<<x<<" ";
}
SubtaskSumTestVerdictTimeMemory
base48/50
1Accepted0/06ms9532 KiB
2Accepted0/06ms9808 KiB
3Partially correct2/46ms10016 KiB
4Accepted4/46ms10288 KiB
5Accepted4/46ms10504 KiB
6Accepted4/46ms10576 KiB
7Accepted4/450ms14836 KiB
8Accepted6/657ms15760 KiB
9Accepted6/665ms16988 KiB
10Accepted6/675ms17908 KiB
11Accepted6/698ms18588 KiB
12Accepted6/6120ms19096 KiB