#[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")
    };
}

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);
        }
    }
}

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

use 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 mut tbl = vec![vec![]; n + 1];
    let ans = solve(0, None, &a, &g)
        .iter()
        .enumerate()
        .find(|&(_, e)| e.0 != Inf::Inf || e.1 < 0.into())
        .unwrap()
        .0;

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

fn solve(
    v: usize,
    prev: Option<usize>,
    a: &Vec<i64>,
    g: &Vec<Vec<usize>>,
) -> Vec<(Inf<i64>, Inf<i64>)> {
    use Inf::*;

    let mut ret = vec![if a[v] < 0 {
        (Inf, a[v].into())
    } else {
        (a[v].into(), a[v].into())
    }];

    for &u in g[v].iter() {
        if Some(u) == prev {
            continue;
        }

        let utbl = solve(u, Some(v), a, g);

        let mut next = vec![(Inf, Inf); ret.len() + utbl.len() + 1];
        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