Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int INF10 = 1000000001, ID_MAX = 20;
const long mod = 100000009;
const long long INF18 = 100000000000000001;

struct edge {
  int to;
  vector<int> child;
  long cost;
};

void sort2array(double *a, long *b, long n) {
  for (int i = 0; i < n; i++) {
    b[i] = i;
  }
  sort(b, b + n, [a](long i, long j) -> bool { return a[i] < a[j]; });
}
// bを何回足せばaを超えるか
int wtover(int a, int b) {
  if (a % b > 0)
    return (a / b) + 1;
  else
    return a / b;
}
int bi_e[ID_MAX] = {0};
// 2進数表示したときの最高桁
int bi_max(long n) {
  int m = 0;
  for (m; (1 << m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入
void bi_exs(long n) {
  memset(bi_e, 0, sizeof(bi_e));
  if (n < (1 << ID_MAX)) {
    for (int i = 0; n > 0; n = (n >> 1), i++)
      bi_e[i] = n & 1;
  }
}
// x^n mod m (nが負の時は０)
long myPow(long x, long n, long m) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
struct node {
  int to;
  vector<int> child;
  pair<int, int> weight;
  int color;

  bool leaforn() { return child.empty(); }
  int sizeofchild() { return child.size(); }
};

const int N_MAX = 1001;
int n, x[N_MAX], larger[N_MAX], dp[5001];
node v[N_MAX];

int rec(int m, int k) {
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  for (int i = 0; i < k; i++) {
    for (int j = m - larger[k]; j >= 0; j--) {
      if (dp[j] != 0)
        dp[j + larger[i]] = 1;
    }
  }
  int res = 0;
  rep(i, m + 1) {
    if (dp[i] != 0) {
      res = i;
    }
  }
  return res;
}

int solve() {
  queue<int> que;
  stack<int> dq;
  que.push(0);
  while (que.size()) {
    int w = que.front();
    dq.push(w);
    que.pop();
    rep(i, v[w].child.size()) { que.push(v[w].child.at(i)); }
  }

  while (dq.size()) {
    int w = dq.top();
    dq.pop();
    if (v[w].leaforn()) {
      v[w].weight.first = x[w];
      v[w].weight.second = 0;
      continue;
    }

    int m, sumn = 0, sum = 0;
    memset(larger, 0, sizeof(larger));
    rep(i, v[w].child.size()) {
      larger[i] =
          v[v[w].child.at(i)].weight.first - v[v[w].child.at(i)].weight.second;
      sumn = sumn + v[v[w].child.at(i)].weight.second;
      sum = sum + v[v[w].child.at(i)].weight.first +
            v[v[w].child.at(i)].weight.second;
    }

    if (sumn > x[w]) {
      cout << "IMPOSSIBLE" << endl;
      return 0;
    }
    m = x[w] - sumn;
    int res = rec(m, v[w].child.size());
    v[w].weight.first = max(x[w], sum - res - sumn);
    v[w].weight.second = min(x[w], sum - res - sumn);
  }

  cout << "POSSIBLE" << endl;
  return 0;
}

int main() {
  int i, y;
  // 入力
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> y;
    v[y - 1].child.push_back(i);
    v[i].to = y - 1;
  }
  rep(i, n) { cin >> x[i]; }
  // 処理
  // 出力
  solve();
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int INF10 = 1000000001, ID_MAX = 20;
const long mod = 100000009;
const long long INF18 = 100000000000000001;

struct edge {
  int to;
  vector<int> child;
  long cost;
};

void sort2array(double *a, long *b, long n) {
  for (int i = 0; i < n; i++) {
    b[i] = i;
  }
  sort(b, b + n, [a](long i, long j) -> bool { return a[i] < a[j]; });
}
// bを何回足せばaを超えるか
int wtover(int a, int b) {
  if (a % b > 0)
    return (a / b) + 1;
  else
    return a / b;
}
int bi_e[ID_MAX] = {0};
// 2進数表示したときの最高桁
int bi_max(long n) {
  int m = 0;
  for (m; (1 << m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入
void bi_exs(long n) {
  memset(bi_e, 0, sizeof(bi_e));
  if (n < (1 << ID_MAX)) {
    for (int i = 0; n > 0; n = (n >> 1), i++)
      bi_e[i] = n & 1;
  }
}
// x^n mod m (nが負の時は０)
long myPow(long x, long n, long m) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
struct node {
  int to;
  vector<int> child;
  pair<int, int> weight;
  int color;

  bool leaforn() { return child.empty(); }
  int sizeofchild() { return child.size(); }
};

const int N_MAX = 1001;
int n, x[N_MAX], larger[N_MAX], dp[5001];
node v[N_MAX];

int rec(int m, int k) {
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  for (int i = 0; i < k; i++) {
    for (int j = m - larger[i]; j >= 0; j--) {
      if (dp[j] != 0)
        dp[j + larger[i]] = 1;
    }
  }
  int res = 0;
  rep(i, m + 1) {
    if (dp[i] != 0) {
      res = i;
    }
  }
  return res;
}

int solve() {
  queue<int> que;
  stack<int> dq;
  que.push(0);
  while (que.size()) {
    int w = que.front();
    dq.push(w);
    que.pop();
    rep(i, v[w].child.size()) { que.push(v[w].child.at(i)); }
  }

  while (dq.size()) {
    int w = dq.top();
    dq.pop();
    if (v[w].leaforn()) {
      v[w].weight.first = x[w];
      v[w].weight.second = 0;
      continue;
    }

    int m, sumn = 0, sum = 0;
    memset(larger, 0, sizeof(larger));
    rep(i, v[w].child.size()) {
      larger[i] =
          v[v[w].child.at(i)].weight.first - v[v[w].child.at(i)].weight.second;
      sumn = sumn + v[v[w].child.at(i)].weight.second;
      sum = sum + v[v[w].child.at(i)].weight.first +
            v[v[w].child.at(i)].weight.second;
    }

    if (sumn > x[w]) {
      cout << "IMPOSSIBLE" << endl;
      return 0;
    }
    m = x[w] - sumn;
    int res = rec(m, v[w].child.size());
    v[w].weight.first = max(x[w], sum - res - sumn);
    v[w].weight.second = min(x[w], sum - res - sumn);
  }

  cout << "POSSIBLE" << endl;
  return 0;
}

int main() {
  int i, y;
  // 入力
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> y;
    v[y - 1].child.push_back(i);
    v[i].to = y - 1;
  }
  rep(i, n) { cin >> x[i]; }
  // 処理
  // 出力
  solve();
}