95742024-02-23 11:17:51csaron71Tevefarmcpp17Wrong answer 27/50101ms17020 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];
	}
	
}

int main() {
	long long n;
	cin >> n;
	ertek.resize(n);
	vissza.assign(n, 10);
	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";

	for (int i=0; i<n; i++) {
		if (vissza[apa[i]]==-10 && vissza[i]==10) {
			veg.push_back(i+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
1Accepted0/03ms1960 KiB
2Wrong answer0/04ms2408 KiB
3Partially correct2/43ms2492 KiB
4Partially correct2/43ms2608 KiB
5Accepted4/43ms2844 KiB
6Partially correct2/43ms3220 KiB
7Partially correct2/443ms10172 KiB
8Partially correct3/652ms11544 KiB
9Partially correct3/661ms13328 KiB
10Partially correct3/668ms14404 KiB
11Partially correct3/690ms15908 KiB
12Partially correct3/6101ms17020 KiB