Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N = int(input())
    A = list(map(int, input().split()))
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    for v in range(1, N+1):
        if len(adj[v]) != 1:
            v0 = v
            break

    # from collections import deque
    que = deque()
    que.append(v0)
    seen = [-1] * (N+1)
    seen[v0] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)

    for v in reversed(seq):
        if not child[v]:
            if A[v-1] == 0:
                print('NO')
                exit()
            continue
        L = []
        for u in child[v]:
            L.append(A[u-1])
        if len(L) == 1:
            if L[0] != A[v-1]:
                print('NO')
                exit()
            continue
        s = sum(L)
        m = max(L)
        if s < A[v-1]:
            print('NO')
            exit()
        if A[v-1] < (s+1) // 2:
            print('NO')
            exit()
        if A[v-1] < m:
            print('NO')
            exit()
        A[v-1] = 2 * A[v-1] - s

    if A[v0-1]:
        print('NO')
    else:
        print('YES')


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N = int(input())
    A = list(map(int, input().split()))
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    if N == 2:
        if A[0] == A[1]:
            print('YES')
        else:
            print('NO')
        exit()

    for v in range(1, N+1):
        if len(adj[v]) != 1:
            v0 = v
            break

    # from collections import deque
    que = deque()
    que.append(v0)
    seen = [-1] * (N+1)
    seen[v0] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)

    for v in reversed(seq):
        if not child[v]:
            continue
        L = []
        for u in child[v]:
            L.append(A[u-1])
        if len(L) == 1:
            if L[0] != A[v-1]:
                print('NO')
                exit()
            continue
        s = sum(L)
        m = max(L)
        if s < A[v-1]:
            print('NO')
            exit()
        if A[v-1] < (s+1) // 2:
            print('NO')
            exit()
        if A[v-1] < m:
            print('NO')
            exit()
        A[v-1] = 2 * A[v-1] - s

    if A[v0-1]:
        print('NO')
    else:
        print('YES')


if __name__ == '__main__':
    main()