95772024-02-23 11:40:02csaron71Tevefarmcpp17Wrong answer 27/50100ms16124 KiB
#include <bits/stdc++.h>
using namespace std;

vector<long long> ertek;
vector<long long> apa;
vector<vector<long long> > gyerekek;
vector<int> vissza;
vector<long long> veg;

long long kereses(long long x) {
	if (gyerekek[x].size()>0) {
		long long epp=ertek[x];
		long long masik=0;
		for (long long sz : gyerekek[x]) {
			masik+=kereses(sz);
		}
		if (epp>=masik) {
			//veg.push_back(x);
			vissza[x]=10;
			return epp;
		}
		else {
			vissza[x]=-10;
			return masik;
		}
		//return max(epp, masik);
	}
	else {
		//veg.push_back(x);
		//vissza[x]=-10;
		return ertek[x];
	}
	
}

void visszaf (int x) {
	if (vissza[x]==-10) {
		for (int sz : gyerekek[x]) {
			visszaf(sz);
		}
	}
	else {
		veg.push_back(x+1);
	}
}

int main() {
	long long n;
	cin >> n;
	ertek.resize(n);
	vissza.assign(n, 0);
	for (long long i=0; i<n; i++) {
		cin >> ertek[i];
	}
	apa.resize(n);
	apa[0]=0;
	gyerekek.assign(n, vector<long long>());
	for (long long i=1; i<n; i++) {
		cin >> apa[i];
		apa[i]--;
		gyerekek[apa[i]].push_back(i);
	}

	cout << kereses(0) << "\n";

	visszaf(1);

	//sort(veg.begin(), veg.end());
	cout << veg.size() << "\n";
	for (long long sz : veg) {
		cout << sz << " ";
	}
	cout << "\n";
	/*for (auto sz : vissza) {
		cout << sz << " ";
	}*/
}
SubtaskSumTestVerdictTimeMemory
base27/50
1Wrong answer0/03ms1688 KiB
2Wrong answer0/03ms2044 KiB
3Partially correct2/43ms2060 KiB
4Accepted4/43ms2284 KiB
5Partially correct2/43ms2508 KiB
6Partially correct2/43ms2880 KiB
7Partially correct2/441ms9360 KiB
8Partially correct3/650ms11020 KiB
9Partially correct3/657ms11896 KiB
10Partially correct3/667ms13672 KiB
11Partially correct3/686ms15076 KiB
12Partially correct3/6100ms16124 KiB