Problem p03603 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define ri register int
#define il inline
#define fi first
#define se second
#define mp make_pair
#define pi pair<int, int>
#define mem0(x) memset((x), 0, sizeof(x))
#define mem1(x) memset((x), 0x3f, sizeof(x))
il char gc() {
  static const int BS = 1 << 22;
  static unsigned char buf[BS], *st, *ed;
  if (st == ed)
    ed = buf + fread(st = buf, 1, BS, stdin);
  return st == ed ? EOF : *st++;
}
#define gc getchar
template <class T> void in(T &x) {
  x = 0;
  bool f = 0;
  char c = gc();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = 1;
    c = gc();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = gc();
  }
  if (f)
    x = -x;
}
#undef gc
#define pb push_back
// 1743
#define N 1010
#define M 5010
int n;
vector<int> sn[N];
int w[N];
int f[N], g[2][M];
void dfs(int x) {
  for (ri i = 0; i < sn[x].size(); ++i) {
    dfs(sn[x][i]);
  }
  mem1(g);
  int op = 0;
  g[0][0] = 0;
  for (ri i = 0; i < sn[x].size(); ++i) {
    int v = sn[x][i];
    op ^= 1;
    mem1(g[op]);
    for (ri j = 0; j <= w[x]; ++j) {
      if (j - w[v] >= 0)
        g[op][j] = min(g[op][j], g[op ^ 1][j - w[v]] + f[v]);
      if (j - f[v] >= 0)
        g[op][j] = min(g[op][j], g[op ^ 1][j - f[v]] + w[v]);
    }
  }
  for (ri i = 0; i <= w[x]; ++i) {
    f[x] = min(f[x], g[op][i]);
  }
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
#endif
  in(n);
  mem1(f);
  for (ri i = 2, p; i <= n; ++i) {
    in(p);
    sn[p].pb(i);
  }
  for (ri i = 1; i <= n; ++i) {
    in(w[i]);
  }
  dfs(1);
  puts(f[1] < f[0] ? "POSSIBLE" : "IMPOSSIBLE");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define ri register int
#define il inline
#define fi first
#define se second
#define mp make_pair
#define pi pair<int, int>
#define mem0(x) memset((x), 0, sizeof(x))
#define mem1(x) memset((x), 0x3f, sizeof(x))
il char gc() {
  static const int BS = 1 << 22;
  static unsigned char buf[BS], *st, *ed;
  if (st == ed)
    ed = buf + fread(st = buf, 1, BS, stdin);
  return st == ed ? EOF : *st++;
}
#define gc getchar
template <class T> void in(T &x) {
  x = 0;
  bool f = 0;
  char c = gc();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = 1;
    c = gc();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = gc();
  }
  if (f)
    x = -x;
}
#undef gc
#define pb push_back
// 1743
#define N 1010
#define M 5010
int n;
vector<int> sn[N];
int w[N];
int f[N], g[2][M];
void dfs(int x) {
  for (ri i = 0; i < sn[x].size(); ++i) {
    dfs(sn[x][i]);
  }
  mem1(g);
  int op = 0;
  g[0][0] = 0;
  for (ri i = 0; i < sn[x].size(); ++i) {
    int v = sn[x][i];
    op ^= 1;
    mem1(g[op]);
    for (ri j = 0; j <= w[x]; ++j) {
      if (j - w[v] >= 0)
        g[op][j] = min(g[op][j], g[op ^ 1][j - w[v]] + f[v]);
      if (j - f[v] >= 0)
        g[op][j] = min(g[op][j], g[op ^ 1][j - f[v]] + w[v]);
    }
  }
  for (ri i = 0; i <= w[x]; ++i) {
    f[x] = min(f[x], g[op][i]);
  }
}
signed main() {
  in(n);
  mem1(f);
  for (ri i = 2, p; i <= n; ++i) {
    in(p);
    sn[p].pb(i);
  }
  for (ri i = 1; i <= n; ++i) {
    in(w[i]);
  }
  dfs(1);
  puts(f[1] < f[0] ? "POSSIBLE" : "IMPOSSIBLE");
  return 0;
}