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

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 mut ans = INF;
    for i in 0..n + 1 {
        if tbl[0][i].0 != INF || tbl[0][i].1 < 0 {
            ans = i as i64;
            break;
        }
    }

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

const INF: i64 = 1_000_000_000_000_000;

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

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

                if ret[i].0 != INF && tbl[u][j].0 != INF {
                    next[i + j].0 = min(next[i + j].0, ret[i].0 + tbl[u][j].0);
                }
                if ret[i].1 != INF && tbl[u][j].1 != INF {
                    next[i + j].1 = min(next[i + j].1, ret[i].1 + tbl[u][j].1);
                }
            }
        }

        ret = next;
    }

    tbl[v] = ret;
}


Measured memory_bytes: 390012