Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10, Lim = 5e3 + 10;

bitset<Lim> A, B;
int to[N << 1], nex[N << 1], beg[N];
int Min[N], val[N];
int e;

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline void add(int x, int y) { to[++e] = y, nex[e] = beg[x], beg[x] = e; }

inline void Dp(int x, int fa) {
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    Dp(to[i], x);
  }
  A.reset(), A[0] = true;
  int sum = 0, tot = 0;
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    int Minv = min(Min[to[i]], val[to[i]]);
    int Maxv = max(Min[to[i]], val[to[i]]);
    A |= (A << (Maxv - Minv)), sum += Minv, tot += Minv + Maxv;
  }
  For(i, max(0, tot - val[x] - sum), tot - sum * 2) if (A[i]) {
    Min[x] = i + sum;
    break;
  }
  if (Min[x] == INF)
    puts("IMPOSSIBLE"), exit(0);
}

int main() {
  int n = read();
  For(i, 2, n) {
    int f = read();
    add(f, i);
  }
  For(i, 1, n) val[i] = read();
  memset(Min, 63, sizeof(Min));
  Dp(1, 0);
  puts("POSSIBLE");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10, Lim = 5e3 + 10;

bitset<Lim> A, B;
int to[N << 1], nex[N << 1], beg[N];
int Min[N], val[N];
int e;

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline void add(int x, int y) { to[++e] = y, nex[e] = beg[x], beg[x] = e; }

inline void Dp(int x, int fa) {
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    Dp(to[i], x);
  }
  A.reset(), A[0] = true;
  int sum = 0, tot = 0;
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    int Minv = min(Min[to[i]], val[to[i]]);
    int Maxv = max(Min[to[i]], val[to[i]]);
    A |= (A << (Maxv - Minv)), sum += Minv, tot += Minv + Maxv;
  }
  Fordown(i, val[x] - sum, 0) if (A[i]) {
    Min[x] = tot - i - sum;
    break;
  }
  if (Min[x] == INF)
    puts("IMPOSSIBLE"), exit(0);
}

int main() {
  int n = read();
  For(i, 2, n) {
    int f = read();
    add(f, i);
  }
  For(i, 1, n) val[i] = read();
  memset(Min, 63, sizeof(Min));
  Dp(1, 0);
  puts("POSSIBLE");
  return 0;
}