macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{chars :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Adjacent {
    pub to: usize,
    pub id: usize,
}
impl Adjacent {
    pub fn new(to: usize, id: usize) -> Adjacent {
        Adjacent { to: to, id: id }
    }
}
#[derive(Clone, Debug, Default)]
pub struct Graph {
    pub vsize: usize,
    pub esize: usize,
    pub graph: Vec<Vec<Adjacent>>,
}
impl Graph {
    pub fn new(vsize: usize) -> Graph {
        Graph {
            vsize: vsize,
            esize: 0,
            graph: vec![vec![]; vsize],
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        self.graph[from].push(Adjacent::new(to, self.esize));
        self.esize += 1;
    }
    pub fn add_undirected_edge(&mut self, u: usize, v: usize) {
        self.graph[u].push(Adjacent::new(v, self.esize));
        self.graph[v].push(Adjacent::new(u, self.esize));
        self.esize += 1;
    }
    pub fn vertices(&self) -> std::ops::Range<usize> {
        0..self.vsize
    }
    pub fn adjacency(&self, from: usize) -> &Vec<Adjacent> {
        &self.graph[from]
    }
}
impl Graph {
    fn size_dfs(&self, u: usize, p: usize, size: &mut Vec<u64>) {
        size[u] = 1;
        for a in self.adjacency(u) {
            if a.to != p {
                self.size_dfs(a.to, u, size);
                size[u] += size[a.to];
            }
        }
    }
    pub fn tree_size(&self, root: usize) -> Vec<u64> {
        let n = self.vsize;
        let mut size = vec![0; n];
        self.size_dfs(root, n, &mut size);
        size
    }
}
#[derive(Debug)]
pub struct TreeRec {
    n: usize,
    a: Vec<i64>,
}
impl TreeRec {
    pub fn new(n: usize, a: Vec<i64>) -> TreeRec {
        TreeRec { n: n, a: a }
    }
    pub fn dfs(&mut self, u: usize, p: usize, graph: &Graph) -> (Vec<i64>, Option<usize>) {
        let mut dp = vec![self.a[u]];
        let mut z = 0;
        for a in graph.adjacency(u) {
            if a.to != p {
                let (v, opt) = self.dfs(a.to, u, graph);
                let mut ep = vec![std::i64::MAX; dp.len() + v.len()];
                for i in 0..dp.len() {
                    for j in 0..v.len() {
                        if v[j] < 0 || opt.map(|x| x == j).unwrap_or(false) {
                            ep[i + j + 1] = min(ep[i + j + 1], dp[i]);
                        }
                        ep[i + j] = min(ep[i + j], dp[i].saturating_add(v[j]));
                    }
                }
                let mut y = find_minus(&v) + 1;
                if let Some(x) = opt {
                    y = min(y, x);
                }
                z += y;
                dp = ep;
            }
        }
        let opt = if self.a[u] > 0 { Some(z) } else { None };
        (dp, opt)
    }
}
pub fn find_minus(v: &[i64]) -> usize {
    for i in 0..v.len() {
        if v[i] < 0 {
            return i;
        }
    }
    return v.len();
}
use std::cmp::min;
fn main() {
    input! { n, a:[i64;n], uv:[(usize1,usize1);n-1] };
    let mut g = Graph::new(n);
    for &(u, v) in &uv {
        g.add_undirected_edge(u, v);
    }
    let mut r = TreeRec::new(n, a);
    let (v, opt) = r.dfs(0, n, &g);
    let mut ans = find_minus(&v);
    if let Some(x) = opt {
        ans = min(ans, x);
    }
    println!("{}", ans);
}


Measured memory_bytes: 7420