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

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $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 main() {
    input! {
        n: usize,
        s: chars,
        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 ans = None;

    for i in 0..n {
        let cur = solve(i, None, &g, &s);
        if cur.0 == 0 {
            if ans == None || ans > Some(cur.1/2) {
                ans = Some(cur.1/2);
            }
        }
    }

    println!("{}", if let Some(ans) = ans {ans} else {-1});
}

use std::cmp::min;

fn solve(u: usize, prev: Option<usize>, g: &Vec<Vec<usize>>, s: &[char]) -> (i64, i64, i64) {
    let mut cs = vec![];
    for &v in g[u].iter() {
        if Some(v) != prev {
            let (a, b, c) = solve(v, Some(u), g, s);
            cs.push((a+c, b+c, c));
        }
    }

    let total = cs.iter().map(|&c| c.2).sum::<i64>() + if s[u] == '1' {1} else {0};
    let maxv = cs.iter().map(|&c| c.1).sum::<i64>();
    let t = cs.iter().map(|&c| c.0).sum::<i64>();

    let minv = if cs.len() >= 2 {
        let mut ng = None;
        for i in 0..cs.len() {
            if cs[i].0*2 > t {
                ng = Some(i);
                break;
            }
        }

        if let Some(i) = ng {
            let mut rest = cs[i].0 - (t - cs[i].0);
            for j in 0..cs.len() {
                if i == j {
                    continue;
                }
                let usable = min(rest, cs[j].1-cs[j].0)/2*2;
                rest -= usable;
            }
            rest
        } else if t % 2 != 0 {
            1
        } else {
            0
        }
    } else {
        t
    };

    (minv, maxv, total)
}



Measured memory_bytes: 4604