Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;
typedef pair<int, int> P;

int N;
int X[1005];
vector<int> next[1005];

P dfs(int i)
{
	if((int)next[i].size() == 0) return make_pair(0, X[i]);
	
	vector<P> vec;
	for(int j = 0; j < (int)next[i].size(); j++){
		vec.push_back(dfs(next[i][j]));
		if(vec.back().first == -1) return make_pair(-1, -1);
	}
	
	vector<int> dif;
	int min = 0, max = 0;
	for(int j = 0; j < (int)vec.size(); j++){
		min += vec[j].first;
		max += vec[j].second;
		dif.push_back(max - min);
	}
	if(X[i] < min) return make_pair(-1, -1);
	if(X[i] >= max) return make_pair(min, X[i]);
	
	sort(dif.begin(), dif.end());
	
	bool dp[5005];
	for(int j = 0; j <= max; j++) dp[j] = false;
	
	dp[0] = true;
	for(int k = 0; k < (int)dif.size(); k++){
		if(dif[k] > max) break;
		for(int j = max; j >= 0; j--){
			if(dp[j - dif[k]]) dp[j] = true;
		}
	}
	
	
	for(int j = max - X[i]; j <= max; j++){
		if(dp[j]){
			P ret = make_pair(min + j, X[i]);
			if(min + j > X[i]) ret = make_pair(X[i], min + j);
			return ret;
		}
	}
}

int main(void)
{
	cin >> N;
	
	int p;
	for(int i = 2; i <= N; i++){
		cin >> p;
		next[p].push_back(i);
	}
	for(int i = 1; i <= N; i++) cin >> X[i];
	
	P ans = dfs(1);
	if(ans.first == -1) cout << "IMPOSSIBLE" << endl;
	else cout << "POSSIBLE" << endl;
	
	return 0;
}

Compiler output:
a.cc: In function 'P dfs(int)':
a.cc:15:17: error: reference to 'next' is ambiguous
   15 |         if((int)next[i].size() == 0) return make_pair(0, X[i]);
      |                 ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:13: note:                 'std::vector<int> next [1005]'
   11 | vector<int> next[1005];
      |             ^~~~
a.cc:18:33: error: reference to 'next' is ambiguous
   18 |         for(int j = 0; j < (int)next[i].size(); j++){
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:13: note:                 'std::vector<int> next [1005]'
   11 | vector<int> next[1005];
      |             ^~~~
a.cc:19:35: error: reference to 'next' is ambiguous
   19 |                 vec.push_back(dfs(next[i][j]));
      |                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:13: note:                 'std::vector<int> next [1005]'
   11 | vector<int> next[1005];
      |             ^~~~
a.cc: In function 'int main()':
a.cc:63:17: error: reference to 'next' is ambiguous
   63 |                 next[p].push_back(i);
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:13: note:                 'std::vector<int> next [1005]'
   11 | vector<int> next[1005];
      |             ^~~~
a.cc: In function 'P dfs(int)':
a.cc:54:1: warning: control reaches end of non-void function [-Wreturn-type]
   54 | }
      | ^