Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

struct edge {
  ll to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void addedge(int st, int ed, ll co) {
  edge e;
  e.to = ed;
  e.cost = co;
  G[st].push_back(e);
}

ll P[1010];
ll X[1010];

bool ok[1010];
ll val[1010];

ll dp[2][5010];

void dfs(ll v) {
  ll len = G[v].size();
  REP(i, 0, len) {
    edge e = G[v][i];
    // if (e.to == P[v]) {
    //     continue;
    // }
    dfs(e.to);
  }
  if (len == 0) {
    val[v] = 0;
  } else {
    fill(dp[0], dp[0] + 5010, inf);
    edge e0 = G[v][0];
    dp[0][X[e0.to]] = val[e0.to];
    dp[0][val[e0.to]] = min(dp[0][val[e0.to]], X[e0.to]);
    REP(i, 1, len) {
      fill(dp[1], dp[1] + 5010, inf);
      edge e = G[v][i];
      REP(j, 0, X[v] + 1) {
        if (dp[0][j] == inf) {
          continue;
        }
        if (j + X[e.to] <= X[v]) {
          dp[1][j + X[e.to]] = min(dp[1][j + X[e.to]], dp[0][j] + val[e.to]);
        }
        if (j + val[e.to] <= X[v]) {
          dp[1][j + val[e.to]] = min(dp[1][j + val[e.to]], dp[0][j] + X[e.to]);
        }
      }
      swap(dp[0], dp[1]);
    }
    ll minval = inf;
    // cout << v << endl;
    // REP(i, 0, X[v] + 1) {
    //     cout << dp[0][i] << " ";
    // }
    // cout << endl;
    REP(i, 0, X[v] + 1) {
      minval = min(minval, dp[0][i]);
      // cout << minval << " ";
    }
    // cout << endl;
    if (minval == inf) {
      ok[v] = false;
      val[v] = inf;
    } else {
      val[v] = minval;
    }
    // cout << val[v] << " aaa" << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  P[0] = -1;
  REP(i, 1, N) {
    cin >> P[i];
    // addedge(i, P[i] - 1, 1);
    addedge(P[i] - 1, i, 1);
  }
  REP(i, 0, N) { cin >> X[i]; }
  fill(ok, ok + N, true);
  dfs(0);
  bool allok = true;
  REP(i, 0, N) {
    if (!ok[i]) {
      allok = false;
    }
  }
  // REP(i, 0, N) {
  //     cout << val[i] << " ";
  // }
  // cout << endl;
  if (allok) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

struct edge {
  ll to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void addedge(int st, int ed, ll co) {
  edge e;
  e.to = ed;
  e.cost = co;
  G[st].push_back(e);
}

ll P[1010];
ll X[1010];

bool ok[1010];
ll val[1010];

ll dp[2][5010];

void dfs(ll v) {
  ll len = G[v].size();
  REP(i, 0, len) {
    edge e = G[v][i];
    // if (e.to == P[v]) {
    //     continue;
    // }
    dfs(e.to);
  }
  if (len == 0) {
    val[v] = 0;
  } else {
    fill(dp[0], dp[0] + 5010, inf);
    edge e0 = G[v][0];
    dp[0][X[e0.to]] = val[e0.to];
    if (val[e0.to] <= 5000) {
      dp[0][val[e0.to]] = min(dp[0][val[e0.to]], X[e0.to]);
    }
    REP(i, 1, len) {
      fill(dp[1], dp[1] + 5010, inf);
      edge e = G[v][i];
      REP(j, 0, X[v] + 1) {
        if (dp[0][j] == inf) {
          continue;
        }
        if (j + X[e.to] <= X[v]) {
          dp[1][j + X[e.to]] = min(dp[1][j + X[e.to]], dp[0][j] + val[e.to]);
        }
        if (j + val[e.to] <= X[v]) {
          dp[1][j + val[e.to]] = min(dp[1][j + val[e.to]], dp[0][j] + X[e.to]);
        }
      }
      swap(dp[0], dp[1]);
    }
    ll minval = inf;
    // cout << v << endl;
    // REP(i, 0, X[v] + 1) {
    //     cout << dp[0][i] << " ";
    // }
    // cout << endl;
    REP(i, 0, X[v] + 1) {
      minval = min(minval, dp[0][i]);
      // cout << minval << " ";
    }
    // cout << endl;
    if (minval == inf) {
      ok[v] = false;
      val[v] = inf;
    } else {
      val[v] = minval;
    }
    // cout << val[v] << " aaa" << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  P[0] = -1;
  REP(i, 1, N) {
    cin >> P[i];
    // addedge(i, P[i] - 1, 1);
    addedge(P[i] - 1, i, 1);
  }
  REP(i, 0, N) { cin >> X[i]; }
  fill(ok, ok + N, true);
  dfs(0);
  bool allok = true;
  REP(i, 0, N) {
    if (!ok[i]) {
      allok = false;
    }
  }
  // REP(i, 0, N) {
  //     cout << val[i] << " ";
  // }
  // cout << endl;
  if (allok) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
}