Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9, MOD = 1e9 + 7;
const long long LINF = 1e18;
using namespace std;
#define int long long
#define fin                                                                    \
  {                                                                            \
    cout << "IMPOSSIBLE" << endl;                                              \
    return 0;                                                                  \
  }
// template
bool dp[1000][10002];
// main
signed main() {
  int N;
  cin >> N;
  std::vector<int> parent(N, 0), son(N, 0), x(N);
  for (int i = 1; i < N; i++)
    cin >> parent[i];
  for (int i = 0; i < N; i++)
    parent[i]--;
  for (int i = 0; i < N; i++)
    cin >> x[i];
  for (int i = 0; i < N; i++)
    son[parent[i]]++;
  queue<int> que;
  for (int i = 0; i < N; i++)
    if (son[i] == 0)
      que.push(i);
  typedef pair<int, int> P;
  std::vector<P> v[N];
  while (que.size()) {
    int g = que.front();
    que.pop(); // cout<<g<<" ";
    int si = v[g].size();
    for (int i = 0; i <= si; i++)
      for (int j = 0; j <= 5000; j++)
        dp[i][j] = 0;
    dp[0][0] = 1;
    int sum = 0;
    for (int i = 0; i < si; i++) {
      sum += v[g][i].first + v[g][i].second;
      for (int j = 0; j <= 5000; j++) {
        if (!dp[i][j])
          continue;
        dp[i + 1][j + v[g][i].first] = 1;
        dp[i + 1][j + v[g][i].second] = 1;
      }
    }
    int check = -1;
    for (int i = 0; i <= 5000; i++) {
      if (!dp[si][i])
        continue;
      if (i <= x[g])
        check = max(check, i);
      if (sum - i <= x[g])
        check = max(check, sum - i);
    }
    // cout<<check<<endl;
    if (check == -1)
      fin if (parent[g] == -1) {
        cout << "POSSIBLE" << endl;
        return 0;
      }
    v[parent[g]].push_back(P(x[g], sum - check));
    if (v[parent[g]].size() == son[parent[g]])
      que.push(parent[g]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9, MOD = 1e9 + 7;
const long long LINF = 1e18;
using namespace std;
#define int long long
#define fin                                                                    \
  {                                                                            \
    cout << "IMPOSSIBLE" << endl;                                              \
    return 0;                                                                  \
  }
// template
bool dp[1000][10002];
// main
signed main() {
  int N;
  cin >> N;
  std::vector<int> parent(N, 0), son(N, 0), x(N);
  for (int i = 1; i < N; i++)
    cin >> parent[i];
  for (int i = 0; i < N; i++)
    parent[i]--;
  for (int i = 0; i < N; i++)
    cin >> x[i];
  for (int i = 1; i < N; i++)
    son[parent[i]]++;
  queue<int> que;
  for (int i = 0; i < N; i++)
    if (son[i] == 0)
      que.push(i);
  typedef pair<int, int> P;
  std::vector<P> v[N];
  while (que.size()) {
    int g = que.front();
    que.pop(); // cout<<g<<" ";
    int si = v[g].size();
    for (int i = 0; i <= si; i++)
      for (int j = 0; j <= 5000; j++)
        dp[i][j] = 0;
    dp[0][0] = 1;
    int sum = 0;
    for (int i = 0; i < si; i++) {
      sum += v[g][i].first + v[g][i].second;
      for (int j = 0; j <= 5000; j++) {
        if (!dp[i][j])
          continue;
        dp[i + 1][j + v[g][i].first] = 1;
        dp[i + 1][j + v[g][i].second] = 1;
      }
    }
    int check = -1;
    for (int i = 0; i <= 5000; i++) {
      if (!dp[si][i])
        continue;
      if (i <= x[g])
        check = max(check, i);
      if (sum - i <= x[g])
        check = max(check, sum - i);
    }
    // cout<<check<<endl;
    if (check == -1)
      fin if (parent[g] == -1) {
        cout << "POSSIBLE" << endl;
        return 0;
      }
    v[parent[g]].push_back(P(x[g], sum - check));
    if (v[parent[g]].size() == son[parent[g]])
      que.push(parent[g]);
  }
}