#include "friend.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#define debug(x) #x << " = " << x << '\n'
using ll = long long;
// Find out best sample
int findSample(int n, int a[], int parent[], int type[]){
std::vector<std::vector<int>> g(n);
for (int i = 1; i < n; i++) {
g[parent[i]].push_back(i);
}
std::vector<std::vector<int>> dp(n, std::vector<int>(2, 0));
auto dfs = [&](auto &&self, int u) -> void {
dp[u][0] = 0;
dp[u][1] = a[u];
std::reverse(g[u].begin(), g[u].end());
for (int v : g[u]) {
self(self, v);
int old0 = dp[u][0];
int old1 = dp[u][1];
if (type[v] == 0) {
dp[u][0] += std::max(dp[v][0], dp[v][1]);
dp[u][1] += dp[v][0];
} else if (type[v] == 1) {
dp[u][0] += dp[v][0];
dp[u][1] = std::max({old1 + dp[v][1], old1 + dp[v][0], old0 + dp[v][1]});
} else {
dp[u][0] += dp[v][0];
dp[u][1] = std::max(old1 + dp[v][0], old0 + dp[v][1]);
}
}
};
dfs(dfs, 0);
return std::max(dp[0][0], dp[0][1]);
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |