Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    a = [0] + map(int, stdin.readline().split(), repeat(10, n))
    m = 2 * (n - 1)
    dat = map(int, stdin.read().split(), repeat(10, m))
    j = 0
    xt = [None] * m
    la = [None] * (n + 1)
    deg = [0] * (n + 1)
    if n == 2:
        print "YES" if a[1] == a[2] else "NO"
        return
    for i in xrange(n - 1):
        x, y = dat[j], dat[j+1]
        xt[j+1] = la[x]
        xt[j] = la[y]
        deg[x] += 1
        deg[y] += 1
        la[x] = j + 1
        la[y] = j
        j += 2
    st = []
    pu = st.append
    po = st.pop
    for i in xrange(1, n + 1):
        if deg[i] != 1:
            r = i
            break
    pu(r)
    col = [0] * (n + 1)
    sd = [0] * (n + 1)
    while st:
        x = po()
        if col[x] == 0:
            col[x] = 1
            pu(x)
            y = la[x]
            while y is not None:
                z = dat[y]
                if not col[z]:
                    pu(z)
                y = xt[y]
        elif deg[x] > 1:
            col[x] = 2
            y = la[x]
            ss = 0
            ms = 0
            while y is not None:
                if col[dat[y]] == 2:
                    z = sd[dat[y]]
                    ss += z
                    if ms < z:
                        ms = z
                    y = xt[y]
            if ss > 2 * a[x]:
                print "NO"
                return
            sd[x] = 2 * a[x] - ss
            if (a[x] - sd[x]) / 2 > min(ms, ss / 2):
                print "NO"
                return
        else:
            col[x] = 2
            sd[x] = a[x]
    print "YES" if sd[r] == 0 else "NO"
main()


Fixed submission:
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    a = [0] + map(int, stdin.readline().split(), repeat(10, n))
    m = 2 * (n - 1)
    dat = map(int, stdin.read().split(), repeat(10, m))
    j = 0
    xt = [None] * m
    la = [None] * (n + 1)
    deg = [0] * (n + 1)
    if n == 2:
        print "YES" if a[1] == a[2] else "NO"
        return
    for i in xrange(n - 1):
        x, y = dat[j], dat[j+1]
        xt[j+1] = la[x]
        xt[j] = la[y]
        deg[x] += 1
        deg[y] += 1
        la[x] = j + 1
        la[y] = j
        j += 2
    st = []
    pu = st.append
    po = st.pop
    for i in xrange(1, n + 1):
        if deg[i] != 1:
            r = i
            break
    pu(r)
    col = [0] * (n + 1)
    sd = [0] * (n + 1)
    while st:
        x = po()
        if col[x] == 0:
            col[x] = 1
            pu(x)
            y = la[x]
            while y is not None:
                z = dat[y]
                if not col[z]:
                    pu(z)
                y = xt[y]
        elif deg[x] > 1:
            col[x] = 2
            y = la[x]
            ss = 0
            ms = 0
            while y is not None:
                if col[dat[y]] == 2:
                    z = sd[dat[y]]
                    ss += z
                    if ms < z:
                        ms = z
                y = xt[y]
            p = ss - a[x]
            if p < 0 or ss < p * 2:
                print "NO"
                return
            sd[x] = ss - p * 2
            if p > ss - ms:
                print "NO"
                return
        else:
            col[x] = 2
            sd[x] = a[x]
    print "YES" if sd[r] == 0 else "NO"
main()