89242024-02-05 15:06:34Balki22Tevefarmcpp17Wrong answer 21/5050ms21876 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int n; cin >> n;
	vector<int> t(n);
	vector<vector<int>> e(n, vector<int>(0));
	vector<int> parent(n);
	for (int i = 0; i < n; i++) cin >> t[i];
	for (int i = 1; i < n; i++) {
		int u; cin >> u; u--;
		e[u].push_back(i);
		e[i].push_back(u);
		parent[i] = u;
	}
	vector<short> chosen(n, 0);
	for (int i = n - 1; i >= 0; i--) {
		int sum = 0;
		for (int s : e[i]) {
			if (s != parent[i]) sum += t[s];
		}

		if (sum > t[i]) {
			chosen[i] = 1;
			for (int s : e[i]) {
				if (s != parent[i] && chosen[s] != 1) chosen[s] = 2;
			}
			t[i] = sum;
		}
		else {
			chosen[i] = 2;
			for (int s : e[i]) {
				if (s != parent[i]) chosen[s] = 1;
			}
		}
	}
	cout << t[0] << '\n';
	int count = 0;
	for (int isin : chosen) if (isin == 2) count++;
	cout << count << '\n';
	for (int i = 0; i < n; i++) if (chosen[i] == 2) cout << i+1 << ' ';
}
SubtaskSumTestVerdictTimeMemory
base21/50
1Accepted0/03ms1828 KiB
2Wrong answer0/03ms2252 KiB
3Partially correct2/43ms2556 KiB
4Partially correct2/43ms2488 KiB
5Accepted4/43ms2860 KiB
6Partially correct2/43ms2844 KiB
7Partially correct2/424ms10212 KiB
8Partially correct3/628ms12256 KiB
9Partially correct3/632ms14368 KiB
10Partially correct3/637ms16408 KiB
11Wrong answer0/646ms19180 KiB
12Wrong answer0/650ms21876 KiB