Problem:
Score : 700 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Currently, there are A_i stones placed on vertex i.
Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:

Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices.
Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.

Note that the operation cannot be performed if there is a vertex with no stone on the path.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i,b_i ≦ N

0 ≦ A_i ≦ 10^9

The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.

Sample Input 1

5
1 2 1 1 2
2 4
5 2
3 2
1 3

Sample Output 1

YES

All the stones can be removed, as follows:

Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.

Select vertices 1 and 5. Then, there is no stone on any vertex.

Sample Input 2

3
1 2 1
1 2
2 3

Sample Output 2

NO

Sample Input 3

6
3 2 2 2 2 2
1 2
2 3
1 4
1 5
4 6

Sample Output 3

YES

Solution in Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/agc010/tasks/agc010_c
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

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

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

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

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

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

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

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

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

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===


type Graph = Vec<Vec<usize>>;

fn dfs_cpo(g: &Graph, root: usize) -> (Vec<usize>, Vec<Option<usize>>, Vec<usize>) {
    let n = g.len();
    struct Cpo {
        count: Vec<usize>,
        parent: Vec<Option<usize>>,
        order: Vec<usize>,
        oidx: usize
    };
    let mut cpo = Cpo {
        count: vec![0; n],
        parent: vec![None; n],
        order: vec![0; n],
        oidx: 0
    };

    fn dfs(g: &Graph, now: usize, par: Option<usize>, cpo: &mut Cpo) {
        for &to in &g[now] {
            if Some(to) != par {
                dfs(g, to, Some(now), cpo);
            }
        }
        cpo.count[now] = 1;
        for &to in &g[now] {
            if Some(to) != par {
                cpo.count[now] += cpo.count[to];
            }
        }
        cpo.parent[now] = par;
        cpo.order[cpo.oidx] = now;
        cpo.oidx += 1;

    }
    dfs(&g, root, None, &mut cpo);
    (cpo.count, cpo.parent, cpo.order)
}
//===

fn solve(g: Vec<Vec<usize>>, a: Vec<i64>) -> bool {
    let n = g.len();
    if n == 2 {
        return a[0] == a[1];
    }

    let mut root = 0;
    for i in 0..n {
        if g[i].len() >= 2 {
            root = i;
            break;
        }
    }
    let (count, parent, order) = dfs_cpo(&g, root);


    let mut isvalid = true;
    let mut up = vec![0; n];
    for i in 0..n {
        let v = order[i];
        let mut total = 0;
        let mut upmax = 0;
        let mut cn = 0;
        for &to in &g[v] {
            if Some(to) == parent[v] {
                continue;
            }
            total += up[to];
            upmax = max(upmax, up[to]);
            cn += 1;
        }
        if cn == 0 {
            up[v] = a[v];
        } else {
            let maxtie = min(total-upmax, total/2);
            let needtie = total-a[v];
            if needtie < 0 || needtie > maxtie {
                return false;
            } else {
                up[v] = total - needtie * 2;
                if v == root {
                    isvalid &= up[v] == 0;
                }
            }
        }
    }
    isvalid
}

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


    let mut g = vec![vec![]; n];
    for (u, v) in edges {
        g[u].push(v);
        g[v].push(u);
    }
    println!("{}", ifv!(solve(g, a), "YES", "NO"));
}