42912023-03-22 13:38:15horvathabelTevefarmcpp17Wrong answer 19/5096ms14488 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100001]; 
vector<int> c(100001);
pair<int, int> megero[100001];
vector<int> penz(100001);
int dfs(int x){
	if (c[x]==0){
		return (penz[x]);
	}
	else{
		int ans=0; 
		for (int edge:g[x]){
			ans+=dfs(edge);
		}
		megero[x]={penz[x],ans};
	}
	return (max(megero[x].first, megero[x].second));
}
int main() {
	int n;
	cin>>n;
	for (int i=1; i<=n;i++){
		cin>>penz[i];
	}
	for (int i=2; i<=n;i++){
		int x;
		cin>>x;
		c[x]++; 
		g[x].push_back(i);
	}
	dfs(1);
	cout<<max(megero[1].first, megero[1].second); 
}
SubtaskSumTestVerdictTimeMemory
base19/50
1Wrong answer0/06ms7980 KiB
2Wrong answer0/06ms8264 KiB
3Partially correct2/44ms8536 KiB
4Partially correct2/44ms8800 KiB
5Partially correct2/44ms9008 KiB
6Partially correct2/44ms9220 KiB
7Partially correct2/439ms11968 KiB
8Partially correct3/646ms12696 KiB
9Partially correct3/656ms12576 KiB
10Partially correct3/663ms13376 KiB
11Wrong answer0/686ms13848 KiB
12Wrong answer0/696ms14488 KiB