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

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define INF 1000000000

vector<vector<int>> tree;
vector<int> X;
vector<int> dp(5010);
vector<int> nex(5010);

int dfs(int v) {
  vector<int> tmp;
  if (tree[v].size() == 0) {
    return 0;
  }
  for (int u : tree[v]) {
    tmp.push_back(dfs(u));
    if (tmp.back() == -1)
      return -1;
  }
  REP(i, 5010) dp[i] = INF;
  REP(i, tree[v].size()) {
    int u = tree[v][i];
    if (i == 0) {
      if (X[u] < 5010)
        dp[X[u]] = tmp[0];
      if (X[u] < 5010)
        dp[tmp[0]] = X[u];
      continue;
    }
    REP(j, 5010) nex[j] = INF;
    REP(j, 5010) {
      int a = X[u];
      int b = tmp[i];
      if (j + a < 5010) {
        nex[j + a] = min(nex[j + a], dp[j] + b);
      }
      if (j + b < 5010) {
        nex[j + b] = min(nex[j + b], dp[j] + a);
      }
    }
    dp = nex;
  }
  int ret = INF;
  REP(i, X[v] + 1) { ret = min(ret, dp[i]); }
  if (ret == INF)
    ret = -1;
  return ret;
}

int main() {
  int n;
  cin >> n;
  tree.resize(n);
  REP(i, n - 1) {
    int p;
    cin >> p;
    p--;
    tree[p].push_back(i + 1);
  }
  X.resize(n);
  REP(i, n) { cin >> X[i]; }
  cout << (dfs(0) >= 0 ? "POSSIBLE" : "IMPOSSIBLE") << endl;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define INF 1000000000

vector<vector<int>> tree;
vector<int> X;
vector<int> dp(5010);
vector<int> nex(5010);

int dfs(int v) {
  vector<int> tmp;
  if (tree[v].size() == 0) {
    return 0;
  }
  for (int u : tree[v]) {
    tmp.push_back(dfs(u));
    if (tmp.back() == -1)
      return -1;
  }
  REP(i, 5010) dp[i] = INF;
  REP(i, tree[v].size()) {
    int u = tree[v][i];
    if (i == 0) {
      if (X[u] < 5010)
        dp[X[u]] = tmp[0];
      if (tmp[0] < 5010)
        dp[tmp[0]] = X[u];
      continue;
    }
    REP(j, 5010) nex[j] = INF;
    REP(j, 5010) {
      int a = X[u];
      int b = tmp[i];
      if (j + a < 5010) {
        nex[j + a] = min(nex[j + a], dp[j] + b);
      }
      if (j + b < 5010) {
        nex[j + b] = min(nex[j + b], dp[j] + a);
      }
    }
    dp = nex;
  }
  int ret = INF;
  REP(i, X[v] + 1) { ret = min(ret, dp[i]); }
  if (ret == INF)
    ret = -1;
  return ret;
}

int main() {
  int n;
  cin >> n;
  tree.resize(n);
  REP(i, n - 1) {
    int p;
    cin >> p;
    p--;
    tree[p].push_back(i + 1);
  }
  X.resize(n);
  REP(i, n) { cin >> X[i]; }
  cout << (dfs(0) >= 0 ? "POSSIBLE" : "IMPOSSIBLE") << endl;
}
Problem p03603 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
#ifdef VC
#define scanf scanf_s
#endif

const int Maxn = 1005;
const int mod = 1000000007;

int n;
vector<int> offs[Maxn];
ll X[Maxn];
ll Y[Maxn];
bool flg;
int dp[5010];

void dfs(int v) {
  if (offs[v].size() == 0) {
    Y[v] = 0;
    return;
  }
  for (int i = 0; i < offs[v].size(); i++) {
    dfs(offs[v][i]);
    if (!flg)
      return;
  }
  ll tsum = 0LL;
  for (int i = 0; i < X[v] + 1; i++)
    dp[i] = 0;
  dp[X[offs[v][0]]] = 1;
  tsum += X[offs[v][0]];
  dp[Y[offs[v][0]]] = 1;
  tsum += Y[offs[v][0]];
  for (int i = 1; i < offs[v].size(); i++) {
    tsum += X[offs[v][i]];
    tsum += Y[offs[v][i]];
    for (int j = X[v]; j >= 0; j--) {
      if (dp[j] == i) {
        if (j + X[offs[v][i]] <= X[v]) {
          dp[j + X[offs[v][i]]] = i + 1;
        }
        if (j + Y[offs[v][i]] <= X[v]) {
          dp[j + Y[offs[v][i]]] = i + 1;
        }
      }
    }
  }
  for (int i = X[v]; i >= 0; i--) {
    if (dp[i] == offs[v].size()) {
      Y[v] = tsum - i;
      return;
    }
  }
  flg = false;
  return;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int p;
    scanf("%d", &p);
    p--;
    offs[p].push_back(i);
  }
  for (int i = 0; i < n; i++)
    scanf("%d", &X[i]);
  flg = true;
  dfs(0);
  if (flg) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }

  return 0;
}


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

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
#ifdef VC
#define scanf scanf_s
#endif

const int Maxn = 1005;
const int mod = 1000000007;

int n;
vector<int> offs[Maxn];
ll X[Maxn];
ll Y[Maxn];
bool flg;
int dp[5010];

void dfs(int v) {
  if (offs[v].size() == 0) {
    Y[v] = 0;
    return;
  }
  for (int i = 0; i < offs[v].size(); i++) {
    dfs(offs[v][i]);
    if (!flg)
      return;
  }
  ll tsum = 0LL;
  for (int i = 0; i < X[v] + 1; i++)
    dp[i] = 0;
  dp[X[offs[v][0]]] = 1;
  tsum += X[offs[v][0]];
  if (Y[offs[v][0]] <= X[v]) {
    dp[Y[offs[v][0]]] = 1;
    tsum += Y[offs[v][0]];
  }
  for (int i = 1; i < offs[v].size(); i++) {
    tsum += X[offs[v][i]];
    tsum += Y[offs[v][i]];
    for (int j = X[v]; j >= 0; j--) {
      if (dp[j] == i) {
        if (j + X[offs[v][i]] <= X[v]) {
          dp[j + X[offs[v][i]]] = i + 1;
        }
        if (j + Y[offs[v][i]] <= X[v]) {
          dp[j + Y[offs[v][i]]] = i + 1;
        }
      }
    }
  }
  for (int i = X[v]; i >= 0; i--) {
    if (dp[i] == offs[v].size()) {
      Y[v] = tsum - i;
      return;
    }
  }
  flg = false;
  return;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int p;
    scanf("%d", &p);
    p--;
    offs[p].push_back(i);
  }
  for (int i = 0; i < n; i++)
    scanf("%d", &X[i]);
  flg = true;
  dfs(0);
  if (flg) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }

  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
const int maxW = 5000 + 5;
const int inf = 0x3f3f3f3f;
vector<int> ed[maxn];
void addedge(int from, int to) {
  ed[from].push_back(to);
  ed[to].push_back(from);
}
int n;
int X[maxn], f[maxn][2], g[maxn][maxW];
void dfs(int v, int fa) {
  f[v][0] = f[v][1] = inf;
  for (int i = 0; i < (int)ed[v].size(); i++) {
    int u = ed[v][i];
    if (u != fa)
      dfs(u, v);
  }
  memset(g, inf, sizeof(g));
  for (int i = 0; i <= 1; i++) {
    g[0][0] = 0;
    int cnt = 0;
    for (int j = 1; j <= X[v]; j++)
      g[0][j] = inf;
    for (int j = 0; j < (int)ed[v].size(); j++) {
      int u = ed[v][j];
      if (u == fa)
        continue;
      cnt++;
      for (int w = 0; w <= X[v]; w++)
        g[cnt][w] = inf;
      for (int w = 0; w <= X[v]; w++) {
        if (w + X[u] <= X[v])
          g[cnt][w + X[u]] = min(g[cnt - 1][w] + f[u][i ^ 1], g[cnt][w + X[u]]);
        if (w + f[u][i ^ 1] <= X[v])
          g[cnt][w + f[u][i ^ 1]] =
              min(g[cnt][w + f[u][i ^ 1]], g[cnt - 1][w] + X[u]);
      }
    }
    for (int w = 0; w <= X[v]; w++)
      f[v][i] = min(f[v][i], g[cnt][w]);
  }
  // printf("f[%d][0]=%d f[%d][1]=%d\n",v,f[v][0],v,f[v][1]);
}
int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    int p;
    scanf("%d", &p);
    addedge(i, p);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &X[i]);
  dfs(1, -1);
  if (f[1][0] < inf || f[1][1] < inf)
    printf("POSSIBLE\n");
  else
    printf("IMPOSSIBLE\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
const int maxW = 5000 + 5;
const int inf = 0x3f3f3f3f;
vector<int> ed[maxn];
void addedge(int from, int to) {
  ed[from].push_back(to);
  ed[to].push_back(from);
}
int n;
int X[maxn], f[maxn][2], g[maxn][maxW];
void dfs(int v, int fa) {
  f[v][0] = f[v][1] = inf;
  for (int i = 0; i < (int)ed[v].size(); i++) {
    int u = ed[v][i];
    if (u != fa)
      dfs(u, v);
  }
  for (int i = 0; i <= 1; i++) {
    g[0][0] = 0;
    int cnt = 0;
    for (int j = 1; j <= X[v]; j++)
      g[0][j] = inf;
    for (int j = 0; j < (int)ed[v].size(); j++) {
      int u = ed[v][j];
      if (u == fa)
        continue;
      cnt++;
      for (int w = 0; w <= X[v]; w++)
        g[cnt][w] = inf;
      for (int w = 0; w <= X[v]; w++) {
        if (w + X[u] <= X[v])
          g[cnt][w + X[u]] = min(g[cnt - 1][w] + f[u][i ^ 1], g[cnt][w + X[u]]);
        if (w + f[u][i ^ 1] <= X[v])
          g[cnt][w + f[u][i ^ 1]] =
              min(g[cnt][w + f[u][i ^ 1]], g[cnt - 1][w] + X[u]);
      }
    }
    for (int w = 0; w <= X[v]; w++)
      f[v][i] = min(f[v][i], g[cnt][w]);
  }
  // printf("f[%d][0]=%d f[%d][1]=%d\n",v,f[v][0],v,f[v][1]);
}
int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    int p;
    scanf("%d", &p);
    addedge(i, p);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &X[i]);
  dfs(1, -1);
  if (f[1][0] < inf || f[1][1] < inf)
    printf("POSSIBLE\n");
  else
    printf("IMPOSSIBLE\n");
  return 0;
}
Problem p03603 (C++)

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

// 達成できる最小の重みを返す
// 達成できない場合は -1
int dfs(vector<vector<int>> &adj, int u, vector<int> &w) {

  // 極小
  if (adj[u].size() == 0)
    return 0;

  vector<pair<int, int>> vec;
  int sum = 0;
  for (auto v : adj[u]) {
    int val = dfs(adj, v, w);
    if (val == -1)
      return -1;

    vec.push_back({val, w[v]});
    sum += val + w[v];
  }

  int n = (int)vec.size();
  // ここから DP する
  vector<vector<bool>> dp(n + 1, vector<bool>(sum + 1, false));
  dp[0][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      if (dp[i][j] == false)
        continue;
      dp[i + 1][j + vec[i].first] = dp[i + 1][j + vec[i].second] = true;
    }
  }

  int ret = -1;
  for (int j = w[u]; j >= 0; j--) {
    if (dp[n][j]) {
      ret = sum - j;
      break;
    }
  }

  return ret;
}

signed main() {

  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  vector<int> w(n);
  for (int i = 1; i < n; i++) {
    // 頂点 i の親が p
    int p;
    cin >> p;
    p--;
    adj[p].push_back(i);
  }

  for (int i = 0; i < n; i++)
    cin >> w[i];

  string ans = "POSSIBLE";
  if (dfs(adj, 0, w) == -1)
    ans = "IM" + ans;
  cout << ans << endl;
  return 0;
}

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

// 達成できる最小の重みを返す
// 達成できない場合は -1
int dfs(vector<vector<int>> &adj, int u, vector<int> &w) {

  // 極小
  if (adj[u].size() == 0)
    return 0;

  vector<pair<int, int>> vec;
  int sum = 0;
  for (auto v : adj[u]) {
    int val = dfs(adj, v, w);
    if (val == -1)
      return -1;

    vec.push_back({val, w[v]});
    sum += val + w[v];
  }

  int n = (int)vec.size();
  // ここから DP する
  vector<vector<bool>> dp(n + 1, vector<bool>(sum + 1, false));
  dp[0][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      if (dp[i][j] == false)
        continue;
      dp[i + 1][j + vec[i].first] = dp[i + 1][j + vec[i].second] = true;
    }
  }

  int ret = -1;
  for (int j = min(w[u], sum); j >= 0; j--) {

    if (dp[n][j]) {
      ret = sum - j;
      break;
    }
  }

  return ret;
}

signed main() {

  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  vector<int> w(n);
  for (int i = 1; i < n; i++) {
    // 頂点 i の親が p
    int p;
    cin >> p;
    p--;
    adj[p].push_back(i);
  }

  for (int i = 0; i < n; i++)
    cin >> w[i];

  string ans = "POSSIBLE";
  if (dfs(adj, 0, w) == -1)
    ans = "IM" + ans;
  cout << ans << endl;
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define per(i, x, y) for (int i = (x); i >= (y); i--)
#define ll long long
#define ld long double
#define inf 1000000000
using namespace std;
#define N 1005
int n, head[N], cnt, a[N], f[N], g[2][N], flag; // f[i]表示
struct edge {
  int to, nxt;
} e[N << 1];
void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void ins(int x, int y) { adde(x, y); }
void dfs(int u) {
  if (!head[u]) {
    f[u] = 0;
    return;
  }
  // g[i]表示和u颜色相同的点权和为i的情况下，最小的和u颜色不同的点权和。
  for (int i = head[u], v; i; i = e[i].nxt) {
    v = e[i].to;
    dfs(v);
  }
  int nk = 0, k = 1;
  memset(g[nk], 0, sizeof(g[nk]));
  for (int i = head[u], v; i; i = e[i].nxt) {
    // a[v] or f[v] ===> sum<=a[u]
    v = e[i].to;
    nk ^= 1, k ^= 1;
    memset(g[nk], 0x3f, sizeof(g[nk]));
    per(j, a[u], 0) {
      if (j >= a[v])
        g[nk][j] = min(g[nk][j], g[k][j - a[v]] + f[v]);
      if (j >= f[v])
        g[nk][j] = min(g[nk][j], g[k][j - f[v]] + a[v]);
    }
  }
  rep(i, 0, a[u]) f[u] = min(f[u], g[nk][i]);
  if (f[u] > inf)
    flag = 0;
}
int main() {
  scanf("%d", &n);
  rep(i, 2, n) {
    int x;
    scanf("%d", &x);
    ins(x, i);
  }
  rep(i, 1, n) scanf("%d", &a[i]);
  memset(f, 0x3f, sizeof(f));
  flag = 1;
  dfs(1);
  // rep (i,1,n) cerr<<f[i]<<' ';cerr<<endl;
  puts(!flag ? "IMPOSSIBLE" : "POSSIBLE");
  return 0;
}
/*
8
1 1 1 3 4 5 5
4 1 6 2 2 1 3 3
*/

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define per(i, x, y) for (int i = (x); i >= (y); i--)
#define ll long long
#define ld long double
#define inf 1000000000
using namespace std;
#define N 5005
int n, head[N], cnt, a[N], f[N], g[2][N], flag; // f[i]表示
struct edge {
  int to, nxt;
} e[N << 1];
void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void ins(int x, int y) { adde(x, y); }
void dfs(int u) {
  if (!head[u]) {
    f[u] = 0;
    return;
  }
  // g[i]表示和u颜色相同的点权和为i的情况下，最小的和u颜色不同的点权和。
  for (int i = head[u], v; i; i = e[i].nxt) {
    v = e[i].to;
    dfs(v);
  }
  int nk = 0, k = 1;
  memset(g[nk], 0, sizeof(g[nk]));
  for (int i = head[u], v; i; i = e[i].nxt) {
    // a[v] or f[v] ===> sum<=a[u]
    v = e[i].to;
    nk ^= 1, k ^= 1;
    memset(g[nk], 0x3f, sizeof(g[nk]));
    per(j, a[u], 0) {
      if (j >= a[v])
        g[nk][j] = min(g[nk][j], g[k][j - a[v]] + f[v]);
      if (j >= f[v])
        g[nk][j] = min(g[nk][j], g[k][j - f[v]] + a[v]);
    }
  }
  rep(i, 0, a[u]) f[u] = min(f[u], g[nk][i]);
  if (f[u] > inf)
    flag = 0;
}
int main() {
  scanf("%d", &n);
  rep(i, 2, n) {
    int x;
    scanf("%d", &x);
    ins(x, i);
  }
  rep(i, 1, n) scanf("%d", &a[i]);
  memset(f, 0x3f, sizeof(f));
  flag = 1;
  dfs(1);
  // rep (i,1,n) cerr<<f[i]<<' ';cerr<<endl;
  puts(!flag ? "IMPOSSIBLE" : "POSSIBLE");
  return 0;
}
/*
8
1 1 1 3 4 5 5
4 1 6 2 2 1 3 3
*/
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
#define int long long
#define MOD 1000000007
typedef pair<int, int> P;
typedef pair<int, P> PP;

vector<int> g[1000], x(1000);

int rec(int v) {
  int INF = 111111111;
  vector<vector<int>> dp(2, vector<int>(5010, INF));
  if (g[v].size() == 0) {
    return 0;
  }
  for (int i = 0; i < g[v].size(); i++) {
    int ii = i % 2;
    int ni = (i + 1) % 2;
    dp[ni] = vector<int>(5010, INF);
    int b = x[g[v][i]];
    int w = rec(g[v][i]);
    if (w == -1)
      return w;
    if (i == 0) {
      dp[1][w] = b;
      dp[1][b] = w;
      continue;
    }
    for (int j = 5000; j >= 0; j--) {
      if (dp[ii][j] == INF)
        continue;
      if (j + w <= 5000)
        dp[ni][j + w] = min(dp[ni][j + w], dp[ii][j] + b);
      if (j + b <= 5000)
        dp[ni][j + b] = min(dp[ni][j + b], dp[ii][j] + w);
    }
  }
  int w = INF;
  for (int i = x[v]; i >= 0; i--) {
    w = min(w, dp[(g[v].size()) % 2][i]);
  }
  if (w == INF)
    return -1;
  return w;
}

signed main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    g[--p].pb(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  if (rec(0) == -1)
    cout << "IM";
  cout << "POSSIBLE" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
#define int long long
#define MOD 1000000007
typedef pair<int, int> P;
typedef pair<int, P> PP;

vector<int> g[1000], x(1000);

int rec(int v) {
  int INF = 111111111;
  vector<vector<int>> dp(2, vector<int>(5010, INF));
  if (g[v].size() == 0) {
    return 0;
  }
  for (int i = 0; i < g[v].size(); i++) {
    int ii = i % 2;
    int ni = (i + 1) % 2;
    dp[ni] = vector<int>(5010, INF);
    int b = x[g[v][i]];
    int w = rec(g[v][i]);
    if (w == -1)
      return w;
    if (i == 0) {
      if (w <= 5000)
        dp[1][w] = b;
      if (b <= 5000)
        dp[1][b] = w;
      continue;
    }
    for (int j = 5000; j >= 0; j--) {
      if (dp[ii][j] == INF)
        continue;
      if (j + w <= 5000)
        dp[ni][j + w] = min(dp[ni][j + w], dp[ii][j] + b);
      if (j + b <= 5000)
        dp[ni][j + b] = min(dp[ni][j + b], dp[ii][j] + w);
    }
  }
  int w = INF;
  for (int i = x[v]; i >= 0; i--) {
    w = min(w, dp[(g[v].size()) % 2][i]);
  }
  if (w == INF)
    return -1;
  return w;
}

signed main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    g[--p].pb(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  if (rec(0) == -1)
    cout << "IM";
  cout << "POSSIBLE" << endl;
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
namespace std {
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
} // namespace std

const int N(1010), M(5010);
struct edge {
  int next, to;
} e[N];
int n, X[N], f[N], g[N], head[N], max, e_num;
inline void add_edge(int from, int to) {
  e[++e_num] = (edge){head[from], to};
  head[from] = e_num;
}

void dfs(int x) {
  for (int i = head[x]; i; i = e[i].next)
    dfs(e[i].to);
  memset(g, 0x3f, (X[x] + 1) << 2), g[0] = 0;
  for (int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    for (int j = X[x]; ~j; j--) {
      int t = 0x3f3f3f3f;
      if (j >= X[to])
        t = std::min(t, g[j - X[to]] + f[to]);
      if (j >= f[to])
        t = std::min(t, g[j - f[to]] + X[to]);
      g[j] = t;
    }
  }
  for (int i = 0; i <= X[x]; i++)
    f[x] = std::min(f[x], g[i]);
}

int main() {
  scanf("%d", &n), memset(f, 0x3f, sizeof f);
  for (int i = 2, x; i <= n; i++)
    scanf("%d", &x), add_edge(x, i);
  for (int i = 1; i <= n; i++)
    scanf("%d", X + i), max = std::max(max, X[i]);
  dfs(1);
  puts(f[1] == 0x3f3f3f3f ? "IMPOSSIBLE" : "POSSIBLE");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
namespace std {
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
} // namespace std

const int N(1010), M(5010);
struct edge {
  int next, to;
} e[N];
int n, X[N], f[N], g[M], head[N], max, e_num;
inline void add_edge(int from, int to) {
  e[++e_num] = (edge){head[from], to};
  head[from] = e_num;
}

void dfs(int x) {
  for (int i = head[x]; i; i = e[i].next)
    dfs(e[i].to);
  memset(g, 0x3f, (X[x] + 1) << 2), g[0] = 0;
  for (int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    for (int j = X[x]; ~j; j--) {
      int t = 0x3f3f3f3f;
      if (j >= X[to])
        t = std::min(t, g[j - X[to]] + f[to]);
      if (j >= f[to])
        t = std::min(t, g[j - f[to]] + X[to]);
      g[j] = t;
    }
  }
  for (int i = 0; i <= X[x]; i++)
    f[x] = std::min(f[x], g[i]);
}

int main() {
  scanf("%d", &n), memset(f, 0x3f, sizeof f);
  for (int i = 2, x; i <= n; i++)
    scanf("%d", &x), add_edge(x, i);
  for (int i = 1; i <= n; i++)
    scanf("%d", X + i), max = std::max(max, X[i]);
  dfs(1);
  puts(f[1] == 0x3f3f3f3f ? "IMPOSSIBLE" : "POSSIBLE");
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 10;
const int M = 10;

int x[N];
vector<int> g[N];

int dp[N];

void dfs(int cur) {
  bitset<2 * M> c;
  c[0] = 1;
  int s = 0;
  for (auto t : g[cur]) {
    dfs(t);
    c = (c << x[t]) | (c << dp[t]);
    s += x[t];
    s += dp[t];
  }
  for (int i = x[cur]; i >= 0; i--) {
    if (c[i]) {
      dp[cur] = s - i;
      return;
    }
  }
  cout << "IMPOSSIBLE\n";
  exit(0);
}

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

  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    p--;
    g[p].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  dfs(0);
  cout << "POSSIBLE\n";
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 1e3 + 7;
const int M = 5e3 + 7;

int x[N];
vector<int> g[N];

int dp[N];

void dfs(int cur) {
  bitset<2 * M> c;
  c[0] = 1;
  int s = 0;
  for (auto t : g[cur]) {
    dfs(t);
    c = (c << x[t]) | (c << dp[t]);
    s += x[t];
    s += dp[t];
  }
  for (int i = x[cur]; i >= 0; i--) {
    if (c[i]) {
      dp[cur] = s - i;
      return;
    }
  }
  cout << "IMPOSSIBLE\n";
  exit(0);
}

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

  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    p--;
    g[p].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  dfs(0);
  cout << "POSSIBLE\n";
}
Problem p03603 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, a[1001], f[1001], g[1001][5001], head[1001], now[1001], tot = 0;
struct data {
  int next, num;
} edge[2001];
void add(int u, int v) {
  edge[++tot].next = head[u];
  edge[tot].num = v;
  head[u] = tot;
}
void dfs(int x) {
  memset(g[x], 0x3f, sizeof(g[x]));
  g[x][0] = 0;
  for (int i = head[x]; i != -1; i = edge[i].next) {
    int kx = edge[i].num;
    dfs(kx);
    memset(now, 0x3f, sizeof(now));
    for (int j = a[kx]; j <= a[x]; j++)
      now[j] = min(now[j], g[x][j - a[kx]] + f[kx]);
    for (int j = f[kx]; j <= a[x]; j++)
      now[j] = min(now[j], g[x][j - f[kx]] + a[kx]);
    for (int j = 0; j <= a[x]; j++)
      g[x][j] = now[j];
  }
  f[x] = INF;
  for (int i = 0; i <= a[x]; i++)
    f[x] = min(f[x], g[x][i]);
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    int fat;
    scanf("%d", &fat);
    add(fat, i);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dfs(1);
  if (f[1] == INF)
    printf("IMPOSSIBLE\n");
  else
    printf("POSSIBLE\n");
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, a[1001], f[1001], g[1001][5001], head[1001], now[5001], tot = 0;
struct data {
  int next, num;
} edge[2001];
void add(int u, int v) {
  edge[++tot].next = head[u];
  edge[tot].num = v;
  head[u] = tot;
}
void dfs(int x) {
  memset(g[x], 0x3f, sizeof(g[x]));
  g[x][0] = 0;
  for (int i = head[x]; i != -1; i = edge[i].next) {
    int kx = edge[i].num;
    dfs(kx);
    memset(now, 0x3f, sizeof(now));
    for (int j = a[kx]; j <= a[x]; j++)
      now[j] = min(now[j], g[x][j - a[kx]] + f[kx]);
    for (int j = f[kx]; j <= a[x]; j++)
      now[j] = min(now[j], g[x][j - f[kx]] + a[kx]);
    for (int j = 0; j <= a[x]; j++)
      g[x][j] = now[j];
  }
  f[x] = INF;
  for (int i = 0; i <= a[x]; i++)
    f[x] = min(f[x], g[x][i]);
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    int fat;
    scanf("%d", &fat);
    add(fat, i);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dfs(1);
  if (f[1] == INF)
    printf("IMPOSSIBLE\n");
  else
    printf("POSSIBLE\n");
  return 0;
}
Problem p03603 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000 + 10, K = 5000;
const int MOD = 1000000007;
const int LOG = 20;
const int INF = 10000010;

int dp[2][N][K], n, X[N], DP[N];
vi G[N];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 2; i <= n; i++) {
    int p;
    cin >> p;
    G[p].pb(i);
  }
  for (int i = 1; i <= n; i++)
    cin >> X[i];
  memset(dp, 31, sizeof dp);
  for (int i = n; i >= 1; i--) {
    bool f = 1;
    dp[0][i][0] = 0;
    for (auto u : G[i]) {
      memset(dp[f], 31, sizeof dp[f]);
      for (int j = X[i]; j >= 0; j--) {
        int x = j + X[u];
        if (x <= X[i])
          dp[f][i][x] = min(dp[f][i][x], dp[f ^ 1][i][j] + DP[u]);
        x = j + DP[u];
        if (x <= X[i])
          dp[f][i][x] = min(dp[f][i][x], dp[f ^ 1][i][j] + X[u]);
      }
      f ^= 1;
    }
    int mn = INF;
    f ^= 1;
    for (int j = 0; j <= X[i]; j++) {
      mn = min(mn, dp[f][i][j]);
    }
    if (mn == INF)
      return cout << "IMPOSSIBLE", 0;
    DP[i] = mn;
  }
  cout << "POSSIBLE";
  return 0;
}

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

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000 + 10, K = 5000;
const int MOD = 1000000007;
const int LOG = 20;
const int INF = 10000010;

int dp[2][N][K], n, X[N], DP[N];
vi G[N];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 2; i <= n; i++) {
    int p;
    cin >> p;
    G[p].pb(i);
  }
  for (int i = 1; i <= n; i++)
    cin >> X[i];
  memset(dp, 31, sizeof dp);
  for (int i = n; i >= 1; i--) {
    bool f = 1;
    dp[0][i][0] = 0;
    for (auto u : G[i]) {
      memset(dp[f][i], 31, sizeof dp[f][i]);
      for (int j = X[i]; j >= 0; j--) {
        int x = j + X[u];
        if (x <= X[i])
          dp[f][i][x] = min(dp[f][i][x], dp[f ^ 1][i][j] + DP[u]);
        x = j + DP[u];
        if (x <= X[i])
          dp[f][i][x] = min(dp[f][i][x], dp[f ^ 1][i][j] + X[u]);
      }
      f ^= 1;
    }
    int mn = INF;
    f ^= 1;
    for (int j = 0; j <= X[i]; j++) {
      mn = min(mn, dp[f][i][j]);
    }
    if (mn == INF)
      return cout << "IMPOSSIBLE", 0;
    DP[i] = mn;
  }
  cout << "POSSIBLE";
  return 0;
}
Problem p03603 (C++)

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

bool dfs(int p, const vector<vector<int>> &Cs, const vector<int> &Xs,
         vector<int> &Ys) {
  for (auto c : Cs[p]) {
    if (not dfs(c, Cs, Xs, Ys))
      return false;
  }

  int cum = 0;
  int cum_min = 0;
  vector<int> dif;
  for (auto c : Cs[p]) {
    cum += Xs[c] + Ys[c];
    cum_min += min(Xs[c], Ys[c]);
    dif.push_back(abs(Xs[c] - Ys[c]));
  }

  if (cum_min > Xs[p])
    return false;

  vector<bool> dp(Xs[p] + 1 - cum_min, false);
  dp[0] = true;
  for (auto d : dif)
    for (int i = Xs[p]; i >= d; --i)
      if (dp[i - d])
        dp[i] = true;

  for (int i = Xs[p] - cum_min; i >= 0; --i) {
    if (dp[i]) {
      Ys[p] = cum - i - cum_min;
      return true;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<int>> Cs(N, vector<int>());
  for (int i = 1; i < N; ++i) {
    int p;
    cin >> p;
    Cs[--p].push_back(i);
  }
  vector<int> Xs(N);
  for (auto &x : Xs)
    cin >> x;
  vector<int> Ys(N, 0);
  if (dfs(0, Cs, Xs, Ys)) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
  return 0;
}

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

bool dfs(int p, const vector<vector<int>> &Cs, const vector<int> &Xs,
         vector<int> &Ys) {
  for (auto c : Cs[p]) {
    if (not dfs(c, Cs, Xs, Ys))
      return false;
  }

  int cum = 0;
  int cum_min = 0;
  vector<int> dif;
  for (auto c : Cs[p]) {
    cum += Xs[c] + Ys[c];
    cum_min += min(Xs[c], Ys[c]);
    dif.push_back(abs(Xs[c] - Ys[c]));
  }

  if (cum_min > Xs[p])
    return false;

  vector<bool> dp(Xs[p] + 1 - cum_min, false);
  dp[0] = true;
  for (auto d : dif)
    for (int i = Xs[p] - cum_min; i >= d; --i)
      if (dp[i - d])
        dp[i] = true;

  for (int i = Xs[p] - cum_min; i >= 0; --i) {
    if (dp[i]) {
      Ys[p] = cum - i - cum_min;
      return true;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<int>> Cs(N, vector<int>());
  for (int i = 1; i < N; ++i) {
    int p;
    cin >> p;
    Cs[--p].push_back(i);
  }
  vector<int> Xs(N);
  for (auto &x : Xs)
    cin >> x;
  vector<int> Ys(N, 0);
  if (dfs(0, Cs, Xs, Ys)) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
  return 0;
}
Problem p03605 (C++)

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

int main() {
  int N;
  cin >> N;
  while (N) {
    if (N % 10 == 9) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  while (N) {
    if (N % 10 == 9) {
      cout << "Yes" << endl;
      return 0;
    }
    N /= 10;
  }
  cout << "No" << endl;
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  char a[100];
  for (int i = 1; i <= 100; i++) {
    cin >> a[i];
    if (a[i] == '9') {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string a;
  cin >> a;
  if (a[0] == '9' || a[1] == '9')
    cout << "Yes";
  else
    cout << "No";
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

string s;
int N, sol;
vector<int> a;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> N;

  while (N) {
    if (N % 10 == 9) {
      cout << "Yes";
      return 0;
    }
  }

  cout << "No" << '\n';
  return 0;
}


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

using namespace std;

string s;
int N, sol;
vector<int> a;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> N;

  while (N) {
    if (N % 10 == 9) {
      cout << "Yes";
      return 0;
    }

    N /= 10;
  }

  cout << "No" << '\n';
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n) {
    if (n % 10 == 9) {
      puts("Yes");
      return 0;
    }
  }
  puts("No");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n) {
    if (n % 10 == 9) {
      puts("Yes");
      return 0;
    }
    n /= 10;
  }
  puts("No");
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define PB push_back
#define MP make_pair
#define L first
#define R second

typedef pair<int, int> pie;

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  while (n > 0) {
    int x = n % 10;
    if (x == 9) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define PB push_back
#define MP make_pair
#define L first
#define R second

typedef pair<int, int> pie;

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  while (n > 0) {
    int x = n % 10;
    if (x == 9) {
      cout << "Yes" << endl;
      return 0;
    }
    n /= 10;
  }
  cout << "No" << endl;
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  unsigned long long n;
  cin >> n;
  while (n) {
    if (n % 10 == 9) {
      cout << "Yes\n";
      return 0;
    }
  }
  cout << "No\n";
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  unsigned long long n;
  cin >> n;
  while (n) {
    if (n % 10 == 9) {
      cout << "Yes\n";
      return 0;
    }
    n /= 10;
  }
  cout << "No\n";
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main(void) {
  string s;

  if (s.at(0) == '9' || s.at(1) == '9')
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main(void) {
  string s;
  cin >> s;

  if (s.at(0) == '9' || s.at(1) == '9')
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const long double PI = acos(-1.0L);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  while (n) {
    if (n % 10 == 9) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const long double PI = acos(-1.0L);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  while (n) {
    if (n % 10 == 9) {
      cout << "Yes" << endl;
      return 0;
    }
    n /= 10;
  }
  cout << "No" << endl;
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string N;
  if (N.at(0) == 9 || N.at(1) == 9) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string N;
  cin >> N;
  if (N.at(0) == '9' || N.at(1) == '9') {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int MAXN = 1000100;
const int MOD7 = 1000000007ll;
const int MOD9 = 1000000009ll;
const int INF = 2000000000;
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = {-1, 1, 0, 0};
const int dir_4c[] = {0, 0, -1, 1};
const int dir_8r[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dir_8c[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int n;
  scanf("%d", &n);
  bool flag = false;
  while (n) {
    int dig = n % 10;
    if (dig == 9) {
      flag = true;
      break;
    }
    dig /= 10;
  }
  if (flag)
    printf("Yes\n");
  else
    printf("No\n");

  // system("pause");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int MAXN = 1000100;
const int MOD7 = 1000000007ll;
const int MOD9 = 1000000009ll;
const int INF = 2000000000;
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = {-1, 1, 0, 0};
const int dir_4c[] = {0, 0, -1, 1};
const int dir_8r[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dir_8c[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int n;
  scanf("%d", &n);
  bool flag = false;
  while (n) {
    int dig = n % 10;
    if (dig == 9) {
      flag = true;
      break;
    }
    n /= 10;
  }
  if (flag)
    printf("Yes\n");
  else
    printf("No\n");

  // system("pause");
  return 0;
}
Problem p03605 (Python)

Buggy submission (status: Runtime Error):
a, b = input().split("")

if a == "9" or b == "9":
    print("Yes")
else:
    print("No")


Fixed submission:
l = list(input())
for i in l:
    if i == "9":
        print("Yes")
        break
else:
    print("No")
Problem p03605 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "iostream"
using namespace std;

int main() {
  int a;
  cin >> a;
  while (a != 0) {
    int rem = a % 10;
    if (rem == 9) {
      cout << "Yes" << endl;
      return 0;
    }
    rem /= 10;
  }
  cout << "No" << endl;
}

Fixed submission:
#include "iostream"
using namespace std;

int main() {
  int a;
  cin >> a;
  while (a != 0) {
    int rem = a % 10;
    if (rem == 9) {
      cout << "Yes" << endl;
      return 0;
    }
    a /= 10;
  }
  cout << "No" << endl;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int i, j, n, s[2][100000], sum;
  while (~scanf("%d", &n)) {
    for (j = 0; j < n; j++) {
      for (i = 0; i < 2; i++) {
        scanf("%d", &s[j][i]);
      }
    }
    for (j = 0, sum = 0; j < n; j++) {
      sum = sum + s[j][1] - s[j][0] + 1;
    }
    printf("%d\n", sum);
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int i, j, n, s[100000][2], sum;
  while (~scanf("%d", &n)) {
    for (j = 0; j < n; j++) {
      for (i = 0; i < 2; i++) {
        scanf("%d", &s[j][i]);
      }
    }
    for (j = 0, sum = 0; j < n; j++) {
      sum = sum + s[j][1] - s[j][0] + 1;
    }
    printf("%d\n", sum);
  }
  return 0;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX 100000
int main() {
  int a;
  int b[1000];
  int total = 0;

  cin >> a;

  for (int j = 0; j < a * 2; j++) {
    cin >> b[j];
    if (j % 2 == 1) {
      total += b[j] - b[j - 1] + 1;
    }
  }

  cout << total;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX 100000
int main() {
  int a;
  int b[2000];
  int total = 0;

  cin >> a;

  for (int j = 0; j < a * 2; j++) {
    cin >> b[j];
    if (j % 2 == 1) {
      total += b[j] - b[j - 1] + 1;
    }
  }

  cout << total;

  return 0;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n, l[100], r[100];
  cin >> n;
  int v = 0;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    v += r[i] - l[i] + 1;
  }

  cout << v << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n, l[1000], r[1000];
  cin >> n;
  int v = 0;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    v += r[i] - l[i] + 1;
  }

  cout << v << endl;

  return 0;
}
Problem p03606 (C++)

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

int main() {
  int N, l, r, total;
  cin >> N;
  int i = 0;
  while (i < N) {
    cin >> l >> r;
    total += r - l + 1;
  }
  cout << total << endl;
  return 0;
}


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

int main() {
  int N, l, r, total;
  cin >> N;
  int i = 0;
  while (i < N) {
    cin >> l >> r;
    total += r - l + 1;
    i++;
  }
  cout << total << endl;
  return 0;
}
Problem p03606 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, c;
  scanf("%d", &n);
  for (; scanf("%d %d", &a, &b);)
    c += b - a + 1;
  printf("%d\n", c);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, c;
  scanf("%d", &n);
  for (; scanf("%d %d", &a, &b) + 1;)
    c += b - a + 1;
  printf("%d\n", c);
  return 0;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100005];
int main() {
  scanf("%d", &n);
  while (n--) {
    int l, r;
    scanf("%d%d", &l, &r);
    for (int i = l; i <= r; i++)
      a[i] = 1;
  }
  for (int i = 1; i <= 1000000; i++)
    m += a[i];
  printf("%d\n", m);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100005];
int main() {
  scanf("%d", &n);
  while (n--) {
    int l, r;
    scanf("%d%d", &l, &r);
    for (int i = l; i <= r; i++)
      a[i] = 1;
  }
  for (int i = 1; i <= 100000; i++)
    m += a[i];
  printf("%d\n", m);
}
Problem p03606 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> l(N);
  vector<int> r(N);
  for (int i = 0; i < N; i++)
    cin >> l[i] >> r[i];

  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = l[i]; i <= r[i]; j++)
      ans++;
  }
  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> l(N);
  vector<int> r(N);
  for (int i = 0; i < N; i++)
    cin >> l[i] >> r[i];

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans += r[i] - l[i] + 1;
  }
  cout << ans << endl;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[100], b[100];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum = sum + (b[i] - a[i] + 1);
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum = sum + (b[i] - a[i] + 1);
  }
  cout << sum << endl;
  return 0;
}
Problem p03606 (C++)

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

int n;
int a[1000][2];
int sum;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i][1];
    cin >> a[i][2];
  }
  for (int i = 1; i <= n; i++) {
    sum = sum + a[i][2] - (a[i][1] - 1);
  }
  cout << sum;
}


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

int n;
int a[1005][2];
int sum;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i][1];
    cin >> a[i][2];
  }
  for (int i = 1; i <= n; i++) {
    sum = sum + a[i][2] - (a[i][1] - 1);
  }
  cout << sum;
}
Problem p03606 (C++)

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

int main() {
  int N;
  int l, r;
  cin >> N;
  int i = 0, j = 0;

  while (i < N) {
    cin >> l >> r;
    j += r - l + 1;
  }
  cout << j << endl;
}


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

int main() {
  int N;
  int l, r;
  cin >> N;
  int i = 0, j = 0;

  while (i < N) {
    cin >> l >> r;
    j += r - l + 1;
    i++;
  }
  cout << j << endl;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a;
  cin >> a;
  int ans = 0;
  vector<int> vec(1100);
  for (int i = 0; i < 2 * a; i++)
    cin >> vec.at(i);
  for (int i = 0; i < 2 * a; i += 2) {
    ans += (vec.at(i + 1) - vec.at(i) + 1);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a;
  cin >> a;
  int ans = 0;
  vector<int> vec(1100000);
  for (int i = 0; i < 2 * a; i++)
    cin >> vec.at(i);
  for (int i = 0; i < 2 * a; i += 2) {
    ans += (vec.at(i + 1) - vec.at(i) + 1);
  }
  cout << ans << endl;
}
Problem p03606 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> l, r;
  for (int i = 0; i < N; i++) {
    cin >> l.at(i);
    cin >> r.at(i);
  }
  int sum;
  sum = 0;
  int j = 0;
  while (j < N) {
    sum += r.at(j) + 1 - l.at(j);
    j++;
  }
  cout << sum << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> l(1100), r(1100);
  for (int i = 0; i < N; i++) {
    cin >> l.at(i);
    cin >> r.at(i);
  }
  int sum;
  sum = 0;
  int j = 0;
  while (j < N) {
    sum += r.at(j) + 1 - l.at(j);
    j++;
  }
  cout << sum << endl;
}
Problem p03606 (C++)

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

int main() {
  int n, l, r, x = 0;
  cin >> n;
  while (n > 0) {
    cin >> l >> r;
    x += r - l + 1;
  }
  cout << x << endl;
}

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

int main() {
  int n, l, r, x = 0;
  cin >> n;
  while (n > 0) {
    cin >> l >> r;
    x += r - l + 1;
    n--;
  }
  cout << x << endl;
}
Problem p03606 (C++)

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

int main() {
  int N, a, b;
  cin >> N;
  int i = 0;
  int c = 0;
  while (i < N) {
    cin >> a >> b;
    c += b - a + 1;
  }
  cout << c << endl;
}

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

int main() {
  int N, a, b;
  cin >> N;
  int i = 0;
  int c = 0;
  while (i < N) {
    cin >> a >> b;
    c += b - a + 1;
    i++;
  }
  cout << c << endl;
}
Problem p03606 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
seat = [0] * 10**5
for i in range(n):
    l, r = map(int, input().split())
    seat[l - 1] += 1
    seat[r] -= 1

for i in range(1, 10**5):
    seat[i] += seat[i - 1]

res = 0
for i in range(10**5):
    if seat[i] == 1:
        res += 1
print(res)


Fixed submission:
n = int(input())
seat = [0] * (10**5 + 1)
for i in range(n):
    l, r = map(int, input().split())
    seat[l - 1] += 1
    seat[r] -= 1

for i in range(1, 10**5):
    seat[i] += seat[i - 1]

res = 0
for i in range(10**5):
    if seat[i] == 1:
        res += 1
print(res)