Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N=int(input())
A=list(map(int,input().split()))
edge=[[] for i in range(N)]
for _ in range(N-1):
    u,v=map(int,input().split())
    edge[u-1].append(v-1)
    edge[v-1].append(u-1)

ans=[0]
parent=[-1]*N
que=deque([(0,-1)])
while que:
    v,pv=que.popleft()
    for nv in edge[v]:
        if nv!=pv:
            parent[nv]=v
            ans.append(nv)
            que.append((nv,v))
ans=ans[::-1]


dpc=[[] for i in range(N)]
dpn=[[] for i in range(N)]
sz=[0]*N
for v in ans:
    sz[v]=1
    if A[v]<0:
        dpc[v]=[float("inf"),A[v]]
        for nv in edge[v]:
            if nv!=parent[v]:
                merged=[float("inf")]*(sz[v]+sz[nv]+1)
                for i in range(1,1+sz[v]):
                    for j in range(1,1+sz[nv]):
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpc[nv][j])
                        if dpn[nv][j]!=float("inf"):
                            merged[i+j]=min(merged[i+j],dpc[v][i])
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpn[nv][j])
                dpc[v]=merged
                sz[v]+=sz[nv]
        dpn[v]=[float("inf")]*(sz[v]+1)
    else:
        dpn[v]=[float("inf"),A[v]]
        for nv in edge[v]:
            if nv!=parent[v]:
                merged=[float("inf")]*(sz[v]+sz[nv]+1)
                for i in range(1,1+sz[v]):
                    for j in range(1,1+sz[nv]):
                        if dpc[nv][j]<0:
                            merged[i+j]=min(merged[i+j],dpn[v][i])
                        merged[i+j-1]=min(merged[i+j-1],dpn[v][i]+dpn[nv][j])
                        if dpn[nv][j]!=float("inf"):
                            merged[i+j]=min(merged[i+j],dpn[v][i])
                dpn[v]=merged
                sz[v]+=sz[nv]

        sz[v]=1
        dpc[v]=[float("inf"),A[v]]
        for nv in edge[v]:
            if nv!=parent[v]:
                merged=[float("inf")]*(sz[v]+sz[nv]+1)
                for i in range(1+sz[v]):
                    for j in range(1+sz[nv]):
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpc[nv][j])
                        if dpn[nv][j]!=float("inf"):
                            merged[i+j]=min(merged[i+j],dpc[v][i])
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpn[nv][j])
                dpc[v]=merged
                sz[v]+=sz[nv]

ans=N
for i in range(1,N+1):
    if dpc[0][i]<0 or dpn[0][i]!=float("inf"):
        ans=i
        break

print(ans-1)


Fixed submission:
from collections import deque

N=int(input())
A=list(map(int,input().split()))
edge=[[] for i in range(N)]
for _ in range(N-1):
    u,v=map(int,input().split())
    edge[u-1].append(v-1)
    edge[v-1].append(u-1)

ans=[0]
parent=[-1]*N
que=deque([(0,-1)])
while que:
    v,pv=que.popleft()
    for nv in edge[v]:
        if nv!=pv:
            parent[nv]=v
            ans.append(nv)
            que.append((nv,v))
ans=ans[::-1]

for v in range(N):
    edge[v]=[nv for nv in edge[v] if nv!=parent[v]]


dpc=[[] for i in range(N)]
dpn=[[] for i in range(N)]
sz=[0]*N
for v in ans:
    sz[v]=1
    sz[v]=1
    dpc[v]=[10**18,A[v]]
    for nv in edge[v]:
        merged=[10**18]*(sz[v]+sz[nv]+1)
        for i in range(1+sz[v]):
            for j in range(1+sz[nv]):
                merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+min(dpc[nv][j],dpn[nv][j]))
                if dpn[nv][j]<=10**15:
                    merged[i+j]=min(merged[i+j],dpc[v][i])
        dpc[v]=merged
        sz[v]+=sz[nv]
    sz[v]=1
    if A[v]<0:
        dpn[v]=[10**18]*2
    else:
        dpn[v]=[10**18,A[v]]
    for nv in edge[v]:
        merged=[10**18]*(sz[v]+sz[nv]+1)
        for i in range(1,1+sz[v]):
            for j in range(1,1+sz[nv]):
                if dpc[nv][j]<0:
                    merged[i+j]=min(merged[i+j],dpn[v][i])
                merged[i+j-1]=min(merged[i+j-1],dpn[v][i]+dpn[nv][j])
                if dpn[nv][j]<=10**15:
                    merged[i+j]=min(merged[i+j],dpn[v][i])
        dpn[v]=merged
        sz[v]+=sz[nv]

ans=N
for i in range(1,N+1):
    if dpc[0][i]<0 or dpn[0][i]<=10**15:
        ans=i
        break

print(ans-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(5001)

N=int(input())
A=list(map(int,input().split()))
edge=[[] for i in range(N)]
for _ in range(N-1):
    u,v=map(int,input().split())
    edge[u-1].append(v-1)
    edge[v-1].append(u-1)

dpc=[[] for i in range(N)]
dpn=[[] for i in range(N)]
sz=[0]*N

def dfs(v,pv):
    sz[v]=1
    if A[v]<0:
        dpc[v]=[float("inf"),A[v]]
        for nv in edge[v]:
            if nv!=pv:
                dfs(nv,v)
                merged=[float("inf")]*(sz[v]+sz[nv]+1)
                for i in range(1,1+sz[v]):
                    for j in range(1,1+sz[nv]):
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpc[nv][j])
                        if dpn[nv][j]!=float("inf"):
                            merged[i+j]=min(merged[i+j],dpc[v][i])
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpn[nv][j])
                dpc[v]=merged
                sz[v]+=sz[nv]
        dpn[v]=[float("inf")]*(sz[v]+1)
    else:
        dpn[v]=[float("inf"),A[v]]
        for nv in edge[v]:
            if nv!=pv:
                dfs(nv,v)
                merged=[float("inf")]*(sz[v]+sz[nv]+1)
                for i in range(1,1+sz[v]):
                    for j in range(1,1+sz[nv]):
                        if dpc[nv][j]<0:
                            merged[i+j]=min(merged[i+j],dpn[v][i])
                        merged[i+j-1]=min(merged[i+j-1],dpn[v][i]+dpn[nv][j])
                        if dpn[nv][j]!=float("inf"):
                            merged[i+j]=min(merged[i+j],dpn[v][i])
                dpn[v]=merged
                sz[v]+=sz[nv]

        sz[v]=1
        dpc[v]=[float("inf"),A[v]]
        for nv in edge[v]:
            if nv!=pv:
                merged=[float("inf")]*(sz[v]+sz[nv]+1)
                for i in range(1+sz[v]):
                    for j in range(1+sz[nv]):
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpc[nv][j])
                        if dpn[nv][j]!=float("inf"):
                            merged[i+j]=min(merged[i+j],dpc[v][i])
                        merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+dpn[nv][j])
                dpc[v]=merged
                sz[v]+=sz[nv]

dfs(0,-1)

ans=N
for i in range(1,N+1):
    if dpc[0][i]<0 or dpn[0][i]!=float("inf"):
        ans=i
        break

print(ans-1)

Fixed submission:
from collections import deque

N=int(input())
A=list(map(int,input().split()))
edge=[[] for i in range(N)]
for _ in range(N-1):
    u,v=map(int,input().split())
    edge[u-1].append(v-1)
    edge[v-1].append(u-1)

ans=[0]
parent=[-1]*N
que=deque([(0,-1)])
while que:
    v,pv=que.popleft()
    for nv in edge[v]:
        if nv!=pv:
            parent[nv]=v
            ans.append(nv)
            que.append((nv,v))
ans=ans[::-1]

for v in range(N):
    edge[v]=[nv for nv in edge[v] if nv!=parent[v]]


dpc=[[] for i in range(N)]
dpn=[[] for i in range(N)]
sz=[0]*N
for v in ans:
    sz[v]=1
    sz[v]=1
    dpc[v]=[10**18,A[v]]
    for nv in edge[v]:
        merged=[10**18]*(sz[v]+sz[nv]+1)
        for i in range(1+sz[v]):
            for j in range(1+sz[nv]):
                merged[i+j-1]=min(merged[i+j-1],dpc[v][i]+min(dpc[nv][j],dpn[nv][j]))
                if dpn[nv][j]<=10**15:
                    merged[i+j]=min(merged[i+j],dpc[v][i])
        dpc[v]=merged
        sz[v]+=sz[nv]
    sz[v]=1
    if A[v]<0:
        dpn[v]=[10**18]*2
    else:
        dpn[v]=[10**18,A[v]]
    for nv in edge[v]:
        merged=[10**18]*(sz[v]+sz[nv]+1)
        for i in range(1,1+sz[v]):
            for j in range(1,1+sz[nv]):
                if dpc[nv][j]<0:
                    merged[i+j]=min(merged[i+j],dpn[v][i])
                merged[i+j-1]=min(merged[i+j-1],dpn[v][i]+dpn[nv][j])
                if dpn[nv][j]<=10**15:
                    merged[i+j]=min(merged[i+j],dpn[v][i])
        dpn[v]=merged
        sz[v]+=sz[nv]

ans=N
for i in range(1,N+1):
    if dpc[0][i]<0 or dpn[0][i]<=10**15:
        ans=i
        break

print(ans-1)