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

    use std::ops::Add;

    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,
            }
        }
    }
}

use inf::*;
use std::cmp::min;

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];
    solve(0, None, &a, &g, &mut tbl);

    let ans = tbl[0]
        .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>>,
    tbl: &mut Vec<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;
        }

        solve(u, Some(v), a, g, tbl);

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

        ret = next;
    }

    tbl[v] = ret;
}


Measured memory_bytes: 826620