#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

// fn min_assign<T: Ord + Clone>(l: &mut T, r: T) {
//     *l = std::cmp::min((*l).clone(), r);
// }

mod inf {
    use std::cmp::min;
    use std::ops::Add;

    #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
    pub enum Inf<T> {
        NotInf(T),
        Inf,
    }

    impl<T> From<T> for Inf<T> {
        fn from(v: T) -> Self {
            Inf::NotInf(v)
        }
    }

    impl<T: Add<Output = T>> Add for Inf<T> {
        type Output = Inf<T>;
        fn add(self, rhs: Self) -> Self {
            use self::Inf::*;
            match (self, rhs) {
                (NotInf(a), NotInf(b)) => NotInf(a + b),
                _ => Inf,
            }
        }
    }

    impl<T: Clone + Ord> Inf<T> {
        pub fn min_assign(&mut self, rhs: Self) {
            *self = min(self.clone(), rhs);
        }
    }
}

use inf::Inf;
use inf::Inf::*;

fn main() {
    input! {
        n: usize,
        a: [i64; n],
        es: [(usize1, usize1); n-1],
    }

    let mut g = vec![vec![]; n];
    for &(u, v) in es.iter() {
        g[u].push(v);
        g[v].push(u);
    }

    let ans = solve(0, None, &a, &g)
        .iter()
        .position(|e| e.0 != Inf || e.1 < 0.into())
        .unwrap();

    println!("{}", ans);
}

fn solve(
    v: usize,
    prev: Option<usize>,
    a: &Vec<i64>,
    g: &Vec<Vec<usize>>,
) -> Vec<(Inf<i64>, Inf<i64>)> {
    let mut ret = vec![(if a[v] < 0 { Inf } else { a[v].into() }, a[v].into())];

    for &u in g[v].iter().filter(|&&u| Some(u) != prev) {
        let utbl = solve(u, Some(v), a, g);

        let mut next = vec![(Inf, Inf); ret.len() + utbl.len()];
        for i in 0..ret.len() {
            for j in 0..utbl.len() {
                if utbl[j].0 != Inf || utbl[j].1 < 0.into() {
                    next[i + j + 1].0.min_assign(ret[i].0);
                    next[i + j + 1].1.min_assign(ret[i].1);
                }
                next[i + j].0.min_assign(ret[i].0 + utbl[j].0);
                next[i + j].1.min_assign(ret[i].1 + utbl[j].1);
            }
        }
        ret = next;
    }

    ret
}


Measured memory_bytes: 7292