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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <stdlib.h>
#include <queue>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
//#include <assert.h>
//#include <sstream>
using namespace std;

#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll> 
#define PI acos(-1)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
#define forn(i, j, n) for(int i = j; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef tahsin

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif

ll add(ll a, ll b, ll m = mod) {
	ll ret = a+b;
	if(ret >= m) ret -= m;
	return ret;
}

ll subtract(ll a, ll b, ll m = mod) {
	ll ret = a-b;
	if(ret < 0) ret += m;
	return ret;
}

ll mult(ll a, ll b, ll m = mod) {
	ll ret = a*b;
	if(ret >= m) ret %= m;
	return ret;
}

ll bigmod(ll a, ll b, ll m = mod) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a, m);
		b >>= 1; a = mult(a, a, m);
	}
	return ret;
}

ll inverse(ll n, ll m = mod) { return bigmod(n, mod-2, m); }

/*
bool base[1000010];
vi primes;
void sieve(int mx) {
	mx += 10;
	int x = sqrt(mx);
	for(int i = 3; i <= x; i += 2) if(base[i] == 0) for(int j = i*i, k = i<<1; j < mx; j += k) base[j] = 1;
	primes.PB(2);
	for(int i = 3; i < mx; i += 2) if(base[i] == 0) primes.PB(i);
}
*/


//Direction Array 
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template


#define mxn 5010
int poss, X[mxn], cases, vis[mxn][mxn], dp[mxn][2];
vi e[mxn];

int f(int u, int i, int tot) {
	if(poss == 0) return 0;
	if(i == -1) return 1;

	int &ret = dp[i][tot];
	if(vis[i][tot] == cases) return ret;
	vis[i][tot] = cases;

	int v = e[u][i];

	ret = 0;
	if(tot >= dp[v][0] && f(u, i-1, tot-dp[v][0])) return ret = 1;
	if(tot >= dp[v][1] && f(u, i-1, tot-dp[v][1])) return ret = 1;
	return ret = 0;
}


void bt (int u, int i, int tot) {
	if(poss == 0) return;
	if(i == -1) {
		dp[u][0] += tot;
		return;
	}


	int v = e[u][i];

	dbg(u, i, v);
	if(tot >= dp[v][0] && f(u, i-1, tot-dp[v][0])) {
		dbg(1);
		dp[u][0] += dp[v][0];
		dp[u][1] += dp[v][1];
		bt(u, i-1, tot-dp[v][0]);
		return;
	}
	if(tot >= dp[v][1] && f(u, i-1, tot-dp[v][1])) {
		dbg(2);
		swap(dp[v][0], dp[v][1]);
		dp[u][0] += dp[v][0];
		dp[u][1] += dp[v][1];
		dbg(dp[u][0], dp[u][1]);
		bt(u, i-1, tot-dp[v][0]);
	}
}

void dfs(int u) {
	dp[u][0] = dp[u][1] = 0;
	if(poss == 0) return;


	if(e[u].size() == 0) {
		dp[u][0] = X[u], dp[u][1] = 0;
		return;
	}
//	int sum = 0;
	for(auto v : e[u]) {
		dfs(v);
//		sum += dp[v][0] + dp[v][1];
//		dbg(u, v, dp[v][0], dp[v][1]);
	}

	++cases;
	int ret = f(u, e[u].size()-1, X[u]);

	if(ret) {
		dbg(u, "backtrack");
		bt(u, e[u].size()-1, X[u]);
		dbg(u, dp[u][0], dp[u][1]);
		for(auto v: e[u]) dbg(v, dp[v][0], dp[v][1]);
//		dp[u][0] = X[u];
//		dp[u][1] = sum - X[u];
	} else {
		poss = 0;
		return;
	}
}

int main () {
#ifdef tahsin
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int n, x;

	while(si(n) == 1) {
		for(int i = 1; i < n; ++i) {
			si(x);
			--x;
			e[x].PB(i);
		}
		rep(i, n) si(X[i]);

		poss = 1;
		dfs(0);

		if(poss == 0) puts("IMPOSSIBLE");
		else puts("POSSIBLE");

		rep(i, n) e[i].clear();
	}

//	timeStamp;
	return 0;
}