#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
    // Fast I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    if (!(cin >> n)) return 0;

    vector<int> val(n + 1);
    vector<vector<int>> adj(n + 1);
    vector<int> ans(n + 1, 0);
    vector<bool> visited(n + 1, false);

    // Read the values (0 or 1) for each node
    for (int i = 1; i <= n; ++i) {
        cin >> val[i];
    }

    // Read the N-1 edges and build the adjacency list
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    // Initialize BFS Queue with the root node
    queue<int> q;
    q.push(1);
    visited[1] = true;
    
    // The root's path sum is just its own value
    ans[1] = val[1];

    // Standard BFS traversal
    while (!q.empty()) {
        int current = q.front();
        q.pop();

        // Check all connected neighbors
        for (int neighbor : adj[current]) {
            if (!visited[neighbor]) {
                visited[neighbor] = true;
                
                // Child's accumulated total = Parent's accumulated total + Child's value
                ans[neighbor] = ans[current] + val[neighbor];
                
                q.push(neighbor);
            }
        }
    }

    // Print the result for each node
    for (int i = 1; i <= n; ++i) {
        if (i == 1) {
            cout << ans[i];
        } else {
            cout << " " << ans[i];
        }
    }
    cout << "\n";

    return 0;
}