#include <bits/stdc++.h>
using namespace std;
// #include "friend.h"
int colour[100001], dp[2][100001], c[100001];
vector<int> graph[100001];
void dfs(int node) {
int curr = 0;
dp[0][node] = c[node];
for (int i : graph[node]) {
dfs(i);
if (!colour[i]) dp[1][node] += dp[0][i], dp[0][node] += dp[1][i];
else dp[0][node] += dp[0][i], curr += dp[1][i];
}
curr += dp[1][node];
dp[0][node] = max(dp[0][node], curr);
for (int i : graph[node]) {
if (!colour[i]) curr += dp[1][i] - dp[0][i];
else curr += dp[0][i] - dp[1][i];
dp[0][node] = max(dp[0][node], curr);
}
}
int findSample(int n, int confidence[], int host[], int protocol[]) {
if (protocol[1] == 2) return *max_element(confidence, confidence + n);
c[0] = confidence[0];
for (int i = 1; i < n; i++) {
c[i] = confidence[i];
colour[i] = protocol[i];
graph[host[i]].push_back(i);
}
dfs(0);
return dp[0][0];
}
#define __MAXSIZE__ 100002
// Confidence
int confidence[__MAXSIZE__];
// Host
int host[__MAXSIZE__];
// Protocol
int protocol[__MAXSIZE__];
// Main
int main(void)
{
int n,i;
// Number of people
assert(scanf("%d",&n)==1);
// Confidence
for(i=0;i<n;i++)
assert(scanf("%d",&confidence[i])==1);
// Host and Protocol
for(i=1;i<n;i++)
assert(scanf("%d %d",&host[i],&protocol[i])==2);
// Answer
printf("%d\n",findSample(n,confidence,host,protocol));
return 0;
}
Compilation message
/tmp/ccapW5rP.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccxHVf98.o:friend.cpp:(.text.startup+0x0): first defined here
/tmp/ccapW5rP.o:(.bss+0xc3540): multiple definition of `confidence'
/tmp/ccxHVf98.o:(.bss+0xc3540): first defined here
/tmp/ccapW5rP.o:(.bss+0x61aa0): multiple definition of `host'
/tmp/ccxHVf98.o:(.bss+0x61aa0): first defined here
/tmp/ccapW5rP.o:(.bss+0x0): multiple definition of `protocol'
/tmp/ccxHVf98.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status