Problem p03603 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
#define ALL(vec) vec.begin(), vec.end()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define repc(bit, k, n)                                                        \
  for (ll bit = (1 << k) - 1; bit < (1 << n); bit = next_combination(bit))
#define repi(itr, vec) for (auto itr = vec.begin(); itr != vec.end(); ++itr)
using namespace std;
vector<vector<int>> path(1001);
vector<int> X;
int dp[1001][2];
bool checked[1001];
int dp2[1001][1001];
bool checked2[1001][1001];
const int INF = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int solve(const int i, int cur, int s) {
  if (checked2[cur][s])
    return dp2[cur][s];
  if (cur == -1)
    return 0;
  int ans = INF;
  rep(j, 2) {
    if (dp[path[i][cur]][j] <= s) {
      int temp = solve(i, cur - 1, s - dp[path[i][cur]][j]);
      if (temp < 0)
        continue;
      chmin(ans, temp + dp[path[i][cur]][1 - j]);
    } else
      continue;
  }
  if (ans == INF)
    return -1;
  checked2[cur][s] = true;
  return dp2[cur][s] = ans;
}

int check(int i) {
  if (checked[i])
    return 1;
  repi(itr, path[i]) {
    if (check(*itr) == -1)
      return -1;
  }
  fill((bool *)checked2, (bool *)checked2 + sizeof(checked2) / sizeof(bool),
       false);
  int temp = solve(i, path[i].size() - 1, X[i]);
  if (temp < 0)
    return -1;
  dp[i][0] = X[i];
  dp[i][1] = temp;
  checked[i] = true;
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a;
    cin >> a;
    a--;
    path[a].push_back(i + 1);
  }
  rep(i, n) {
    int a;
    cin >> a;
    X.push_back(a);
  }
  if (check(0) == -1)
    cout << "IMPOSSIBLE\n";
  else
    cout << "POSSIBLE\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define ALL(vec) vec.begin(), vec.end()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define repc(bit, k, n)                                                        \
  for (ll bit = (1 << k) - 1; bit < (1 << n); bit = next_combination(bit))
#define repi(itr, vec) for (auto itr = vec.begin(); itr != vec.end(); ++itr)
using namespace std;
vector<vector<int>> path(1001);
vector<int> X;
int dp[1001][2];
bool checked[1001];
int dp2[1001][1001];
bool checked2[1001][1001];
const int INF = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int solve(const int i, int cur, int s) {
  if (checked2[cur][s])
    return dp2[cur][s];
  if (cur == -1)
    return 0;
  int ans = INF;
  rep(j, 2) {
    if (dp[path[i][cur]][j] <= s) {
      int temp = solve(i, cur - 1, s - dp[path[i][cur]][j]);
      if (temp < 0)
        continue;
      chmin(ans, temp + dp[path[i][cur]][1 - j]);
    } else
      continue;
  }
  if (ans == INF)
    ans = -1;
  checked2[cur][s] = true;
  return dp2[cur][s] = ans;
}

int check(int i) {
  if (checked[i])
    return 1;
  repi(itr, path[i]) {
    if (check(*itr) == -1)
      return -1;
  }
  fill((bool *)checked2, (bool *)checked2 + sizeof(checked2) / sizeof(bool),
       false);
  int temp = solve(i, path[i].size() - 1, X[i]);
  if (temp < 0)
    return -1;
  dp[i][0] = X[i];
  dp[i][1] = temp;
  checked[i] = true;
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a;
    cin >> a;
    a--;
    path[a].push_back(i + 1);
  }
  rep(i, n) {
    int a;
    cin >> a;
    X.push_back(a);
  }
  if (check(0) == -1)
    cout << "IMPOSSIBLE\n";
  else
    cout << "POSSIBLE\n";

  return 0;
}