Problem p03603 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define CL fclose(stdin), fclose(stdout)
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
long long G() {
  long long n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
} // namespace io
int f[1111];
int g[2][5005];
int fa[1111];
int x[1111];
struct edge {
  int to;
  int next;
} e[2222];
int pe = 1111;
void insert(int a, int to) {
  e[pe] = (edge){to, e[a].next};
  e[a].next = pe++;
}
int main() {
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
  int n = io::F();
  for (register int i = 2; i <= n; ++i)
    fa[i] = io::F(), insert(fa[i], i);
  for (register int i = 1; i <= n; ++i)
    x[i] = io::F();
  for (register int i = n; i; --i) {
    memset(g, 63, sizeof(g));
    g[0][0] = 0;
    int *X = g[0], *Y = g[1];
    for (register int p = e[i].next; p;
         p = e[p].next, std::swap(X, Y), memset(Y, 63, sizeof(g[0])))
      for (register int j = 0; j <= 5000; ++j) {
        if (j >= x[e[p].to])
          cmin(Y[j], X[j - x[e[p].to]] + f[e[p].to]);
        if (j >= f[e[p].to])
          cmin(Y[j], X[j - f[e[p].to]] + x[e[p].to]);
      }
    f[i] = 0x3f3f3f3f;
    for (register int j = 0; j <= x[i]; ++j)
      cmin(f[i], X[j]);
    if (f[i] == 0x3f3f3f3f) {
      puts("IMPOSSIBLE");
      return 0;
    }
  }
  puts("POSSIBLE");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define CL fclose(stdin), fclose(stdout)
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
long long G() {
  long long n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
} // namespace io
int f[1111];
int g[2][5005];
int fa[1111];
int x[1111];
struct edge {
  int to;
  int next;
} e[2222];
int pe = 1111;
void insert(int a, int to) {
  e[pe] = (edge){to, e[a].next};
  e[a].next = pe++;
}
int main() {
  // freopen("E.in","r",stdin);
  // freopen("E.out","w",stdout);
  int n = io::F();
  for (register int i = 2; i <= n; ++i)
    fa[i] = io::F(), insert(fa[i], i);
  for (register int i = 1; i <= n; ++i)
    x[i] = io::F();
  for (register int i = n; i; --i) {
    memset(g, 63, sizeof(g));
    g[0][0] = 0;
    int *X = g[0], *Y = g[1];
    for (register int p = e[i].next; p;
         p = e[p].next, std::swap(X, Y), memset(Y, 63, sizeof(g[0])))
      for (register int j = 0; j <= 5000; ++j) {
        if (j >= x[e[p].to])
          cmin(Y[j], X[j - x[e[p].to]] + f[e[p].to]);
        if (j >= f[e[p].to])
          cmin(Y[j], X[j - f[e[p].to]] + x[e[p].to]);
      }
    f[i] = 0x3f3f3f3f;
    for (register int j = 0; j <= x[i]; ++j)
      cmin(f[i], X[j]);
    if (f[i] == 0x3f3f3f3f) {
      puts("IMPOSSIBLE");
      return 0;
    }
  }
  puts("POSSIBLE");
  return 0;
}