Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 1005
#define INF 1000000005LL
#define MOD 1000000007

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

struct tree {
  int p;
  vector<int> c;
  int x, y;
};

int N;
tree T[SIZE];
P R[SIZE];

void dfs(int v, int r) {
  R[v].first = r;
  R[v].second = v;
  for (auto c : T[v].c)
    dfs(c, r + 1);
}

int main() {
  cin >> N;
  rep(i, 2, N + 1) {
    int p;
    cin >> p;
    T[i].p = p;
    T[p].c.push_back(i);
  }
  dfs(1, 0);
  rep(i, 1, N + 1) {
    int x;
    cin >> x;
    T[i].x = x;
  }
  sort(R + 1, R + N + 1, greater<P>());

  bool U = true;
  rep(i, 1, N + 1) {
    vector<int> dp;
    int v = R[i].second;
    if (T[v].c.size() == 0) {
      T[v].y = 0;
      continue;
    }
    int sum = 0;
    dp.push_back(0);
    for (auto c : T[v].c) {
      int X = T[c].x;
      int Y = T[c].y;
      sum += X + Y;
      vector<int> DP;
      for (auto k : dp) {
        if (X + k <= T[v].x)
          DP.push_back(X + k);
        if (Y + k <= T[v].x)
          DP.push_back(Y + k);
      }
      sort(DP.begin(), DP.end());
      dp.erase(unique(DP.begin(), DP.end()), DP.end());
      dp = DP;
    }
    if (dp.empty()) {
      U = false;
      break;
    }
    if (dp[0] > T[v].x) {
      U = false;
      break;
    }
    if (dp[0] == T[v].x) {
      T[v].y = sum - dp[0];
      continue;
    }
    auto it = upper_bound(dp.begin(), dp.end(), T[v].x);
    --it;
    T[v].y = sum - (*it);
  }
  if (U)
    cout << "POSSIBLE" << endl;
  else
    cout << "IMPOSSIBLE" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 1005
#define INF 1000000005LL
#define MOD 1000000007

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

struct tree {
  int p;
  vector<int> c;
  int x, y;
};

int N;
tree T[SIZE];
P R[SIZE];

void dfs(int v, int r) {
  R[v].first = r;
  R[v].second = v;
  for (auto c : T[v].c)
    dfs(c, r + 1);
}

int main() {
  cin >> N;
  rep(i, 2, N + 1) {
    int p;
    cin >> p;
    T[i].p = p;
    T[p].c.push_back(i);
  }
  dfs(1, 0);
  rep(i, 1, N + 1) {
    int x;
    cin >> x;
    T[i].x = x;
  }
  sort(R + 1, R + N + 1, greater<P>());

  bool U = true;
  rep(i, 1, N + 1) {
    vector<int> dp;
    int v = R[i].second;
    if (T[v].c.size() == 0) {
      T[v].y = 0;
      continue;
    }
    int sum = 0;
    dp.push_back(0);
    for (auto c : T[v].c) {
      int X = T[c].x;
      int Y = T[c].y;
      sum += X + Y;
      vector<int> DP;
      for (auto k : dp) {
        if (X + k <= T[v].x)
          DP.push_back(X + k);
        if (Y + k <= T[v].x)
          DP.push_back(Y + k);
      }
      sort(DP.begin(), DP.end());
      DP.erase(unique(DP.begin(), DP.end()), DP.end());
      dp = DP;
    }
    if (dp.empty()) {
      U = false;
      break;
    }
    if (dp[0] > T[v].x) {
      U = false;
      break;
    }
    if (dp[0] == T[v].x) {
      T[v].y = sum - dp[0];
      continue;
    }
    auto it = upper_bound(dp.begin(), dp.end(), T[v].x);
    --it;
    T[v].y = sum - (*it);
  }
  if (U)
    cout << "POSSIBLE" << endl;
  else
    cout << "IMPOSSIBLE" << endl;

  return 0;
}