Problem:
Score : 700 points

Problem Statement

We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i.

To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight.

Snuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to allocate colors and weights so that the following condition is satisfied for all v.

The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is X_v.

Here, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants.

Determine whether it is possible to allocate colors and weights in this way.

Constraints

1 \leq N \leq 1 000

1 \leq P_i \leq i - 1

0 \leq X_i \leq 5 000

Inputs

Input is given from Standard Input in the following format:

N
P_2 P_3 ... P_N
X_1 X_2 ... X_N

Outputs

If it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3
1 1
4 3 2

Sample Output 1

POSSIBLE

For example, the following allocation satisfies the condition:

Set the color of Vertex 1 to white and its weight to 2.

Set the color of Vertex 2 to black and its weight to 3.

Set the color of Vertex 3 to white and its weight to 2.

There are also other possible allocations.

Sample Input 2

3
1 2
1 2 3

Sample Output 2

IMPOSSIBLE

If the same color is allocated to Vertex 2 and Vertex 3, Vertex 2 cannot be allocated a non-negative weight.

If different colors are allocated to Vertex 2 and 3, no matter which color is allocated to Vertex 1, it cannot be allocated a non-negative weight.

Thus, there exists no allocation of colors and weights that satisfies the condition.

Sample Input 3

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

Sample Output 3

POSSIBLE

Sample Input 4

1

0

Sample Output 4

POSSIBLE

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

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let mut tree = vec![vec![]; n];
    let v = read_vec::<usize>();
    for i in 1..n {
        let (a, b) = (v[i - 1] - 1, i);
        tree[a].push(b);
    }
    let x = read_vec::<usize>();
    let x_end = x.iter().max().unwrap() + 1;

    let mut topological_sorted_indexes = vec![0];
    topological_dfs(0, &tree, &mut topological_sorted_indexes);

    let mut sum_self_and_childs = vec![(0usize, 0usize); n];
    'outer: for &ti in topological_sorted_indexes.iter().rev() {
        let mut dp = vec![false; x_end];
        dp[0] = true;
        for &ci in tree[ti].iter() {
            let mut next = vec![false; x_end];
            for xi in 0..x_end {
                if dp[xi] && xi + sum_self_and_childs[ci].0 < x_end - 1 {
                    next[xi + sum_self_and_childs[ci].0] = true;
                }
                if dp[xi] && xi + sum_self_and_childs[ci].1 < x_end {
                    next[xi + sum_self_and_childs[ci].1] = true;
                }
            }
            dp = next;
        }
        let sum_childs = tree[ti]
            .iter()
            .map(|&x| sum_self_and_childs[x].0 + sum_self_and_childs[x].1)
            .sum::<usize>();

        for xi in (0..x[ti] + 1).rev() {
            if dp[xi] {
                sum_self_and_childs[ti].0 = x[ti];
                sum_self_and_childs[ti].1 = sum_childs - xi;
                continue 'outer;
            }
        }
        println!("IMPOSSIBLE");
        return;
    }
    println!("POSSIBLE");
}

fn topological_dfs(cur_idx: usize, tree: &Vec<Vec<usize>>, result: &mut Vec<usize>) {
    for child_idx in tree[cur_idx].iter() {
        result.push(*child_idx);
        topological_dfs(*child_idx, tree, result);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}