42922023-03-22 13:39:02horvathabelTevefarmcpp17Wrong answer 25/5096ms18408 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){
		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));
}
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); 
}
SubtaskSumTestVerdictTimeMemory
base25/50
1Wrong answer0/06ms9668 KiB
2Wrong answer0/06ms9940 KiB
3Partially correct2/46ms9996 KiB
4Partially correct2/46ms10212 KiB
5Partially correct2/46ms10420 KiB
6Partially correct2/46ms10664 KiB
7Partially correct2/441ms14136 KiB
8Partially correct3/648ms15004 KiB
9Partially correct3/656ms15652 KiB
10Partially correct3/664ms16424 KiB
11Partially correct3/687ms17552 KiB
12Partially correct3/696ms18408 KiB