Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 2000
#define INF 999999999
int n, W[maxn], f[2][2][maxn], g[maxn][2];

int read() {
  int x = 0, k = 1;
  char c;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * k;
}

struct edge {
  int cnp, to[maxn], last[maxn], head[maxn];
  edge() { cnp = 2; }
  void add(int u, int v) { to[cnp] = v, last[cnp] = head[u], head[u] = cnp++; }
} E1;

bool Down(int &x, int y) {
  if (y >= INF)
    return 0;
  x = min(x, y);
  return 1;
}

void dfs(int u) {
  for (int i = E1.head[u]; i; i = E1.last[i]) {
    int v = E1.to[i];
    dfs(v);
  }
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j <= W[u]; j++)
      f[0][i][j] = INF;
  f[0][0][0] = f[0][1][0] = 0;
  int pre = 0, now = 1;
  for (int i = E1.head[u]; i; i = E1.last[i]) {
    int v = E1.to[i];
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= W[u]; k++)
        f[now][j][k] = INF;
    for (int k = 0; k <= 1; k++)
      for (int j = 0; j <= W[u]; j++) {
        int flag = 0;
        if (j - W[v] >= 0)
          flag = Down(f[now][k][j], f[pre][k][j - W[v]] + g[v][k]);
        if (j - g[v][k ^ 1] >= 0)
          flag = Down(f[now][k][j], f[pre][k][j - g[v][k ^ 1]] + W[v]);
      }
    swap(pre, now);
  }
  g[u][0] = g[u][1] = INF;
  for (int k = 0; k <= 1; k++)
    for (int j = 0; j <= W[u]; j++)
      g[u][k] = min(g[u][k], f[pre][k][j]);
}

int main() {
  n = read();
  for (int i = 2; i <= n; i++) {
    int x = read();
    E1.add(x, i);
  }
  for (int i = 1; i <= n; i++)
    W[i] = read();
  dfs(1);
  if (g[1][0] < INF || g[1][1] < INF)
    printf("POSSIBLE\n");
  else
    printf("IMPOSSIBLE\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 6000
#define INF 999999999
int n, W[maxn], f[2][2][maxn], g[maxn][2];

int read() {
  int x = 0, k = 1;
  char c;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * k;
}

struct edge {
  int cnp, to[maxn], last[maxn], head[maxn];
  edge() { cnp = 2; }
  void add(int u, int v) { to[cnp] = v, last[cnp] = head[u], head[u] = cnp++; }
} E1;

bool Down(int &x, int y) {
  if (y >= INF)
    return 0;
  x = min(x, y);
  return 1;
}

void dfs(int u) {
  for (int i = E1.head[u]; i; i = E1.last[i]) {
    int v = E1.to[i];
    dfs(v);
  }
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j <= W[u]; j++)
      f[0][i][j] = INF;
  f[0][0][0] = f[0][1][0] = 0;
  int pre = 0, now = 1;
  for (int i = E1.head[u]; i; i = E1.last[i]) {
    int v = E1.to[i];
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= W[u]; k++)
        f[now][j][k] = INF;
    for (int k = 0; k <= 1; k++)
      for (int j = 0; j <= W[u]; j++) {
        int flag = 0;
        if (j - W[v] >= 0)
          flag = Down(f[now][k][j], f[pre][k][j - W[v]] + g[v][k]);
        if (j - g[v][k ^ 1] >= 0)
          flag = Down(f[now][k][j], f[pre][k][j - g[v][k ^ 1]] + W[v]);
      }
    swap(pre, now);
  }
  g[u][0] = g[u][1] = INF;
  for (int k = 0; k <= 1; k++)
    for (int j = 0; j <= W[u]; j++)
      g[u][k] = min(g[u][k], f[pre][k][j]);
}

int main() {
  n = read();
  for (int i = 2; i <= n; i++) {
    int x = read();
    E1.add(x, i);
  }
  for (int i = 1; i <= n; i++)
    W[i] = read();
  dfs(1);
  if (g[1][0] < INF || g[1][1] < INF)
    printf("POSSIBLE\n");
  else
    printf("IMPOSSIBLE\n");
  return 0;
}