#include "friend.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
using pr = pair<int, int>;
const int INF = 1e9+7, MOD = 1e9+7;
// Find out best sample
int findSample(int n,int a[],int host[],int type[]){
vector<int> col(n, 0), hasfren(n, 0);
col[0] = 0;
int color = 0;
for (int i = 1; i < n; i++) {
if (type[i] == 0) {
hasfren[host[i]] = 1;
hasfren[i] = 1;
col[i] = col[host[i]] ^ 1;
}
if (type[i] == 1 && !hasfren[host[i]]) {
col[i] = color*2;
color++;
}
if (type[i] == 1 && hasfren[host[i]]) {
hasfren[i] = 1;
col[i] = col[host[i]];
}
}
vector<int> cnt(color*2+2, 0);
int res = 0;
for (int i = 0; i < n; i++) cnt[col[i]] += a[i];
for (int i = 0; i <= color; i++) {
res += max(cnt[i*2], cnt[i*2+1]);
}
return res;
}