#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5; // Max nodes
vector<int> adj[N]; // Adjacency list
int nodeValue[N]; // Stores the value (0 or 1) of each node
int dp[N], dp2[N]; // DP arrays

// DFS function to compute longest path of 1's
void dfs(int node, int parent) {
    if (nodeValue[node] == 0) {
        dp[node] = dp2[node] = 0;
        return;
    }

    dp[node] = 1; // At least the node itself
    vector<int> childPaths; // Store child dp values

    for (int child : adj[node]) {
        if (child == parent) continue;
        dfs(child, node);
        childPaths.push_back(dp[child]);
    }

    // Sort child paths in descending order
    sort(childPaths.rbegin(), childPaths.rend());

    // Longest vertical path
    if (!childPaths.empty()) dp[node] += childPaths[0];

    // Longest V-path
    if (childPaths.size() > 1) dp2[node] = 1 + childPaths[0] + childPaths[1];
}

int main() {
    int n;
    cout << "Enter number of nodes: ";
    cin >> n;

    cout << "Enter node values (0 or 1) for each node from 1 to " << n << ":\n";
    for (int i = 1; i <= n; i++) {
        cin >> nodeValue[i];
    }

    cout << "Enter " << n-1 << " edges (parent child format):\n";
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int root = -1;
    for (int i = 1; i <= n; i++) {
        if (nodeValue[i] == 1) {
            root = i;
            break;
        }
    }

    if (root == -1) {
        cout << "Final Answer: 0 (No '1' in the tree)\n";
        return 0;
    }

    dfs(root, -1);

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans = max({ans, dp[i], dp2[i]});
    }

    cout << "Final Answer: " << ans << endl;

    return 0;
}
