prithivMLmods/Coder-Stat huggingface dataset:
Problem p03601 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll, ll>

ll a, b, c, d, e, f, rw, rs, w, w2, s;

void solve() {
  cin >> a >> b >> c >> d >> e >> f;
  rw = 0;
  rs = 0;
  for (int i = 0; i * a * 100 <= f; i++) {
    for (int j = 0; j * b * 100 <= f; j++) {
      w2 = (i * a) + (j * b);
      w = w2 * 100;
      if (w > f)
        break;
      for (int x = 0; x * c <= w2 * e; x++) {
        for (int y = 0; y * d <= w2 * e; y++) {
          s = min((x * c) + (y * d), f - w);
          if (s > w2 * e)
            break;
          if ((rs == 0 && rw == 0) || s * (rw + rs) >= rs * (w + s)) {
            rs = s;
            rw = w;
          }
        }
      }
    }
  }
  cout << rw + rs << " " << rs << "\n";
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll, ll>

ll a, b, c, d, e, f, rw, rs, w, w2, s;

void solve() {
  cin >> a >> b >> c >> d >> e >> f;
  rw = 0;
  rs = 0;
  for (int i = 0; i * a * 100 <= f; i++) {
    for (int j = 0; j * b * 100 <= f; j++) {
      w2 = (i * a) + (j * b);
      w = w2 * 100;
      if (w > f)
        break;
      for (int x = 0; x * c <= w2 * e; x++) {
        for (int y = 0; y * d <= w2 * e; y++) {
          s = min((x * c) + (y * d), f - w);
          if (s > w2 * e)
            break;
          if ((rs == 0 && rw == 0) || s * (rw + rs) >= rs * (w + s)) {
            rs = s;
            rw = w;
          }
        }
      }
    }
  }
  cout << rw + rs << " " << rs << "\n";
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  // #ifndef ONLINE_JUDGE
  //	freopen("in.txt","r",stdin);
  // #endif
  solve();
  return 0;
}
Problem p03601 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 5e3 + 5;

#define st first
#define nd second

typedef pair<int, int> pun;
typedef long long ll;

bool dp[N][N];

int main() {
  int a, b, c, d, e, f;
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  dp[0][0] = 1;
  for (int water = 0; water <= f; water++) {
    for (int sugar = 0; sugar <= water; sugar++) {
      if (dp[water][sugar] == 0)
        continue;
      dp[water + 100 * a][sugar] = true;
      dp[water + 100 * b][sugar] = true;
      dp[water + c][sugar + c] = true;
      dp[water + d][sugar + d] = true;
    }
  }
  pair<int, int> best = make_pair(0, 0);
  for (int water = 0; water <= f; water++) {
    for (int sugar = 0; sugar <= water; sugar++) {
      if (dp[water][sugar] == 0)
        continue;
      if (e * water < sugar * (e + 100))
        continue;
      if (best.first == 0 || best.second * water < best.first * sugar) {
        best = make_pair(water, sugar);
      }
    }
  }
  printf("%d %d\n", best.first, best.second);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 6e3 + 5;

#define st first
#define nd second

typedef pair<int, int> pun;
typedef long long ll;

bool dp[N][N];

int main() {
  int a, b, c, d, e, f;
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  dp[0][0] = 1;
  for (int water = 0; water <= f; water++) {
    for (int sugar = 0; sugar <= water; sugar++) {
      if (dp[water][sugar] == 0)
        continue;
      dp[water + 100 * a][sugar] = true;
      dp[water + 100 * b][sugar] = true;
      dp[water + c][sugar + c] = true;
      dp[water + d][sugar + d] = true;
    }
  }
  pair<int, int> best = make_pair(0, 0);
  for (int water = 0; water <= f; water++) {
    for (int sugar = 0; sugar <= water; sugar++) {
      if (dp[water][sugar] == 0)
        continue;
      if (e * water < sugar * (e + 100))
        continue;
      if (best.first == 0 || best.second * water < best.first * sugar) {
        best = make_pair(water, sugar);
      }
    }
  }
  printf("%d %d\n", best.first, best.second);
}
Problem p03601 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int a, b, c, d, e, f;
int answ, ansk;
bool g[3010];

int main() {
#ifndef ONLINE_JUDGE
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif
  int i, j, k;
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  for (i = 0; i * c * 100 <= f * e; i++)
    for (j = 0; i * c * 100 + j * d * 100 <= f * e; j++)
      g[i * c + j * d] = true;
  for (i = 0; i * a * 100 <= f; i++)
    for (j = 0; i * a * 100 + j * b * 100 <= f; j++) {
      int w = i * a * 100 + j * b * 100;
      if (w == 0)
        continue;
      for (k = floor(w * 1.0 / 100 * e); k; k--) {
        // printf(":(%d, %d)", k, g[k]);
        if (k + w <= f && g[k])
          break;
      }
      // printf("\n");
      // printf("%d %d\n", w, k);
      if (answ == 0 && ansk == 0 || k * (answ + ansk) >= ansk * (w + k))
        ansk = k, answ = w;
    }
  printf("%d %d\n", answ + ansk, ansk);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int a, b, c, d, e, f;
int answ, ansk;
bool g[3010];

int main() {
  // freopen("C.in", "r", stdin);
  // freopen("C.out", "w", stdout);
  int i, j, k;
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  for (i = 0; i * c * 100 <= f * e; i++)
    for (j = 0; i * c * 100 + j * d * 100 <= f * e; j++)
      g[i * c + j * d] = true;
  for (i = 0; i * a * 100 <= f; i++)
    for (j = 0; i * a * 100 + j * b * 100 <= f; j++) {
      int w = i * a * 100 + j * b * 100;
      if (w == 0)
        continue;
      for (k = floor(w * 1.0 / 100 * e); k; k--) {
        // printf(":(%d, %d)", k, g[k]);
        if (k + w <= f && g[k])
          break;
      }
      // printf("\n");
      // printf("%d %d\n", w, k);
      if (answ == 0 && ansk == 0 || k * (answ + ansk) >= ansk * (w + k))
        ansk = k, answ = w;
    }
  printf("%d %d\n", answ + ansk, ansk);
  return 0;
}
Problem p03601 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b, c, d, e, f;
  cin >> a >> b >> c >> d >> e >> f;

  int x0 = (f + 99) / 100 + 1;
  int y0 = f + 1;
  vector<int> x(x0), y(y0);
  x[0] = 1;
  for (int i = 0; i <= x0; i++) {
    if (i >= a && x[i - a])
      x[i] = 1;
    if (i >= b && x[i - b])
      x[i] = 1;
  }
  y[0] = 1;
  for (int i = 0; i <= y0; i++) {
    if (i >= c && y[i - c])
      y[i] = 1;
    if (i >= d && y[i - d])
      y[i] = 1;
  }

  int r0, r1;
  double m = -1.0;
  for (int i = 1; i <= x0; i++) {
    for (int j = 0; j <= y0; j++) {
      if (x[i] && y[j]) {
        double t = (double)j / (i * 100 + j);
        if (t > m && t - 1e-9 <= (double)e / (100 + e) && i * 100 + j <= f) {
          // cout << t - 1e-9 << ' ' << (double)e / (100 + e) << endl;
          r0 = i;
          r1 = j;
          m = t;
        }
      }
    }
  }

  cout << r0 * 100 + r1 << ' ' << r1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b, c, d, e, f;
  cin >> a >> b >> c >> d >> e >> f;

  int x0 = (f + 99) / 100 + 1;
  int y0 = f + 1;
  vector<int> x(x0 + 1), y(y0 + 1);
  x[0] = 1;
  for (int i = 0; i <= x0; i++) {
    if (i >= a && x[i - a])
      x[i] = 1;
    if (i >= b && x[i - b])
      x[i] = 1;
  }
  y[0] = 1;
  for (int i = 0; i <= y0; i++) {
    if (i >= c && y[i - c])
      y[i] = 1;
    if (i >= d && y[i - d])
      y[i] = 1;
  }

  int r0, r1;
  double m = -1.0;
  for (int i = 1; i <= x0; i++) {
    for (int j = 0; j <= y0; j++) {
      if (x[i] && y[j]) {
        double t = (double)j / (i * 100 + j);
        if (t > m && t - 1e-9 <= (double)e / (100 + e) && i * 100 + j <= f) {
          // cout << t - 1e-9 << ' ' << (double)e / (100 + e) << endl;
          r0 = i;
          r1 = j;
          m = t;
        }
      }
    }
  }

  cout << r0 * 100 + r1 << ' ' << r1 << endl;

  return 0;
}
Problem p03602 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

const int N = 100001;
const int MOD = 998244353;

struct Fenwick {
  vector<int> tree;
  Fenwick(int n) { tree.resize(n + 1); }
  Fenwick() {}
  void add(int in, int val) {
    in++;
    while (in < tree.size()) {
      tree[in] += val;
      in += in & -in;
    }
  }
  ll get(int in) {
    in++;
    ll res = 0;
    while (in) {
      res += tree[in];
      in -= in & -in;
    }
    return res;
  }
  ll get(int l, int r) {
    if (l == 0)
      return get(r);
    return get(r) - get(l - 1);
  }
  ll get_at(int in) { return get(in, in); }
};

bool done[201][201];
int main() {
  //  freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);

  int n;
  cin >> n;
  int A[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        if (A[i][j] + A[j][k] < A[i][k] || A[i][j] != A[j][i]) {
          cout << -1;
          return 0;
        }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == j || i == k || j == k)
          continue;
        if (A[i][j] + A[j][k] == A[i][k]) {
          done[i][k] = 1;
        }
      }
    }
  }
  ll res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (!done[i][j])
        res += A[i][j];
    }
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

const int N = 100001;
const int MOD = 998244353;

struct Fenwick {
  vector<int> tree;
  Fenwick(int n) { tree.resize(n + 1); }
  Fenwick() {}
  void add(int in, int val) {
    in++;
    while (in < tree.size()) {
      tree[in] += val;
      in += in & -in;
    }
  }
  ll get(int in) {
    in++;
    ll res = 0;
    while (in) {
      res += tree[in];
      in -= in & -in;
    }
    return res;
  }
  ll get(int l, int r) {
    if (l == 0)
      return get(r);
    return get(r) - get(l - 1);
  }
  ll get_at(int in) { return get(in, in); }
};

bool done[301][301];
int main() {
  //  freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);

  int n;
  cin >> n;
  int A[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        if (A[i][j] + A[j][k] < A[i][k] || A[i][j] != A[j][i]) {
          cout << -1;
          return 0;
        }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == j || i == k || j == k)
          continue;
        if (A[i][j] + A[j][k] == A[i][k]) {
          done[i][k] = 1;
        }
      }
    }
  }
  ll res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (!done[i][j])
        res += A[i][j];
    }
  }
  cout << res;
}
Problem p03602 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int N = 310;

int mat[N][N], dis[N][N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &mat[i][j]);
      dis[i][j] = mat[i][j];
    }
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == j || i == k || j == k)
          continue;
        dis[i][j] = std::min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (dis[i][j] != mat[i][j]) {
        return printf("-1\n");
      }
      bool flag = true;
      for (int k = 0; k < n; ++k) {
        if (i == k || i == j || k == j)
          continue;
        if (dis[i][k] + dis[k][j] == dis[i][j]) {
          flag = false;
          break;
        }
      }
      if (flag)
        ans += mat[i][j];
    }
  }
  return printf("%lld\n", ans >> 1), 0;
}

Fixed submission:
#include <bits/stdc++.h>

const int N = 310;

int mat[N][N], dis[N][N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &mat[i][j]);
      dis[i][j] = mat[i][j];
    }
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == j || i == k || j == k)
          continue;
        dis[i][j] = std::min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (dis[i][j] != mat[i][j]) {
        return printf("-1\n"), 0;
      }
      bool flag = true;
      for (int k = 0; k < n; ++k) {
        if (i == k || i == j || k == j)
          continue;
        if (dis[i][k] + dis[k][j] == dis[i][j]) {
          flag = false;
          break;
        }
      }
      if (flag)
        ans += mat[i][j];
    }
  }
  return printf("%lld\n", ans >> 1), 0;
}
Problem p03602 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

INF = 10**9 + 1
for i in range(N):
    A[i][i] = INF

ans = 0
for i in range(N):
    Ai = A[i]
    for j in range(i):
        Aj = A[j]
        d = INF * 2
        for k in range(N):
            d = min(d, Ai[k] + Aj[k])

        if A[i][j] > d:
            print("-1")
            exit()
        elif A[i][j] < d:
            ans += A[i][j]

print(ans)


Fixed submission:
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

INF = 10**9 + 1
for i in range(N):
    A[i][i] = INF

ans = 0
for i in range(N):
    Ai = A[i]
    for j in range(i):
        d = min(map(sum, zip(Ai, A[j])))

        if A[i][j] > d:
            print("-1")
            exit()
        elif A[i][j] < d:
            ans += A[i][j]

print(ans)
Problem p03602 (Python)

Buggy submission (status: Runtime Error):
def warshall_floyd(G):
    import copy

    ret = copy.deepcopy(G)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
    return ret


N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

if A == warshall_floyd(A):
    ans = 0
    for i in range(1, N):
        for j in range(0, i):
            if (
                min(
                    [A[i][k] + A[k][j] - A[i][j] for k in range(N) if k != i and k != j]
                )
                > 0
            ):
                ans += A[i][j]
    print(ans)
else:
    print(-1)


Fixed submission:
def warshall_floyd(G):
    import copy

    ret = copy.deepcopy(G)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
    return ret


N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

if A == warshall_floyd(A):
    ans = 0
    for i in range(1, N):
        for j in range(0, i):
            if (
                min(
                    [A[i][k] + A[k][j] - A[i][j] for k in range(N) if k != i and k != j]
                    + [1]
                )
                > 0
            ):
                ans += A[i][j]
    print(ans)
else:
    print(-1)
Problem p03602 (Python)

Buggy submission (status: Runtime Error):
def solve(n, tbl):
    ans = 0
    for i, row_i in enumerate(tbl):
        for j in range(i + 1, n):
            row_j = tbl[j]
            ij = row_i[j]
            mind = min(ik + kj for ik, kj in zip(row_i, row_j) if ik and kj)
            if ij > mind:
                return -1
            if ij < mind:
                ans += ij
    return ans


n = int(input())
tbl = [list(map(int, input().split())) for _ in range(n)]
print(solve(n, tbl))


Fixed submission:
def solve(n, tbl):
    ans = 0
    for i, row_i in enumerate(tbl):
        for j in range(i + 1, n):
            row_j = tbl[j]
            ij = row_i[j]
            mind = min(
                (ik + kj for ik, kj in zip(row_i, row_j) if ik and kj),
                default=float("inf"),
            )
            if ij > mind:
                return -1
            if ij < mind:
                ans += ij
    return ans


n = int(input())
tbl = [list(map(int, input().split())) for _ in range(n)]
print(solve(n, tbl))
Problem p03602 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

int N;

int in[200][200];
long long ans = 0;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> in[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (in[i][j] == 0)
        continue;
      int min = 2000000000;
      for (int k = 0; k < N; k++) {
        if (in[i][k] == 0 || in[k][j] == 0)
          continue;
        if (in[i][k] + in[k][j] < min) {
          min = in[i][k] + in[k][j];
        }
        if (in[i][j] == in[i][k] + in[k][j]) {
          goto kend;
        }
      }
      if (min > in[i][j]) {
        if (i < j)
          ans += in[i][j];
      } else {
        cout << -1 << endl;
        return 0;
      }
    kend:;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int N;

int in[300][300];
long long ans = 0;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> in[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (in[i][j] == 0)
        continue;
      int min = 2000000000;
      for (int k = 0; k < N; k++) {
        if (in[i][k] == 0 || in[k][j] == 0)
          continue;
        if (in[i][k] + in[k][j] < min) {
          min = in[i][k] + in[k][j];
        }
        if (in[i][j] == in[i][k] + in[k][j]) {
          goto kend;
        }
      }
      if (min > in[i][j]) {
        if (i < j)
          ans += in[i][j];
      } else {
        cout << -1 << endl;
        return 0;
      }
    kend:;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 1005, INF = 0x3f3f3f3f;

vector<int> V[N];
int f[N][5005], g[N], h[N];

int n;

void Dfs(int x) {
  int a, b;
  f[x][0] = 0;
  for (auto v : V[x]) {
    Dfs(v);
    a = min(g[v], h[v]);
    b = max(g[v], h[v]);
    for (int j = h[x]; j >= a; --j) {
      if (b > j)
        f[x][j] = f[x][j - a] + b;
      else
        f[x][j] = min(f[x][j - a] + b, f[x][j - b] + a);
    }
    for (int j = 0; j < a; ++j)
      f[x][j] = INF;
  }
  g[x] = f[x][0];
  for (int i = 0; i <= h[x]; ++i)
    g[x] = min(g[x], f[x][i]);
}

int main() {
  scanf("%d", &n);
  memset(f, 0x3f, sizeof f);
  for (int i = 2, j; i <= n; ++i)
    scanf("%d", &j), V[j].emplace_back(i);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &h[i]);
  Dfs(1);
  return puts("POSSIBLE"), 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 1005, INF = 0x3f3f3f3f;

vector<int> V[N];
int f[N][5005], g[N], h[N];

int n;

void Dfs(int x) {
  int a, b;
  f[x][0] = 0;
  for (auto v : V[x]) {
    Dfs(v);
    a = min(g[v], h[v]);
    b = max(g[v], h[v]);
    for (int j = h[x]; j >= a; --j) {
      if (b > j)
        f[x][j] = f[x][j - a] + b;
      else
        f[x][j] = min(f[x][j - a] + b, f[x][j - b] + a);
    }
    for (int j = 0; j < a; ++j)
      f[x][j] = INF;
  }
  g[x] = f[x][0];
  for (int i = 0; i <= h[x]; ++i)
    g[x] = min(g[x], f[x][i]);
  if (g[x] >= INF)
    puts("IMPOSSIBLE"), exit(0);
}

int main() {
  scanf("%d", &n);
  memset(f, 0x3f, sizeof f);
  for (int i = 2, j; i <= n; ++i)
    scanf("%d", &j), V[j].emplace_back(i);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &h[i]);
  Dfs(1);
  return puts("POSSIBLE"), 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxx = 1e3 + 100;
const int MOD = 1e9;
struct node {
  int to, nextt;
} e[maxx];
int n;
int p[maxx];
int x[maxx];
int head[maxx];
int dp[maxx];
int f[maxx];
int cnt = 0;
void add(int u, int v) {
  e[cnt].to = v;
  e[cnt].nextt = head[u];
  head[u] = cnt++;
}
void dfs(int u) {
  for (int i = head[u]; ~i; i = e[i].nextt) {
    dfs(e[i].to);
  }
  dp[0] = 0;
  for (int i = head[u]; ~i; i = e[i].nextt) {
    int v = e[i].to;
    for (int j = x[u]; j >= 0; j--) {
      int temp = INF;
      if (j >= x[v])
        temp = min(temp, dp[j - x[v]] + f[v]);
      if (j >= f[v])
        temp = min(temp, dp[j - f[v]] + x[v]);
      dp[j] = temp;
    }
  }
  for (int i = 0; i <= x[u]; i++) {
    f[u] = min(f[u], dp[i]);
  }
}
int main() {
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  for (int i = 2; i <= n; i++) {
    scanf("%d", &p[i]);
    add(p[i], i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i]);
  }
  memset(f, INF, sizeof(f));
  dfs(1);
  if (f[1] < INF)
    cout << "POSSIBLE" << endl;
  else
    cout << "IMPOSSIBLE" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxx = 1e5 + 100;
const int MOD = 1e9;
struct node {
  int to, nextt;
} e[maxx];
int n;
int p[maxx];
int x[maxx];
int head[maxx];
int dp[maxx];
int f[maxx];
int cnt = 0;
void add(int u, int v) {
  e[cnt].to = v;
  e[cnt].nextt = head[u];
  head[u] = cnt++;
}
void dfs(int u) {
  for (int i = head[u]; ~i; i = e[i].nextt) {
    dfs(e[i].to);
  }
  dp[0] = 0;
  for (int i = head[u]; ~i; i = e[i].nextt) {
    int v = e[i].to;
    for (int j = x[u]; j >= 0; j--) {
      int temp = INF;
      if (j >= x[v])
        temp = min(temp, dp[j - x[v]] + f[v]);
      if (j >= f[v])
        temp = min(temp, dp[j - f[v]] + x[v]);
      dp[j] = temp;
    }
  }
  for (int i = 0; i <= x[u]; i++) {
    f[u] = min(f[u], dp[i]);
  }
}
int main() {
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  for (int i = 2; i <= n; i++) {
    scanf("%d", &p[i]);
    add(p[i], i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i]);
  }
  memset(f, INF, sizeof(f));
  dfs(1);
  if (f[1] < INF)
    cout << "POSSIBLE" << endl;
  else
    cout << "IMPOSSIBLE" << endl;
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

vector<int> x, a;
vector<list<int>> child;

bool dfs(int n) {
  int ruiseki = 0;
  int si = child[n].size();
  bool ans = true;
  vector<int> wei(si);
  int count = 0, weitotal = 0;
  for (auto i : child[n]) {
    ans &= dfs(i);
    ruiseki += min(x[i], a[i]);
    wei[count] = max(x[i], a[i]) - min(x[i], a[i]);
    weitotal += wei[count];
    count++;
  }
  if (ruiseki > x[n]) {
    return false;
  }
  vector<vector<bool>> dp(si + 1, vector<bool>(x[n] - ruiseki + 1, false));
  for (int i = 0; i <= si; i++)
    dp[i][0] = true;
  for (int i = 1; i <= si; i++) {
    for (int j = 1; j <= x[n]; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= wei[i - 1])
        dp[i][j] = dp[i][j] | dp[i - 1][j - wei[i - 1]];
    }
  }
  int ma = 0;
  for (int i = 0; i <= x[n] - ruiseki; i++)
    if (dp[si][i])
      ma = i;
  a[n] = weitotal - ma;
  return ans;
}

int main() {
  int n;
  cin >> n;
  x = vector<int>(n);
  a = vector<int>(n, 0);
  child = vector<list<int>>(n, list<int>());
  int p;
  for (int i = 1; i < n; i++) {
    cin >> p;
    child[p - 1].push_back(i);
  }
  for (int i = 0; i < n; i++)
    cin >> x[i];
  cout << (dfs(0) ? "POSSIBLE" : "IMPOSSIBLE") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

vector<int> x, a;
vector<list<int>> child;

bool dfs(int n) {
  int ruiseki = 0;
  int si = child[n].size();
  bool ans = true;
  vector<int> wei(si);
  int count = 0, weitotal = 0;
  for (auto i : child[n]) {
    ans &= dfs(i);
    ruiseki += min(x[i], a[i]);
    wei[count] = max(x[i], a[i]) - min(x[i], a[i]);
    weitotal += wei[count];
    count++;
  }
  if (ruiseki > x[n]) {
    return false;
  }
  vector<vector<bool>> dp(si + 1, vector<bool>(x[n] - ruiseki + 1, false));
  for (int i = 0; i <= si; i++)
    dp[i][0] = true;
  for (int i = 1; i <= si; i++) {
    for (int j = 1; j <= x[n] - ruiseki; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= wei[i - 1])
        dp[i][j] = dp[i][j] | dp[i - 1][j - wei[i - 1]];
    }
  }
  int ma = 0;
  for (int i = 0; i <= x[n] - ruiseki; i++)
    if (dp[si][i])
      ma = i;
  a[n] = weitotal - ma;
  return ans;
}

int main() {
  int n;
  cin >> n;
  x = vector<int>(n);
  a = vector<int>(n, 0);
  child = vector<list<int>>(n, list<int>());
  int p;
  for (int i = 1; i < n; i++) {
    cin >> p;
    child[p - 1].push_back(i);
  }
  for (int i = 0; i < n; i++)
    cin >> x[i];
  cout << (dfs(0) ? "POSSIBLE" : "IMPOSSIBLE") << endl;
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
const int N = 1002;

inline void apn(int &a, int b) {
  if (a > b)
    a = b;
}

inline char get_c() {
  static char buf[20000], *h, *t;
  if (h == t) {
    t = (h = buf) + fread(buf, 1, 20000, stdin);
  }
  return h == t ? EOF : *h++;
}

inline int nxi() {
  int x = 0;
  char c;
  while ((c = get_c()) > '9' || c < '0')
    ;
  while (x = x * 10 + c - 48, (c = get_c()) >= '0' && c <= '9')
    ;
  return x;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
#endif
  static int fa[N], hx[N], dp[N][5002];
  int n = nxi();
  for (int i = 2; i <= n; ++i)
    fa[i] = nxi();
  for (int i = 1; i <= n; ++i)
    hx[i] = nxi();

  for (int x = n; x; --x) {
    int y = fa[x];
    for (int i = hx[y]; i >= 0; --i) {
      int p = dp[y][i];
      dp[y][i] = 1e5;
      if (hx[x] <= i) {
        dp[y][i] = (hx[x] ? dp[y][i - hx[x]] : p) + dp[x][hx[x]];
      }
      if (dp[x][hx[x]] <= i) {
        apn(dp[y][i], (dp[x][hx[x]] ? dp[y][i - dp[x][hx[x]]] : p) + hx[x]);
      }
    }
  }
  puts(dp[1][hx[1]] >= 1e5 ? "IMPOSSIBLE" : "POSSIBLE");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
const int N = 1002;

inline void apn(int &a, int b) {
  if (a > b)
    a = b;
}

inline char get_c() {
  static char buf[20000], *h, *t;
  if (h == t) {
    t = (h = buf) + fread(buf, 1, 20000, stdin);
  }
  return h == t ? EOF : *h++;
}

inline int nxi() {
  int x = 0;
  char c;
  while ((c = get_c()) > '9' || c < '0')
    ;
  while (x = x * 10 + c - 48, (c = get_c()) >= '0' && c <= '9')
    ;
  return x;
}

int main() {
  static int fa[N], hx[N], dp[N][5002];
  int n = nxi();
  for (int i = 2; i <= n; ++i)
    fa[i] = nxi();
  for (int i = 1; i <= n; ++i)
    hx[i] = nxi();

  for (int x = n; x; --x) {
    int y = fa[x];
    for (int i = hx[y]; i >= 0; --i) {
      int p = dp[y][i];
      dp[y][i] = 1e5;
      if (hx[x] <= i) {
        dp[y][i] = (hx[x] ? dp[y][i - hx[x]] : p) + dp[x][hx[x]];
      }
      if (dp[x][hx[x]] <= i) {
        apn(dp[y][i], (dp[x][hx[x]] ? dp[y][i - dp[x][hx[x]]] : p) + hx[x]);
      }
    }
  }
  puts(dp[1][hx[1]] >= 1e5 ? "IMPOSSIBLE" : "POSSIBLE");
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

/*
  let's try computer shogi!!!
 */

int N;
vector<int> X;
vector<int> W;
vector<vector<int>> P;
const int Xmax = 114514;

bool dfs(int id) {
  // cerr<<id<<"(id)"<<endl;
  int total = 0;
  bool oks[Xmax];
  bool toks[Xmax];
  oks[0] = true;
  for (int i = 0; i < P[id].size(); ++i) {
    // cerr<<id<<","<<P[id][i]<<" id,child"<<endl;
    if (!dfs(P[id][i])) {
      return false;
    }
    for (int k = 0; k < Xmax; ++k) {
      if (oks[k]) {
        toks[k + X[P[id][i]]] = true;
        toks[k + W[P[id][i]]] = true;
      }
    }
    for (int k = 0; k < Xmax; ++k) {
      oks[k] = toks[k];
    }
    total += X[P[id][i]] + W[P[id][i]];
  }
  // cerr<<total<<"(total) oks : ";
  bool possible = false;
  for (int i = 0; i <= total; ++i) {
    // cerr<<oks[i]<<",";
    if (oks[i] && total - i <= X[id]) {
      W[id] = i;
      possible = true;
      break;
    }
  }
  // cerr<<"result "<<possible<<endl;
  return possible;
}

int main() {
  cin >> N;

  X.resize(N);
  P.resize(N);
  W.resize(N);

  for (int i = 0; i < N - 1; ++i) {
    int p;
    cin >> p;
    P[p - 1].push_back(i + 1);
  }

  for (int i = 0; i < N; ++i) {
    cin >> X[i];
  }

  cout << (dfs(0) ? "POSSIBLE" : "IMPOSSIBLE") << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

/*
  let's try computer shogi!!!
 */

int N;
vector<int> X;
vector<int> W;
vector<vector<int>> P;
const int Xmax = 114514;

bool dfs(int id) {
  // cerr<<id<<"(id)"<<endl;
  int total = 0;
  bool oks[Xmax];
  bool toks[Xmax];
  for (int i = 0; i < Xmax; ++i) {
    oks[i] = false;
    toks[i] = false;
  }
  oks[0] = true;
  for (int i = 0; i < P[id].size(); ++i) {
    // cerr<<id<<","<<P[id][i]<<" id,child"<<endl;
    if (!dfs(P[id][i])) {
      return false;
    }
    for (int k = 0; k < Xmax; ++k) {
      if (oks[k]) {
        toks[k + X[P[id][i]]] = true;
        toks[k + W[P[id][i]]] = true;
      }
    }
    for (int k = 0; k < Xmax; ++k) {
      oks[k] = toks[k];
    }
    total += X[P[id][i]] + W[P[id][i]];
  }
  // cerr<<total<<"(total) oks : ";
  bool possible = false;
  for (int i = 0; i <= total; ++i) {
    // cerr<<oks[i]<<",";
    if (oks[i] && total - i <= X[id]) {
      W[id] = i;
      possible = true;
      break;
    }
  }
  // cerr<<"result "<<possible<<endl;
  return possible;
}

int main() {
  cin >> N;

  X.resize(N);
  P.resize(N);
  W.resize(N);

  for (int i = 0; i < N - 1; ++i) {
    int p;
    cin >> p;
    P[p - 1].push_back(i + 1);
  }

  for (int i = 0; i < N; ++i) {
    cin >> X[i];
  }

  cout << (dfs(0) ? "POSSIBLE" : "IMPOSSIBLE") << endl;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
// GhostCai is huger than wjmzbmr
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1005;
struct Edge {
  int to, nxt;
} e[N << 1];
int n, x[N], head[N], ecnt, f[N], g[2][N];
void add(int bg, int ed) {
  e[++ecnt].nxt = head[bg];
  e[ecnt].to = ed;
  head[bg] = ecnt;
}
void dfs(int u) {
  for (int i = head[u]; i; i = e[i].nxt)
    dfs(e[i].to);
  memset(g[0], 0x3f, sizeof g[0]);
  int cur = 0;
  g[cur][0] = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    cur ^= 1;
    memset(g[cur], 0x3f, sizeof g[cur]);
    for (int j = 0; j <= x[u]; j++) {
      if (j - x[v] >= 0)
        g[cur][j] = min(g[cur ^ 1][j - x[v]] + f[v], g[cur][j]);
      if (j - f[v] >= 0)
        g[cur][j] = min(g[cur ^ 1][j - f[v]] + x[v], g[cur][j]);
    }
  }
  for (int i = 0; i <= x[u]; i++)
    f[u] = min(f[u], g[cur][i]);
}
int main() {
  scanf("%d", &n);
  for (int i = 2, p; i <= n; i++)
    scanf("%d", &p), add(p, i);
  for (int i = 1; i <= n; i++)
    scanf("%d", &x[i]);
  memset(f, 0x3f, sizeof f);
  dfs(1);
  if (f[1] < 0x3f3f3f3f)
    puts("POSSIBLE");
  else
    puts("IMPOSSIBLE");
  return 0;
}

Fixed submission:
// GhostCai is huger than wjmzbmr
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1005;
struct Edge {
  int to, nxt;
} e[N << 1];
int n, x[N], head[N], ecnt, f[N], g[2][5005];
void add(int bg, int ed) {
  e[++ecnt].nxt = head[bg];
  e[ecnt].to = ed;
  head[bg] = ecnt;
}
void dfs(int u) {
  for (int i = head[u]; i; i = e[i].nxt)
    dfs(e[i].to);
  memset(g[0], 0x3f, sizeof g[0]);
  int cur = 0;
  g[cur][0] = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    cur ^= 1;
    memset(g[cur], 0x3f, sizeof g[cur]);
    for (int j = 0; j <= x[u]; j++) {
      if (j - x[v] >= 0)
        g[cur][j] = min(g[cur ^ 1][j - x[v]] + f[v], g[cur][j]);
      if (j - f[v] >= 0)
        g[cur][j] = min(g[cur ^ 1][j - f[v]] + x[v], g[cur][j]);
    }
  }
  for (int i = 0; i <= x[u]; i++)
    f[u] = min(f[u], g[cur][i]);
}
int main() {
  scanf("%d", &n);
  for (int i = 2, p; i <= n; i++)
    scanf("%d", &p), add(p, i);
  for (int i = 1; i <= n; i++)
    scanf("%d", &x[i]);
  memset(f, 0x3f, sizeof f);
  dfs(1);
  if (f[1] < 0x3f3f3f3f)
    puts("POSSIBLE");
  else
    puts("IMPOSSIBLE");
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

// #define int long long

vector<int> drogi[1010];
pair<int, int> tab[1010];

// bool plecak[5010];

bool dfs(int where = 1) {
  int a = 0, suma = 0;
  int x;
  vector<bool> plecak(tab[where].first + 3);
  plecak[0] = 1;
  for (int i : drogi[where]) {
    if (!dfs(i))
      return false;
    a += min(tab[i].first, tab[i].second);
    suma += tab[i].first + tab[i].second;
    x = abs(tab[i].first - tab[i].second);
    for (int i = 5000; i >= x; i--) {
      if (plecak[i - x])
        plecak[i] = true;
    }
  }
  if (a > tab[where].first)
    return false;
  int jak;
  for (jak = tab[where].first - a; jak >= 0; jak--) {
    if (plecak[jak])
      break;
  }
  a += jak;
  suma += tab[where].first - a;
  tab[where].second = suma - tab[where].first;

  return true;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  for (int a, i = 2; i <= n; i++) {
    cin >> a;
    drogi[a].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    cin >> tab[i].first;
  }
  if (dfs()) {
    cout << "POSSIBLE";
  } else {
    cout << "IMPOSSIBLE";
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

// #define int long long

vector<int> drogi[1010];
pair<int, int> tab[1010];

// bool plecak[5010];

bool dfs(int where = 1) {
  int a = 0, suma = 0;
  int x;
  vector<bool> plecak(tab[where].first + 3);
  plecak[0] = 1;
  for (int i : drogi[where]) {
    if (!dfs(i))
      return false;
    a += min(tab[i].first, tab[i].second);
    suma += tab[i].first + tab[i].second;
    x = abs(tab[i].first - tab[i].second);
    for (int i = tab[where].first; i >= x; i--) {
      if (plecak[i - x])
        plecak[i] = true;
    }
  }
  if (a > tab[where].first)
    return false;
  int jak;
  for (jak = tab[where].first - a; jak >= 0; jak--) {
    if (plecak[jak])
      break;
  }
  a += jak;
  suma += tab[where].first - a;
  tab[where].second = suma - tab[where].first;

  return true;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  for (int a, i = 2; i <= n; i++) {
    cin >> a;
    drogi[a].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    cin >> tab[i].first;
  }
  if (dfs()) {
    cout << "POSSIBLE";
  } else {
    cout << "IMPOSSIBLE";
  }

  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct edge {
  int k, next;
} e[2005];
int f[1005], a[1005], n, home[1005], cnt = -1;
void add(int x, int y) {
  cnt++;
  e[cnt].k = y;
  e[cnt].next = home[x];
  home[x] = cnt;
}
int g[5005], ok = 1;
void dfs(int k, int F) {
  for (int i = home[k]; ~i; i = e[i].next)
    if (e[i].k != F) {
      dfs(e[i].k, k);
    }
  memset(g, 0x3f, sizeof(g));
  g[0] = 0;
  for (int i = home[k]; ~i; i = e[i].next)
    if (e[i].k != F) {
      for (int p = a[k]; ~p; p--) {
        int x = 1e9;
        if (p >= a[e[i].k])
          x = g[p - a[e[i].k]] + f[e[i].k];
        if (p >= f[e[i].k])
          x = min(x, g[p - f[e[i].k]] + a[e[i].k]);
        g[p] = x;
      }
    }
  f[k] = 1e9;
  for (int i = 0; i <= a[k]; i++)
    f[k] = min(f[k], g[i]);
  if (f[k] > 1e8)
    ok = 0;
}
int main() {
  memset(home, -1, sizeof(home));
  scanf("%d", &n);
  for (int i = 2, x; i <= n; i++) {
    scanf("%d", &x);
    add(i, x);
    add(x, i);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dfs(1, 0);
  if (ok)
    puts("POSSIBLE");
  else
    puts("IMPOSSIBLE");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct edge {
  int k, next;
} e[2005];
int f[1005], a[1005], n, home[1005], cnt = -1;
void add(int x, int y) {
  cnt++;
  e[cnt].k = y;
  e[cnt].next = home[x];
  home[x] = cnt;
}
int g[5005], ok = 1;
void dfs(int k, int F) {
  for (int i = home[k]; ~i; i = e[i].next)
    if (e[i].k != F) {
      dfs(e[i].k, k);
    }
  memset(g, 0x3f, sizeof(g));
  g[0] = 0;
  for (int i = home[k]; ~i; i = e[i].next)
    if (e[i].k != F) {
      for (int p = a[k]; ~p; p--) {
        int x = 1e9;
        if (p >= a[e[i].k])
          x = min(x, g[p - a[e[i].k]] + f[e[i].k]);
        if (p >= f[e[i].k])
          x = min(x, g[p - f[e[i].k]] + a[e[i].k]);
        g[p] = x;
      }
    }
  f[k] = 1e9;
  for (int i = 0; i <= a[k]; i++)
    f[k] = min(f[k], g[i]);
  if (f[k] > 1e8)
    ok = 0;
}
int main() {
  memset(home, -1, sizeof(home));
  scanf("%d", &n);
  for (int i = 2, x; i <= n; i++) {
    scanf("%d", &x);
    add(i, x);
    add(x, i);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dfs(1, 0);
  if (ok)
    puts("POSSIBLE");
  else
    puts("IMPOSSIBLE");
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1009;
int head[1009];
struct edge {
  int to, next;
} edges[1009];
void addedge(int from, int to, int no) {
  edges[no].to = to;
  edges[no].next = head[from];
  head[from] = no;
}
int n;
bool xing;
int arr[maxn];
int mi[maxn], bian[maxn];
void sol(int o, int fa) {
  for (int i = head[o]; ~i; i = edges[i].next) {
    int to = edges[i].to;
    if (to == fa)
      continue;
    sol(to, o);
  }
  int ans = 0;
  int tol = 0;
  for (int i = head[o]; ~i; i = edges[i].next) {
    int to = edges[i].to;
    if (to != fa)
      ans += mi[to], tol += bian[to];
  }
  if (ans > arr[o]) {
    printf("IMPOSSIBLE\n");
    exit(0);
  }
  bool mark[5009] = {0};
  mark[0] = 1;
  for (int i = head[o]; ~i; i = edges[i].next) {
    int to = edges[i].to;
    if (to == fa)
      continue;
    for (int j = arr[o] - ans; j >= bian[to]; j--) {
      mark[j] |= mark[j - bian[to]];
    }
  }
  for (int j = arr[o] - ans; j > -1; j--) {
    if (mark[j]) {
      tol += arr[o] - j - ans;
      int tmp = tol - arr[o];
      mi[o] = min(tmp, arr[o]);
      bian[o] = max(tmp, arr[o]) - mi[o];
      break;
    }
  }
}
int main() {
  scanf("%d", &n);
  int e;
  memset(head, -1, sizeof(head));
  for (int i = 2; i <= n; i++) {
    scanf("%d", &e);
    addedge(e, i, i);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  sol(1, -1);
  printf("POSSIBLE\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1009;
int head[1009];
struct edge {
  int to, next;
} edges[1009];
void addedge(int from, int to, int no) {
  edges[no].to = to;
  edges[no].next = head[from];
  head[from] = no;
}
int n;
bool xing;
int arr[maxn];
int mi[maxn], bian[maxn];
void sol(int o, int fa) {
  for (int i = head[o]; ~i; i = edges[i].next) {
    int to = edges[i].to;
    if (to == fa)
      continue;
    sol(to, o);
  }
  int ans = 0;
  int tol = 0;
  for (int i = head[o]; ~i; i = edges[i].next) {
    int to = edges[i].to;
    if (to != fa)
      ans += mi[to], tol += bian[to];
  }
  tol += ans;
  if (ans > arr[o]) {
    printf("IMPOSSIBLE\n");
    exit(0);
  }
  bool mark[5009] = {0};
  mark[0] = 1;
  for (int i = head[o]; ~i; i = edges[i].next) {
    int to = edges[i].to;
    if (to == fa)
      continue;
    for (int j = arr[o] - ans; j >= bian[to]; j--) {
      mark[j] |= mark[j - bian[to]];
    }
  }
  for (int j = arr[o] - ans; j > -1; j--) {
    if (mark[j]) {
      tol += arr[o] - j - ans;
      int tmp = tol - arr[o];
      mi[o] = min(tmp, arr[o]);
      bian[o] = max(tmp, arr[o]) - mi[o];
      break;
    }
  }
}
int main() {
  scanf("%d", &n);
  int e;
  memset(head, -1, sizeof(head));
  for (int i = 2; i <= n; i++) {
    scanf("%d", &e);
    addedge(e, i, i);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  sol(1, -1);
  printf("POSSIBLE\n");
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= (ll)e; i++)
#define DEC(i, s, e) for (ll i = s; i >= e; i--)
#define aFOR(i, s) for (auto i : s)
#define maxn 1000
#define maxx 500
#define INF 1e9
#define pb push_back
#define int ll
int N;
int p[maxn];
int A[maxn];

vi adj[maxn];

int dp[maxn];

int dp2[maxn][maxx];
int B[maxn];
bool vis[maxn];
bool np = 0;
set<int> st;

void dfs(int x) {
  if (vis[x] || np) {
    np = 1;
    return;
  }
  vis[x] = 1;

  dp[x] = INF;
  aFOR(i, adj[x]) { dfs(i); }
  int sm = 0;
  FOR(i, 0, adj[x].size() - 1) {
    B[i] = abs(A[adj[x][i]] - dp[adj[x][i]]);
    sm += min(A[adj[x][i]], dp[adj[x][i]]);
  }
  if (A[x] < sm) {
    dp[x] = INF;
    np = 1;
    return;
  }

  st.clear();
  st.insert(0);

  FOR(i, 0, adj[x].size() - 1) {
    vi newst;

    aFOR(j, st) newst.pb(j);
    aFOR(j, newst) if (j + B[i] <= 5000) st.insert(j + B[i]);
  }

  // FOR(i,0,adj[x].size()-1) cout<<B[i]<<' ';
  // cout<<'\n';

  if (adj[x].empty())
    dp[x] = 0;
  else {
    auto it = st.upper_bound(A[x] - sm);
    it--;
    // cout<<*it<<' ';
    dp[x] = -*it + sm;

    FOR(i, 0, adj[x].size() - 1) dp[x] += B[i];
  }
}

int32_t main() {
  cin >> N;

  FOR(i, 2, N) {
    cin >> p[i];
    adj[p[i]].pb(i);
  }

  FOR(i, 1, N) cin >> A[i];

  dfs(1);

  if (dp[1] == INF || np)
    cout << "IMPOSSIBLE";
  else
    cout << "POSSIBLE";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= (ll)e; i++)
#define DEC(i, s, e) for (ll i = s; i >= e; i--)
#define aFOR(i, s) for (auto i : s)
#define maxn 1001
#define maxx 501
#define INF 1e9
#define pb push_back
#define int ll
int N;
int p[maxn];
int A[maxn];

vi adj[maxn];

int dp[maxn];

int dp2[maxn][maxx];
int B[maxn];
bool vis[maxn];
bool np = 0;
set<int> st;

void dfs(int x) {
  if (vis[x] || np) {
    np = 1;
    return;
  }
  vis[x] = 1;

  dp[x] = INF;
  aFOR(i, adj[x]) { dfs(i); }
  int sm = 0;
  FOR(i, 0, adj[x].size() - 1) {
    B[i] = abs(A[adj[x][i]] - dp[adj[x][i]]);
    sm += min(A[adj[x][i]], dp[adj[x][i]]);
  }
  if (A[x] < sm) {
    dp[x] = INF;
    np = 1;
    return;
  }

  st.clear();
  st.insert(0);

  FOR(i, 0, adj[x].size() - 1) {
    vi newst;

    aFOR(j, st) newst.pb(j);
    aFOR(j, newst) if (j + B[i] <= 5000) st.insert(j + B[i]);
  }

  // FOR(i,0,adj[x].size()-1) cout<<B[i]<<' ';
  // cout<<'\n';

  if (adj[x].empty())
    dp[x] = 0;
  else {
    auto it = st.upper_bound(A[x] - sm);
    it--;
    // cout<<*it<<' ';
    dp[x] = -*it + sm;

    FOR(i, 0, adj[x].size() - 1) dp[x] += B[i];
  }
}

int32_t main() {
  cin >> N;

  FOR(i, 2, N) {
    cin >> p[i];
    adj[p[i]].pb(i);
  }

  FOR(i, 1, N) cin >> A[i];

  dfs(1);

  if (dp[1] == INF || np)
    cout << "IMPOSSIBLE";
  else
    cout << "POSSIBLE";
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F(i, a, b) for (int i = a; i <= b; i++)
#define G(i, a, b) for (int i = a; i >= b; i--)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mem(a, b) memset(a, b, sizeof a)
#define mn(a, b) ((a) = min(a, b))

const int inf = 2e9;
const int N = 5e3 + 10;
const int M = 5e3 + 10;

using namespace std;

int n, x, p[N], w[N], f[N], g1[M], g2[M];
int tov[N], nex[N], las[N], tot;

void ins(int x, int y) { tov[++tot] = y, nex[tot] = las[x], las[x] = tot; }

void Dfs(int k) {
  for (int x = las[k]; x; x = nex[x])
    Dfs(tov[x]);
  g1[0] = 0;
  F(i, 1, w[k]) g1[i] = inf;
  for (x = las[k]; x; x = nex[x]) {
    F(i, 0, w[k]) g2[i] = inf;
    G(i, w[k] - w[tov[x]], 0)
    mn(g2[i + w[tov[x]]], g1[i] + f[tov[x]]);
    G(i, w[k] - f[tov[x]], 0)
    mn(g2[i + f[tov[x]]], g1[i] + w[tov[x]]);
    F(i, 0, w[k]) g1[i] = g2[i];
  }
  f[k] = inf;
  F(i, 0, w[k])
  mn(f[k], g1[i]);
}

int main() {
  scanf("%d", &n);
  F(i, 2, n) scanf("%d", &p[i]), ins(p[i], i);
  F(i, 1, n) scanf("%d", &w[i]);

  Dfs(1);

  puts(f[1] == inf ? "IMPOSSIBLE" : "POSSIBLE");
}

Fixed submission:
#include <bits/stdc++.h>

#define F(i, a, b) for (int i = a; i <= b; i++)
#define G(i, a, b) for (int i = a; i >= b; i--)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mem(a, b) memset(a, b, sizeof a)
#define mn(a, b) ((a) = min(a, b))

const int inf = 1e9;
const int N = 1e3 + 10;
const int M = 5e3 + 10;

using namespace std;

int n, x, p[N], w[N], f[N], g1[M], g2[M];
int tov[N], nex[N], las[N], tot;

void ins(int x, int y) { tov[++tot] = y, nex[tot] = las[x], las[x] = tot; }

void Dfs(int k) {
  for (int x = las[k]; x; x = nex[x])
    Dfs(tov[x]);
  g1[0] = 0;
  F(i, 1, w[k]) g1[i] = inf;
  for (x = las[k]; x; x = nex[x]) {
    F(i, 0, w[k]) g2[i] = inf;
    G(i, w[k] - w[tov[x]], 0)
    mn(g2[i + w[tov[x]]], g1[i] + f[tov[x]]);
    G(i, w[k] - f[tov[x]], 0)
    mn(g2[i + f[tov[x]]], g1[i] + w[tov[x]]);
    F(i, 0, w[k]) g1[i] = g2[i];
  }
  f[k] = inf;
  F(i, 0, w[k])
  mn(f[k], g1[i]);
}

int main() {
  scanf("%d", &n);
  F(i, 2, n) scanf("%d", &p[i]), ins(p[i], i);
  F(i, 1, n) scanf("%d", &w[i]);

  Dfs(1);

  puts(f[1] == inf ? "IMPOSSIBLE" : "POSSIBLE");
}