Language: C++

#include<bits/stdc++.h>
#include<queue>
using namespace std;
void cha(int &a, int b) {
  if (b == -1) return;
  if (a == -1 || a > b) {
    a = b;
  }
}
int main () {
  int N;
  cin >> N;
  vector<vector<int>>son(N + 1, vector<int>(N + 1));
  vector<int>si(N + 1, 0);
  for (int i = 2; i <= N; i ++) {
    int a;
    cin >> a;
    son[a][si[a]] = i;
    si[a] ++;
  }
  vector<int>X(N + 1);
  for (int i = 1; i <= N; i ++) cin >> X[i];
  vector<int>per;
  queue<int>que;
  que.push(1);
  per.push_back(1);
  while(!que.empty()) {
    int u = que.front();
    for (int i = 0; i < si[u]; i ++) {
      int ne = son[u][i];
      per.push_back(ne);
      que.push(ne);
    }
    que.pop();
  }
  int dp[1010];
  for (int i = N - 1; i >= 0; i --) {
    int nw = per[i];
    if (!si[nw]) dp[nw] = 0;
    else {
      int *dp_ = new int[si[nw] + 1][5050];
      for (int i_ = 0; i_ >= si[nw]; i_ ++) {
        for (int j = 0; j < 5050; j ++) {
          dp_[i][j] = -1;
        }
      }
      dp_[0][0] = 0;
      for (int i_ = 1; i_ <= si[nw]; i_ ++) {
        int so = son[nw][i_];
        for (int j = X[nw]; j >= 0; j --) {
          if (j + X[so] <= X[nw] && dp_[i - 1][j] != -1) {
            cha(dp_[i][j + X[so]], dp_[i - 1][j] + dp[so]);
          }
          if (j + dp[so] <= X[nw] && dp_[i - 1][j] != -1) {
            cha(dp_[i][j + dp[so]], dp_[i - 1][j] + X[so]);
          }
        }
      }
      dp[nw] = -1;
      for (int i_ = 1; i_ <= X[nw]; i_ ++) {
        cha(dp[nw], dp_[si[nw]][i]);
      }
      if (dp[nw] == -1) {
        cout << "IMPOSSIBLE" << endl;
        return 0;
      }
      delete[] dp_;
    }
  }
  cout << "POSSIBLE" << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:41:42: error: cannot convert 'int (*)[5050]' to 'int*' in initialization
   41 |       int *dp_ = new int[si[nw] + 1][5050];
      |                                          ^
      |                                          |
      |                                          int (*)[5050]
a.cc:44:17: error: invalid types 'int[int]' for array subscript
   44 |           dp_[i][j] = -1;
      |                 ^
a.cc:47:13: error: invalid types 'int[int]' for array subscript
   47 |       dp_[0][0] = 0;
      |             ^
a.cc:51:47: error: invalid types 'int[int]' for array subscript
   51 |           if (j + X[so] <= X[nw] && dp_[i - 1][j] != -1) {
      |                                               ^
a.cc:52:23: error: invalid types 'int[int]' for array subscript
   52 |             cha(dp_[i][j + X[so]], dp_[i - 1][j] + dp[so]);
      |                       ^
a.cc:52:46: error: invalid types 'int[int]' for array subscript
   52 |             cha(dp_[i][j + X[so]], dp_[i - 1][j] + dp[so]);
      |                                              ^
a.cc:54:48: error: invalid types 'int[int]' for array subscript
   54 |           if (j + dp[so] <= X[nw] && dp_[i - 1][j] != -1) {
      |                                                ^
a.cc:55:23: error: invalid types 'int[int]' for array subscript
   55 |             cha(dp_[i][j + dp[so]], dp_[i - 1][j] + X[so]);
      |                       ^
a.cc:55:47: error: invalid types 'int[int]' for array subscript
   55 |             cha(dp_[i][j + dp[so]], dp_[i - 1][j] + X[so]);
      |                                               ^
a.cc:61:32: error: invalid types 'int[int]' for array subscript
   61 |         cha(dp[nw], dp_[si[nw]][i]);
      |                                ^