Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define int long long
#define INF 1000000000000000
using namespace std;

typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

Pii devide(vector<int> &B, vector<int> &W, int K) {
  int N = B.size();
  vector<vector<bool>> dp(N + 1, vector<bool>(K + 1, false));
  dp[0][0] = true;
  REP(i, N) {
    REP(k, K + 1) {
      if (dp[i][k]) {
        if (k + B[k] <= K)
          dp[i + 1][k + B[k]] = true;
        if (k + W[k] <= K)
          dp[i + 1][k + W[k]] = true;
      }
    }
  }
  int x1 = -1, x2 = 0;
  IREP(i, K + 1) {
    if (dp[N][i]) {
      x1 = i;
      break;
    }
  }
  REP(i, N) x2 += B[i] + W[i];

  return Pii(x1, x2 - x1);
}

signed main() {

  int N;
  cin >> N;
  vector<vector<int>> ko(N, vector<int>(0)), depth(N, vector<int>(0));
  int P, maxdepth = 1;
  FOR(i, 1, N) {
    cin >> P;
    P--;
    ko[P].push_back(i);
  }
  depth[0].push_back(0);
  REP(i, N) {
    if (depth[i].size() == 0)
      break;
    maxdepth++;
    REP(j, depth[i].size()) {
      REP(k, ko[depth[i][j]].size()) {
        depth[i + 1].push_back(ko[depth[i][j]][k]);
      }
    }
  }

  vector<int> X(N);
  readvec(X);

  vector<int> B(N), W(N, 0);
  REP(i, N) B[i] = X[i];

  IREP(i, maxdepth - 1) {
    REP(j, depth[i].size()) {
      int l = depth[i][j];
      if (ko[l].size() == 0)
        continue;
      int n = ko[l].size();
      vector<int> b(n), w(n);
      REP(k, n) { b[k] = B[ko[l][k]], w[k] = W[ko[l][k]]; }
      Pii res = devide(b, w, X[l]);
      if (res.first == -1) {
        cout << "IMPOSSIBLE";
        return 0;
      } else {
        B[l] = X[l];
        W[l] = res.second;
      }
    }
  }
  cout << "POSSIBLE";

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define int long long
#define INF 1000000000000000
using namespace std;

typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

Pii devide(vector<int> &B, vector<int> &W, int K) {
  int N = B.size();
  vector<vector<bool>> dp(N + 1, vector<bool>(K + 1, false));
  dp[0][0] = true;
  REP(i, N) {
    REP(k, K + 1) {
      if (dp[i][k]) {
        if (k + B[i] <= K)
          dp[i + 1][k + B[i]] = true;
        if (k + W[i] <= K)
          dp[i + 1][k + W[i]] = true;
      }
    }
  }
  int x1 = -1, x2 = 0;
  IREP(i, K + 1) {
    if (dp[N][i]) {
      x1 = i;
      break;
    }
  }
  REP(i, N) x2 += B[i] + W[i];

  return Pii(x1, x2 - x1);
}

signed main() {

  int N;
  cin >> N;
  vector<vector<int>> ko(N, vector<int>(0)), depth(N, vector<int>(0));
  int P, maxdepth = 1;
  FOR(i, 1, N) {
    cin >> P;
    P--;
    ko[P].push_back(i);
  }
  depth[0].push_back(0);
  REP(i, N) {
    if (depth[i].size() == 0)
      break;
    maxdepth++;
    REP(j, depth[i].size()) {
      REP(k, ko[depth[i][j]].size()) {
        depth[i + 1].push_back(ko[depth[i][j]][k]);
      }
    }
  }

  vector<int> X(N);
  readvec(X);

  vector<int> B(N), W(N, 0);
  REP(i, N) B[i] = X[i];

  IREP(i, maxdepth - 1) {
    REP(j, depth[i].size()) {
      int l = depth[i][j];
      if (ko[l].size() == 0)
        continue;
      int n = ko[l].size();
      vector<int> b(n), w(n);
      REP(k, n) { b[k] = B[ko[l][k]], w[k] = W[ko[l][k]]; }
      Pii res = devide(b, w, X[l]);
      if (res.first == -1) {
        cout << "IMPOSSIBLE";
        return 0;
      } else {
        B[l] = X[l];
        W[l] = res.second;
      }
    }
  }
  cout << "POSSIBLE";

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}