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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MAX_N = 4020;
const int INF = 1145141919;

struct BIT {
  // [1, n]
  vector<int> bit;
  int N;
  void init(int n) {
    N = n;
    bit.resize(n, 0);
  }
  BIT(int n = 0) { init(n); }
  int sum(int i) {
    int s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, int x) {
    if (i == 0)
      return;
    while (i <= N) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int dp[MAX_N][MAX_N];

int main() {

  rep(i, MAX_N) rep(j, MAX_N) dp[i][j] = INF;
  dp[0][0] = 0;

  int N;
  cin >> N;
  BIT bit(N + 10);

  vector<int> bpos(N + 2, 0), wpos(N + 2, 0);
  for (int i = 1; i <= 2 * N; i++) {
    char c;
    int a;
    cin >> c >> a;
    if (c == 'B')
      bpos[a] = i;
    else
      wpos[a] = i;
    bit.add(i, 1);
  }

  for (int b = 0; b <= N; b++) {
    BIT tmpBit = bit;
    for (int w = 0; w <= N; w++) {
      chmin(dp[b + 1][w], dp[b][w] + tmpBit.sum(bpos[b + 1] - 1));
      chmin(dp[b][w + 1], dp[b][w] + tmpBit.sum(wpos[w + 1] - 1));
      tmpBit.add(wpos[w + 1], -1);
    }
    bit.add(bpos[b + 1], -1);
  }

  cout << dp[N][N] << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MAX_N = 4020;
const int INF = 1145141919;

struct BIT {
  // [1, n]
  vector<int> bit;
  int N;
  void init(int n) {
    N = n;
    bit.resize(n, 0);
  }
  BIT(int n = 0) { init(n); }
  int sum(int i) {
    int s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, int x) {
    if (i == 0)
      return;
    while (i <= N) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int dp[MAX_N][MAX_N];

int main() {

  rep(i, MAX_N) rep(j, MAX_N) dp[i][j] = INF;
  dp[0][0] = 0;

  int N;
  cin >> N;
  BIT bit(2 * N + 10);

  vector<int> bpos(N + 2, 0), wpos(N + 2, 0);
  for (int i = 1; i <= 2 * N; i++) {
    char c;
    int a;
    cin >> c >> a;
    if (c == 'B')
      bpos[a] = i;
    else
      wpos[a] = i;
    bit.add(i, 1);
  }

  for (int b = 0; b <= N; b++) {
    BIT tmpBit = bit;
    for (int w = 0; w <= N; w++) {
      chmin(dp[b + 1][w], dp[b][w] + tmpBit.sum(bpos[b + 1] - 1));
      chmin(dp[b][w + 1], dp[b][w] + tmpBit.sum(wpos[w + 1] - 1));
      tmpBit.add(wpos[w + 1], -1);
    }
    bit.add(bpos[b + 1], -1);
  }

  cout << dp[N][N] << endl;

  return 0;
}
Problem p03357 (C++)

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

int pos[2][1005];
int inv[2][2][1005][1005];
int S[1005][1005];

int main() {
  int n;
  cin >> n;
  for (int ni = 0; ni < n * 2; ni++) {
    char c;
    int a;
    cin >> c >> a;
    bool white = (c == 'W');
    pos[white][a] = ni;
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int ni = 1; ni <= n; ni++) {
        int cnt = 0;
        for (int nj = 1; nj <= n; nj++) {
          if (pos[i][ni] < pos[j][nj])
            cnt++;
          inv[i][j][ni][nj] = cnt;
        }
      }
    }
  }
  S[0][0] = 0;
  for (int ni = 1; ni <= n; ni++) {
    S[ni][0] = S[ni - 1][0] + inv[0][0][ni][ni - 1];
    S[0][ni] = S[0][ni - 1] + inv[1][1][ni][ni - 1];
  }
  for (int ni = 1; ni <= n; ni++) {
    for (int nj = 1; nj <= n; nj++) {
      S[ni][nj] =
          min(S[ni - 1][nj] + inv[0][0][ni][ni - 1] + inv[0][1][ni][nj],
              S[ni][nj - 1] + inv[1][1][nj][nj - 1] + inv[1][0][nj][ni]);
    }
  }
  cout << S[n][n] << endl;
}

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

int pos[2][2005];
int inv[2][2][2005][2005];
int S[2005][2005];

int main() {
  int n;
  cin >> n;
  for (int ni = 0; ni < n * 2; ni++) {
    char c;
    int a;
    cin >> c >> a;
    bool white = (c == 'W');
    pos[white][a] = ni;
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int ni = 1; ni <= n; ni++) {
        int cnt = 0;
        for (int nj = 1; nj <= n; nj++) {
          if (pos[i][ni] < pos[j][nj])
            cnt++;
          inv[i][j][ni][nj] = cnt;
        }
      }
    }
  }
  S[0][0] = 0;
  for (int ni = 1; ni <= n; ni++) {
    S[ni][0] = S[ni - 1][0] + inv[0][0][ni][ni - 1];
    S[0][ni] = S[0][ni - 1] + inv[1][1][ni][ni - 1];
  }
  for (int ni = 1; ni <= n; ni++) {
    for (int nj = 1; nj <= n; nj++) {
      S[ni][nj] =
          min(S[ni - 1][nj] + inv[0][0][ni][ni - 1] + inv[0][1][ni][nj],
              S[ni][nj - 1] + inv[1][1][nj][nj - 1] + inv[1][0][nj][ni]);
    }
  }
  cout << S[n][n] << endl;
}
Problem p03357 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int f[2001][2001], w[2001], b[2001];
int cw[2001][2001], cb[2001][2001];

int main() {
  memset(f, 0, sizeof(f));
  memset(cw, 0, sizeof(cw));
  memset(cb, 0, sizeof(cb));

  int N;
  cin >> N;
  for (int i = 1; i <= 2 * N; ++i) {
    char c;
    cin >> c;
    int a_;
    cin >> a_;
    if (c == 'W')
      w[a_] = i;
    else
      b[a_] = i;
  }

  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j < i; ++j) {
      cw[i][0] += (w[i] < w[j]);
    }
    // j以下の番号で、w[i]よりも右にあるb[k]の数をたす
    for (int j = 1; j <= N; ++j) {
      cw[i][j] = cw[i][j - 1] + (b[j] > w[i]);
    }
  }

  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j < i; ++i) {
      cb[0][i] += (b[j] > b[i]);
    }
    for (int j = 1; j <= N; ++j) {
      cb[j][i] = cb[j - 1][i] + (w[j] > b[i]);
    }
  }

  for (int i = 1; i <= N; ++i) {
    f[i][0] = f[i - 1][0] + cw[i][0];
  }
  for (int j = 1; j <= N; ++j) {
    f[0][j] = f[0][j - 1] + cb[0][j];
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= N; ++j) {
      f[i][j] = min(f[i - 1][j] + cw[i][j], f[i][j - 1] + cb[i][j]);
    }
  }

  cout << f[N][N] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int f[2001][2001], w[2001], b[2001];
int cw[2001][2001], cb[2001][2001];

int main() {
  memset(f, 0, sizeof(f));
  memset(cw, 0, sizeof(cw));
  memset(cb, 0, sizeof(cb));

  int N;
  cin >> N;
  for (int i = 1; i <= 2 * N; ++i) {
    char c;
    cin >> c;
    int a_;
    cin >> a_;
    if (c == 'W')
      w[a_] = i;
    else
      b[a_] = i;
  }

  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j < i; ++j) {
      cw[i][0] += (w[i] < w[j]);
    }
    // j以下の番号で、w[i]よりも右にあるb[k]の数をたす
    for (int j = 1; j <= N; ++j) {
      cw[i][j] = cw[i][j - 1] + (b[j] > w[i]);
    }
  }

  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j < i; ++j) {
      cb[0][i] += (b[j] > b[i]);
    }
    for (int j = 1; j <= N; ++j) {
      cb[j][i] = cb[j - 1][i] + (w[j] > b[i]);
    }
  }

  for (int i = 1; i <= N; ++i) {
    f[i][0] = f[i - 1][0] + cw[i][0];
  }
  for (int j = 1; j <= N; ++j) {
    f[0][j] = f[0][j - 1] + cb[0][j];
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= N; ++j) {
      f[i][j] = min(f[i - 1][j] + cw[i][j], f[i][j - 1] + cb[i][j]);
    }
  }

  cout << f[N][N] << endl;
  return 0;
}
Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int main() {
  int N;
  cin >> N;
  vector<pair<char, int>> A(2 * N);
  vector<int> idx_b(N), idx_w(N);
  for (int i = 0; i < 2 * N; ++i) {
    char c;
    int a;
    cin >> c >> a;
    A[i] = make_pair(c, a);
    if (c == 'B')
      idx_b[a] = i;
    else
      idx_w[a] = i;
  }

  vector<vector<int>> sum_b(N + 1, vector<int>(2 * N + 1));
  vector<vector<int>> sum_w(N + 1, vector<int>(2 * N + 1));
  for (int x = 0; x <= N; ++x) {
    for (int i = 0; i < 2 * N; ++i) {
      if (A[i].first == 'B')
        sum_b[x][i] += (A[i].second > x);
      else
        sum_w[x][i] += (A[i].second > x);
      sum_b[x][i + 1] = sum_b[x][i];
      sum_w[x][i + 1] = sum_w[x][i];
    }
  }

  vector<vector<int>> dp(N + 1, vector<int>(N + 1, INF));
  dp[0][0] = 0;

  auto f = [&](auto &&f, int b, int w) -> int {
    if (dp[b][w] != INF)
      return dp[b][w];
    if (b)
      dp[b][w] = min(dp[b][w],
                     f(f, b - 1, w) + sum_b[b][idx_b[b]] + sum_w[w][idx_b[b]]);
    if (w)
      dp[b][w] = min(dp[b][w],
                     f(f, b, w - 1) + sum_b[b][idx_w[w]] + sum_w[w][idx_w[w]]);
    return dp[b][w];
  };
  cout << f(f, N, N) << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int main() {
  int N;
  cin >> N;
  vector<pair<char, int>> A(2 * N);
  vector<int> idx_b(N + 1), idx_w(N + 1);
  for (int i = 0; i < 2 * N; ++i) {
    char c;
    int a;
    cin >> c >> a;
    A[i] = make_pair(c, a);
    if (c == 'B')
      idx_b[a] = i;
    else
      idx_w[a] = i;
  }

  vector<vector<int>> sum_b(N + 1, vector<int>(2 * N + 1));
  vector<vector<int>> sum_w(N + 1, vector<int>(2 * N + 1));
  for (int x = 0; x <= N; ++x) {
    for (int i = 0; i < 2 * N; ++i) {
      if (A[i].first == 'B')
        sum_b[x][i] += (A[i].second > x);
      else
        sum_w[x][i] += (A[i].second > x);
      sum_b[x][i + 1] = sum_b[x][i];
      sum_w[x][i + 1] = sum_w[x][i];
    }
  }

  vector<vector<int>> dp(N + 1, vector<int>(N + 1, INF));
  dp[0][0] = 0;

  auto f = [&](auto &&f, int b, int w) -> int {
    if (dp[b][w] != INF)
      return dp[b][w];
    if (b)
      dp[b][w] = min(dp[b][w],
                     f(f, b - 1, w) + sum_b[b][idx_b[b]] + sum_w[w][idx_b[b]]);
    if (w)
      dp[b][w] = min(dp[b][w],
                     f(f, b, w - 1) + sum_b[b][idx_w[w]] + sum_w[w][idx_w[w]]);
    return dp[b][w];
  };
  cout << f(f, N, N) << '\n';
  return 0;
}
Problem p03357 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
balls = []
for i in range(2 * N):
    c, a = input().split()
    balls += [(c, int(a))]

# numB[i][b]: 初期状態において、i番目の玉より右にある、1～bが書かれた黒玉の数
numB = [[0] * (N + 1) for i in range(2 * N)]
numW = [[0] * (N + 1) for i in range(2 * N)]
for i in reversed(range(1, 2 * N)):
    c, a = balls[i]
    numB[i - 1] = numB[i].copy()
    numW[i - 1] = numW[i].copy()
    if c == "B":
        for b in range(a, N + 1):
            numB[i - 1][b] += 1
    else:
        for w in range(a, N + 1):
            numW[i - 1][w] += 1

# costB[b][w]: 黒玉1～b、白玉1～wのうち、初期状態において、黒玉[b+1]より右にある玉の数
costB = [[0] * (N + 1) for i in range(N + 1)]
costW = [[0] * (N + 1) for i in range(N + 1)]
for i in reversed(range(2 * N)):
    c, a = balls[i]
    if c == "B":
        cost = numB[i][a - 1]
        for w in range(N + 1):
            costB[a - 1][w] = cost + numW[i][w]
    else:
        cost = numW[i][a - 1]
        for b in range(N + 1):
            costW[b][a - 1] = cost + numB[i][b]

dp = [[float("inf")] * (N + 1) for i in range(N + 1)]
dp[0][0] = 0
for b in range(N + 1):
    for w in range(N + 1):
        if b > 0:
            dp[b][w] = min(dp[b][w], dp[b - 1][w] + costB[b - 1][w])
        if w > 0:
            dp[b][w] = min(dp[b][w], dp[b][w - 1] + costW[b][w - 1])

print(dp[N][N])


Fixed submission:
N = int(input())
balls = []
for i in range(2 * N):
    c, a = input().split()
    balls += [(c, int(a))]

# numB[i][b]: 初期状態において、i番目の玉より右にある、1～bが書かれた黒玉の数
numB = [[0] * (N + 1) for i in range(2 * N)]
numW = [[0] * (N + 1) for i in range(2 * N)]
for i in reversed(range(1, 2 * N)):
    c, a = balls[i]
    numB[i - 1] = list(numB[i])
    numW[i - 1] = list(numW[i])
    if c == "B":
        for b in range(a, N + 1):
            numB[i - 1][b] += 1
    else:
        for w in range(a, N + 1):
            numW[i - 1][w] += 1

# costB[b][w]: 黒玉1～b、白玉1～wのうち、初期状態において、黒玉[b+1]より右にある玉の数
costB = [[0] * (N + 1) for i in range(N + 1)]
costW = [[0] * (N + 1) for i in range(N + 1)]
for i in reversed(range(2 * N)):
    c, a = balls[i]
    if c == "B":
        cost = numB[i][a - 1]
        for w in range(N + 1):
            costB[a - 1][w] = cost + numW[i][w]
    else:
        cost = numW[i][a - 1]
        for b in range(N + 1):
            costW[b][a - 1] = cost + numB[i][b]

dp = [[float("inf")] * (N + 1) for i in range(N + 1)]
dp[0][0] = 0
for b in range(N + 1):
    for w in range(N + 1):
        if b > 0:
            dp[b][w] = min(dp[b][w], dp[b - 1][w] + costB[b - 1][w])
        if w > 0:
            dp[b][w] = min(dp[b][w], dp[b][w - 1] + costW[b][w - 1])

print(dp[N][N])
Problem p03357 (C++)

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

using namespace std;
const int N = 2e3 + 10;
const int inf = 0x3f3f3f3f;

char c[N];
int a[N], n, dp[N][N], b[N][N], w[N][N], sumb[N], sumw[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * 2; i++) {
    cin >> c[i];
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n * 2; i++) {
    if (c[i] == 'B') {
      for (int j = 0; j <= n; j++) {
        b[a[i]][j] = sumb[a[i] + 1] + sumw[j + 1];
      }
      for (int j = 0; j <= a[i]; j++) {
        sumb[j] += 1;
      }
    } else {
      for (int j = 0; j <= n; j++) {
        w[j][a[i]] = sumb[j + 1] + sumw[a[i] + 1];
      }
      for (int j = 0; j <= a[i]; j++) {
        sumw[j] += 1;
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (!i && !j)
        dp[i][i] = 0;
      else
        dp[i][j] = inf;
      if (i)
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[i][j]);
      if (j)
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + w[i][j]);
    }
  }
  cout << dp[n][n] << endl;
  return 0;
}


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

using namespace std;
const int N = 4e3 + 10;
const int inf = 0x3f3f3f3f;

char c[N];
int a[N], n, dp[N][N], b[N][N], w[N][N], sumb[N], sumw[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * 2; i++) {
    cin >> c[i];
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n * 2; i++) {
    if (c[i] == 'B') {
      for (int j = 0; j <= n; j++) {
        b[a[i]][j] = sumb[a[i] + 1] + sumw[j + 1];
      }
      for (int j = 0; j <= a[i]; j++) {
        sumb[j] += 1;
      }
    } else {
      for (int j = 0; j <= n; j++) {
        w[j][a[i]] = sumb[j + 1] + sumw[a[i] + 1];
      }
      for (int j = 0; j <= a[i]; j++) {
        sumw[j] += 1;
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (!i && !j)
        dp[i][i] = 0;
      else
        dp[i][j] = inf;
      if (i)
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[i][j]);
      if (j)
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + w[i][j]);
    }
  }
  cout << dp[n][n] << endl;
  return 0;
}
Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 2010
int n, a[MAXN << 1], c[MAXN << 1], f[MAXN][MAXN], p[2][MAXN];
int g[2][MAXN][MAXN], h[2][MAXN][MAXN];
char buf[3];
inline void up(int &x, int y) {
  if (y < x)
    x = y;
}
int main() {
  scanf("%d", &n);
  n <<= 1;
  for (int i = 1; i <= n; i++) {
    scanf("%s%d", buf, a + i);
    p[c[i] = buf[0] == 'B'][a[i]] = i;
  }
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      for (int k = 1; k <= n; k++) {
        g[j][i][k] = g[j][i - 1][k] + (a[i] == k && c[i] == j);
      }
    }
  }
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      for (int k = n; k >= 1; k--) {
        g[j][i][k] += g[j][i][k + 1];
      }
    }
  }
  for (int j = 0; j < 2; j++) {
    for (int i = n; i >= 1; i--) {
      for (int k = 1; k <= n; k++) {
        h[j][i][k] = h[j][i][k - 1];
      }
    }
  }
  memset(f, 0x3F, sizeof(f));
  f[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < 2; k++) {
        int pos = p[k][(k ? j : i) + 1];
        int cost = 0;
        cost += g[0][pos - 1][i + 1];
        cost += g[1][pos - 1][j + 1];
        cost += h[0][pos + 1][i - 1];
        cost += h[1][pos + 1][j - 1];
        up(f[i + !k][j + k], f[i][j] + cost);
      }
    }
  }
  printf("%d\n", f[n][n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 4010
int n, a[MAXN], c[MAXN], f[MAXN][MAXN], p[2][MAXN];
int g[2][MAXN][MAXN], h[2][MAXN][MAXN];
char buf[3];
inline void up(int &x, int y) {
  if (y < x)
    x = y;
}
int main() {
  scanf("%d", &n);
  n <<= 1;
  for (int i = 1; i <= n; i++) {
    scanf("%s%d", buf, a + i);
    p[c[i] = buf[0] == 'B'][a[i]] = i;
  }
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      for (int k = 1; k <= n; k++) {
        g[j][i][k] = g[j][i - 1][k] + (a[i] == k && c[i] == j);
      }
    }
  }
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      for (int k = n; k >= 1; k--) {
        g[j][i][k] += g[j][i][k + 1];
      }
    }
  }
  for (int j = 0; j < 2; j++) {
    for (int i = n; i >= 1; i--) {
      for (int k = 1; k <= n; k++) {
        h[j][i][k] = h[j][i][k - 1];
      }
    }
  }
  memset(f, 0x3F, sizeof(f));
  f[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < 2; k++) {
        int pos = p[k][(k ? j : i) + 1];
        int cost = 0;
        cost += g[0][pos - 1][i + 1];
        cost += g[1][pos - 1][j + 1];
        cost += h[0][pos + 1][i - 1];
        cost += h[1][pos + 1][j - 1];
        up(f[i + !k][j + k], f[i][j] + cost);
      }
    }
  }
  printf("%d\n", f[n][n]);
  return 0;
}
Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define re register
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAXN 4000007
#define mo 19930726
using namespace std;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
inline int read() {
  int x = 0, c = 1;
  char ch = ' ';
  while ((ch > '9' || ch < '0') && ch != '-')
    ch = getchar();
  while (ch == '-')
    c *= -1, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - '0', ch = getchar();
  return x * c;
}
int n, m, a[2001], f[2005][2005], cw[4001][2002], cb[4002][2001], pos[2][2001],
    col[4001];
char s[2];
int main() {
  n = read();
  for (re int i = 1; i <= n << 1; i++) {
    scanf("%s", s);
    a[i] = read();
    col[i] = (s[0] == 'B');
    pos[col[i]][a[i]] = i;
  }
  memset(f, 100, sizeof(f));
  f[0][0] = 0;
  for (re int i = 1; i <= n << 1; i++)
    for (re int j = 0; j <= n; j++) {
      cw[i][j] = cw[i - 1][j];
      cb[i][j] = cb[i - 1][j];
      if (a[i - 1] > j)
        col[i - 1] ? cb[i][j]++ : cw[i][j]++;
    }
  for (re int i = 0; i <= n; i++) {
    for (re int j = 0; j <= n; j++) {
      int x = pos[0][i + 1], y = pos[1][j + 1];
      f[i + 1][j] = min(f[i + 1][j], f[i][j] + cw[x][i + 1] + cb[x][j]);
      f[i][j + 1] = min(f[i][j + 1], f[i][j] + cw[y][i] + cb[y][j + 1]);
    }
  }
  cout << f[n][n];
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define re register
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAXN 4000007
#define mo 19930726
using namespace std;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
inline int read() {
  int x = 0, c = 1;
  char ch = ' ';
  while ((ch > '9' || ch < '0') && ch != '-')
    ch = getchar();
  while (ch == '-')
    c *= -1, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - '0', ch = getchar();
  return x * c;
}
int n, m, a[4001], f[2005][2005], cw[4001][2002], cb[4002][2001], pos[2][2001],
    col[4001];
char s[2];
int main() {
  n = read();
  for (re int i = 1; i <= n << 1; i++) {
    scanf("%s", s);
    a[i] = read();
    col[i] = (s[0] == 'B');
    pos[col[i]][a[i]] = i;
  }
  memset(f, 100, sizeof(f));
  f[0][0] = 0;
  for (re int i = 1; i <= n << 1; i++)
    for (re int j = 0; j <= n; j++) {
      cw[i][j] = cw[i - 1][j];
      cb[i][j] = cb[i - 1][j];
      if (a[i - 1] > j)
        col[i - 1] ? cb[i][j]++ : cw[i][j]++;
    }
  for (re int i = 0; i <= n; i++) {
    for (re int j = 0; j <= n; j++) {
      int x = pos[0][i + 1], y = pos[1][j + 1];
      f[i + 1][j] = min(f[i + 1][j], f[i][j] + cw[x][i + 1] + cb[x][j]);
      f[i][j + 1] = min(f[i][j + 1], f[i][j] + cw[y][i] + cb[y][j + 1]);
    }
  }
  cout << f[n][n];
}
Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
inline int read() {
  int Res = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    f = (ch == '-' ? -f : f), ch = getchar();
  while (ch >= '0' && ch <= '9')
    Res = Res * 10 + ch - '0', ch = getchar();
  return Res * f;
}
int N;
int A[2005], B[2005], W[2005], Col[4005];
int F[2005][2005], NB[4005][2005], NW[4005][2005];
int main() {
  N = read();
  for (int i = 1; i <= 2 * N; i++) {
    char S[5];
    scanf("%s", S);
    Col[i] = S[0] == 'B' ? 1 : 0;
    A[i] = read();
    if (S[0] == 'B')
      B[A[i]] = i;
    else
      W[A[i]] = i;
  }
  for (int i = 1; i <= 2 * N; i++) {
    for (int j = 0; j <= N; j++) {
      NB[i][j] = NB[i - 1][j] + (A[i - 1] > j && Col[i - 1] == 1);
      NW[i][j] = NW[i - 1][j] + (A[i - 1] > j && Col[i - 1] == 0);
    }
  }
  memset(F, 63, sizeof F);
  F[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      if (i)
        F[i][j] = min(F[i][j], F[i - 1][j] + NB[B[i]][i - 1] + NW[B[i]][j]);
      if (j)
        F[i][j] = min(F[i][j], F[i][j - 1] + NB[W[j]][i] + NW[W[j]][j - 1]);
    }
  }
  printf("%d\n", F[N][N]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
inline int read() {
  int Res = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    f = (ch == '-' ? -f : f), ch = getchar();
  while (ch >= '0' && ch <= '9')
    Res = Res * 10 + ch - '0', ch = getchar();
  return Res * f;
}
int N;
int A[4005], B[2005], W[2005], Col[4005];
int F[2005][2005], NB[4005][2005], NW[4005][2005];
int main() {
  N = read();
  for (int i = 1; i <= 2 * N; i++) {
    char S[5];
    scanf("%s", S);
    Col[i] = S[0] == 'B' ? 1 : 0;
    A[i] = read();
    if (S[0] == 'B')
      B[A[i]] = i;
    else
      W[A[i]] = i;
  }
  for (int i = 1; i <= 2 * N; i++) {
    for (int j = 0; j <= N; j++) {
      NB[i][j] = NB[i - 1][j] + (A[i - 1] > j && Col[i - 1] == 1);
      NW[i][j] = NW[i - 1][j] + (A[i - 1] > j && Col[i - 1] == 0);
    }
  }
  memset(F, 63, sizeof F);
  F[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      if (i)
        F[i][j] = min(F[i][j], F[i - 1][j] + NB[B[i]][i - 1] + NW[B[i]][j]);
      if (j)
        F[i][j] = min(F[i][j], F[i][j - 1] + NB[W[j]][i] + NW[W[j]][j - 1]);
    }
  }
  printf("%d\n", F[N][N]);
  return 0;
}
Problem p03357 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REP(i, m, n) for (ll i = m; i < (ll)(n); i++)
typedef pair<int, long long> pint;
typedef long long ll;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int cnt[2][4040][2020], dp[2020][2020], a[2][2020];

int rec(int b, int w) {
  if (dp[b][w] != -1)
    return dp[b][w];
  if (b == 0 && w == 0)
    return 0;
  int ret = inf;
  if (b > 0)
    ret = min(ret, rec(b - 1, w) + a[0][b] + cnt[0][a[0][b]][b - 1] +
                       cnt[1][a[0][b]][w] - b - w);
  if (w > 0)
    ret = min(ret, rec(b, w - 1) + a[1][w] + cnt[0][a[1][w]][b] +
                       cnt[1][a[1][w]][w - 1] - b - w);
  return ret;
}

int main() {
  int n;
  cin >> n;
  rep(i, 2 * n) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    if (s == "W")
      a[1][x] = i + 1;
    else
      a[0][x] = i + 1;
  }
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = -1;
  rep(i, 2 * n) rep(j, n) {
    if (i + 1 < a[0][j + 1])
      cnt[0][i + 1][j + 1]++;
    if (i + 1 < a[1][j + 1])
      cnt[1][i + 1][j + 1]++;
  }
  rep(i, 2 * n) rep(j, n - 1) {
    cnt[0][i + 1][j + 2] += cnt[0][i + 1][j + 1];
    cnt[1][i + 1][j + 2] += cnt[1][i + 1][j + 1];
  }
  cout << rec(n, n) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REP(i, m, n) for (ll i = m; i < (ll)(n); i++)
typedef pair<int, long long> pint;
typedef long long ll;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int cnt[2][4040][2020], dp[2020][2020], a[2][2020];

int rec(int b, int w) {
  if (dp[b][w] != -1)
    return dp[b][w];
  if (b == 0 && w == 0)
    return 0;
  int ret = inf;
  if (b > 0)
    ret = min(ret, rec(b - 1, w) + a[0][b] + cnt[0][a[0][b]][b - 1] +
                       cnt[1][a[0][b]][w] - b - w);
  if (w > 0)
    ret = min(ret, rec(b, w - 1) + a[1][w] + cnt[0][a[1][w]][b] +
                       cnt[1][a[1][w]][w - 1] - b - w);
  return dp[b][w] = ret;
}

int main() {
  int n;
  cin >> n;
  rep(i, 2 * n) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    if (s == "W")
      a[1][x] = i + 1;
    else
      a[0][x] = i + 1;
  }
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = -1;
  rep(i, 2 * n) rep(j, n) {
    if (i + 1 < a[0][j + 1])
      cnt[0][i + 1][j + 1]++;
    if (i + 1 < a[1][j + 1])
      cnt[1][i + 1][j + 1]++;
  }
  rep(i, 2 * n) rep(j, n - 1) {
    cnt[0][i + 1][j + 2] += cnt[0][i + 1][j + 1];
    cnt[1][i + 1][j + 2] += cnt[1][i + 1][j + 1];
  }
  cout << rec(n, n) << endl;
  return 0;
}
Problem p03359 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
int main() {
  int mon[13][31];
  int sum = 0;
  for (int i = 1; i <= 12; i++)
    for (int j = 1; j <= 31; j++) {
      if ((i <= 7 && i % 2 == 0) && j == 31)
        break;
      if (i == 2 && j == 29)
        break;
      if (i >= 8 && i % 2 == 1 && j == 31)
        break;
      if (i == j)
        sum++;
      mon[i][j] = sum;
    }
  int a, b;
  while (scanf("%d%d", &a, &b) != EOF) {
    printf("%d\n", mon[a][b]);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
using namespace std;
int main() {
  int mon[13][31];
  int sum = 0;
  for (int i = 1; i <= 12; i++)
    for (int j = 1; j <= 31; j++) {
      if ((i <= 7 && i % 2 == 0) && j == 31)
        break;
      if (i == 2 && j == 29)
        break;
      if (i >= 8 && i % 2 == 1 && j == 31)
        break;
      if (i == j)
        sum++;
      mon[i][j] = sum;
    }
  int a, b;
  scanf("%d%d", &a, &b);
  { printf("%d\n", mon[a][b]); }
  return 0;
}
Problem p03359 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES // for C++
#include "bits/stdc++.h"
#define NUM ((long long)1000000007)
#define int long long

using namespace std;

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

  int a, b;
  cin >> a >> b;
  for (size_t i = 12; i > 0; i--) {
    if (a >= i && b >= i) {
      cout << i << endl;
      return 1;
    }
  }
}

Fixed submission:
#define _USE_MATH_DEFINES // for C++
#include "bits/stdc++.h"
#define NUM ((long long)1000000007)
#define int long long

using namespace std;

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

  int a, b;
  cin >> a >> b;
  if (a <= b) {
    cout << a << endl;
  } else {
    cout << a - 1 << endl;
  }
}
Problem p03359 (Python)

Buggy submission (status: Runtime Error):
a, b = int(input().split())
print(a if a <= b else a - 1)


Fixed submission:
a, b = map(int, input().split())
print(a if a <= b else a - 1)
Problem p03359 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INF 1145141919
typedef long long int ll;
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  if (a == 1) {
    cout << 1 << endl;
    exit(1);
  }
  if (a > b)
    cout << a - 1 << endl;
  else
    cout << a << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INF 1145141919
typedef long long int ll;
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  if (a == 1) {
    cout << 1 << endl;
    exit(0);
  }
  if (a > b)
    cout << a - 1 << endl;
  else
    cout << a << endl;
  return 0;
}
Problem p03359 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define Rint register int
#define mem(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
template <typename T> inline void read(T &x) {
  x = 0;
  T w = 1, ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar();
  x = x * w;
}
inline void File() {
  freopen("fuck.in", "r", stdin);
  freopen("fuck.out", "w", stdout);
}

int a, b;
int ans;

int main() {
  File();
  read(a);
  read(b);
  ans += a;
  if (b < a)
    ans--;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define Rint register int
#define mem(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
template <typename T> inline void read(T &x) {
  x = 0;
  T w = 1, ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar();
  x = x * w;
}
inline void File() {
  freopen("fuck.in", "r", stdin);
  freopen("fuck.out", "w", stdout);
}

int a, b;
int ans;

int main() {
  // File();
  read(a);
  read(b);
  ans += a;
  if (b < a)
    ans--;
  printf("%d\n", ans);
  return 0;
}
Problem p03360 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef queue<P> Q;

#define _CRT_SECURE_NO_WARNINGS

int a[3];
int n;
int main() {
  scanf("%d%d%d%d", a, a + 1, a + 2, n);
  sort(a, a + 2);

  printf("%d\n", a[2] * (1 << n) + a[1] + a[0]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef queue<P> Q;

#define _CRT_SECURE_NO_WARNINGS

int a[3];
int n;
int main() {
  scanf("%d%d%d%d", a, a + 1, a + 2, &n);
  sort(a, a + 3);

  printf("%d\n", a[2] * (1 << n) + a[1] + a[0]);
  return 0;
}
Problem p03360 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int A[3], K, i, max;
  scanf("%d%d%d%d", A[0], A[1], A[2], K);
  max = 0;
  if (A[max] < A[1]) {
    max = 1;
  }
  if (A[max] < A[2]) {
    max = 2;
  }
  for (i = 0; i < K; i++) {
    A[max] = A[max] * 2;
  }
  printf("%d", A[0] + A[1] + A[2]);
  return 0;
}


Fixed submission:
#include <stdio.h>
int main() {
  int A[3], K, i, max;
  scanf("%d%d%d%d", &A[0], &A[1], &A[2], &K);
  max = 0;
  if (A[max] < A[1]) {
    max = 1;
  }
  if (A[max] < A[2]) {
    max = 2;
  }
  for (i = 0; i < K; i++) {
    A[max] = A[max] * 2;
  }
  printf("%d", A[0] + A[1] + A[2]);
  return 0;
}
Problem p03360 (Python)

Buggy submission (status: Runtime Error):
a, b, c = map(int, input().split())
k = int(input())

m = max(a, b, c)
ans = sum(a, b, c) - m + (2**k) * m

print(ans)


Fixed submission:
a, b, c = map(int, input().split())
k = int(input())

m = max(a, b, c)
ans = a + b + c - m + (2**k) * m

print(ans)
Problem p03360 (Python)

Buggy submission (status: Runtime Error):
A, B, C = map(int, input().split())
K = int(input())
result = sum(A, B, C)
t = max(A, B, C)
result += (t << K) - t
print(result)


Fixed submission:
A, B, C = map(int, input().split())
K = int(input())
result = sum([A, B, C])
t = max(A, B, C)
result += (t << K) - t
print(result)
Problem p03360 (C++)

Buggy submission (status: Runtime Error):

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

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

  vi v;
  REP(i, 3) cin >> v[i];

  sort(ALL(v), greater<>());

  int k;
  cin >> k;

  print(v[0] * (1 << k) + v[1] + v[2]);
}


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

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

  vi v(3);
  REP(i, 3) cin >> v[i];

  sort(ALL(v), greater<>());

  int k;
  cin >> k;

  print(v[0] * (1 << k) + v[1] + v[2]);
}
Problem p03361 (C++)

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

const string YES = "Yes";
const string NO = "No";

void solve(long long H, long long W, std::vector<std::string> s) {
  string ans = YES;
  rep(i, H) {
    rep(j, W) {
      if (s[i][j] == '#') {

        int count = 0;
        if (0 <= j - 1) {
          if (s[i][j - 1] == '#') {
            count++;
          }
        }
        if (W > j + 1) {
          if (s[i][j + 1] == '#') {
            count++;
          }
        }
        if (0 <= i - 1) {
          if (s[i - 1][j] == '#') {
            count++;
          }
        }
        if (H > i + 1) {
          if (s[i + 1][j] == '#') {
            count++;
          }
        }
        if (count == 0) {
          ans = NO;
        }
      }
    }
  }
  cout << ans << endl;
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  std::vector<std::string> s(W);
  for (int i = 0; i < W; i++) {
    std::cin >> s[i];
  }
  solve(H, W, std::move(s));
  return 0;
}


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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

const string YES = "Yes";
const string NO = "No";

void solve(long long H, long long W, std::vector<std::string> s) {
  string ans = YES;
  rep(i, H) {
    rep(j, W) {
      if (s[i][j] == '#') {

        int count = 0;
        if (0 <= j - 1) {
          if (s[i][j - 1] == '#') {
            count++;
          }
        }
        if (W > j + 1) {
          if (s[i][j + 1] == '#') {
            count++;
          }
        }
        if (0 <= i - 1) {
          if (s[i - 1][j] == '#') {
            count++;
          }
        }
        if (H > i + 1) {
          if (s[i + 1][j] == '#') {
            count++;
          }
        }
        if (count == 0) {
          ans = NO;
        }
      }
    }
  }
  cout << ans << endl;
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  std::vector<std::string> s(H);
  for (int i = 0; i < H; i++) {
    std::cin >> s[i];
  }
  solve(H, W, std::move(s));
  return 0;
}
Problem p03361 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> s(H + 2, "");
  rep(i, W + 2) {
    s[0] += '.';
    s[H + 1] += '.';
  }
  for (int i = 1; i <= H; ++i) {
    string str;
    cin >> str;
    s[i].append(".");
    s[i].append(str);
    s[i].append(".");
  }
  bool flag = true;
  for (int i = 1; i < W + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      if (s[i][j] == '#' && ((s[i - 1][j] == '.' && s[i + 1][j] == '.') &&
                             (s[i][j + 1] == '.' && s[i][j - 1] == '.')))
        flag = false;
    }
  }
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> s(H + 2, "");
  rep(i, W + 2) {
    s[0] += '.';
    s[H + 1] += '.';
  }
  for (int i = 1; i <= H; ++i) {
    string str;
    cin >> str;
    s[i].append(".");
    s[i].append(str);
    s[i].append(".");
  }
  bool flag = true;
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      if (s[i][j] == '#' && ((s[i - 1][j] == '.' && s[i + 1][j] == '.') &&
                             (s[i][j + 1] == '.' && s[i][j - 1] == '.')))
        flag = false;
    }
  }
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03361 (C++)

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

int H, W;
vector<string> grid;
const int nx[4] = {0, 0, 1, -1};
const int ny[4] = {1, -1, 0, 0};

int main() {
  cin >> H >> W;
  grid.resize(H);
  for (int i = 0; i < H; i++)
    cin >> grid[i];
  const int conditx[4] = {-1, -1, W - 1, 0};
  const int condity[4] = {H - 1, 0, -1, -1};
  int flag = 1;
  for (int x = 0; x < W && flag; x++) {
    for (int y = 0; y < H && flag; y++) {
      if (grid[x][y] == '.')
        continue;
      int tempflag = 0;
      for (int i = 0; i < 4; i++) {
        if (!tempflag && x != conditx[i] && y != condity[i] &&
            grid[y + ny[i]][x + nx[i]] == '#')
          tempflag = 1;
      }
      flag = tempflag;
    }
  }
  string ans = flag ? "Yes" : "No";
  cout << ans << endl;
}

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

int H, W;
vector<string> grid;
const int nx[4] = {0, 0, 1, -1};
const int ny[4] = {1, -1, 0, 0};

int main() {
  cin >> H >> W;
  grid.resize(H);
  for (int i = 0; i < H; i++)
    cin >> grid[i];
  const int conditx[4] = {-1, -1, W - 1, 0};
  const int condity[4] = {H - 1, 0, -1, -1};
  int flag = 1;
  for (int x = 0; x < W && flag; x++) {
    for (int y = 0; y < H && flag; y++) {
      if (grid[y][x] == '.')
        continue;
      int tempflag = 0;
      for (int i = 0; i < 4; i++) {
        if (!tempflag && x != conditx[i] && y != condity[i] &&
            grid[y + ny[i]][x + nx[i]] == '#')
          tempflag = 1;
      }
      flag = tempflag;
    }
  }
  string ans = flag ? "Yes" : "No";
  cout << ans << endl;
}
Problem p03361 (C++)

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

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> s(H + 2);
  for (int i = 1; i <= H; i++)
    cin >> s[i];

  s[0] = "";
  s[H + 1] = "";
  for (int i = 0; i <= W + 1; i++) {
    s[0] += '.';
    s[H + 1] += '.';
  }

  for (int i = 1; i <= W; i++) {
    s[i] = '.' + s[i] + '.';
  }

  bool ans_flag = 1;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (s[i][j] == '#' && s[i][j - 1] == '.' && s[i][j + 1] == '.' &&
          s[i - 1][j] == '.' && s[i + 1][j] == '.') {
        ans_flag = 0;
        break;
      }
    }
  }

  if (ans_flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> s(H + 2);
  for (int i = 1; i <= H; i++)
    cin >> s[i];

  s[0] = "";
  s[H + 1] = "";
  for (int i = 0; i <= W + 1; i++) {
    s[0] += '.';
    s[H + 1] += '.';
  }

  for (int i = 1; i <= H; i++) {
    s[i] = '.' + s[i] + '.';
  }

  bool ans_flag = 1;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (s[i][j] == '#' && s[i][j - 1] == '.' && s[i][j + 1] == '.' &&
          s[i - 1][j] == '.' && s[i + 1][j] == '.') {
        ans_flag = 0;
        break;
      }
    }
  }

  if (ans_flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p03361 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define mer make_pair
#define fi first
#define se second
using namespace std;

int n, m;
char ch[555][555];
bool bol[555][555];
int w[7][5];

bool ok(int a, int b) {
  if (a <= n && a > 0 && b <= m && b > 0)
    return true;
}

int dfs(int x, int y) {
  if (ch[x][y] == '.' || !ok(x, y) || bol[x][y])
    return 0;
  bol[x][y] = true;
  int sum = 1;
  for (int i = 1; i <= 4; i++) {
    int xx = x + w[i][1];
    int yy = y + w[i][2];
    sum += dfs(xx, yy);
  }
  return sum;
}

int main() {
  // my new name is Veex
  // clock_t start1=clock();
  w[1][1] = 0;
  w[1][2] = 1;
  w[2][1] = 0;
  w[2][2] = -1;
  w[3][1] = 1;
  w[3][2] = 0;
  w[4][1] = -1;
  w[4][2] = 0;

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string cs;
    cin >> cs;
    for (int j = 0; j < m; j++) {
      ch[i][j + 1] = cs[j];
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '#' && !bol[i][j]) {
        if (dfs(i, j) == 1) {
          cout << "No";
          return 0;
        }
      }
    }
  cout << "Yes";
  //((double)(clock()-start1)/CLOCKS_PER_SEC)*1000;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define mer make_pair
#define fi first
#define se second
using namespace std;

int n, m;
char ch[555][555];
bool bol[555][555];
int w[7][5];

bool ok(int a, int b) {
  if (a <= n && a > 0 && b <= m && b > 0)
    return true;
  return false;
}

int dfs(int x, int y) {
  if (ch[x][y] == '.' || !ok(x, y) || bol[x][y])
    return 0;
  bol[x][y] = true;
  int sum = 1;
  for (int i = 1; i <= 4; i++) {
    int xx = x + w[i][1];
    int yy = y + w[i][2];
    sum += dfs(xx, yy);
  }
  return sum;
}

int main() {
  // my new name is Veex
  // clock_t start1=clock();
  w[1][1] = 0;
  w[1][2] = 1;
  w[2][1] = 0;
  w[2][2] = -1;
  w[3][1] = 1;
  w[3][2] = 0;
  w[4][1] = -1;
  w[4][2] = 0;

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string cs;
    cin >> cs;
    for (int j = 0; j < m; j++) {
      ch[i][j + 1] = cs[j];
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '#' && !bol[i][j]) {
        if (dfs(i, j) == 1) {
          cout << "No";
          return 0;
        }
      }
    }
  cout << "Yes";
  //((double)(clock()-start1)/CLOCKS_PER_SEC)*1000;
  return 0;
}
Problem p03361 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
using namespace std;
int n, m;
char a[100][100];
int bio[100][100];
int r, s;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
queue<pair<int, int>> q;

void bfs(int r, int s) {
  while (!q.empty()) {
    r = q.front().first;
    s = q.front().second;
    //	cout << r << " " << s << endl;
    q.pop();
    bio[r][s] = 1;
    for (int i = 0; i < 4; i++) {
      if (a[r + dx[i]][s + dy[i]] == '#') {
        if (bio[r + dx[i]][s + dy[i]] == 0)
          q.push(make_pair(r + dx[i], s + dy[i]));
      }
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        r = i, s = j;
    }
  }
  q.push(make_pair(r, s));
  bfs(r, s);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '#') {
        int l = 0;
        for (int x = 0; x < 4; x++) {
          if (a[i + dx[x]][j + dy[x]] == '#') {
            l = 1;
          }
        }
        if (l == 0) {
          cout << "No";
          return 0;
        }
      }
    }
    //	cout << endl;
  }
  cout << "Yes";

  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
using namespace std;
int n, m;
char a[100][100];
int bio[100][100];
int r, s;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
queue<pair<int, int>> q;

void bfs(int r, int s) {
  while (!q.empty()) {
    r = q.front().first;
    s = q.front().second;
    //	cout << r << " " << s << endl;
    q.pop();
    bio[r][s] = 1;
    for (int i = 0; i < 4; i++) {
      if (a[r + dx[i]][s + dy[i]] == '#') {
        if (bio[r + dx[i]][s + dy[i]] == 0)
          q.push(make_pair(r + dx[i], s + dy[i]));
      }
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        r = i, s = j;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '#') {
        int l = 0;
        for (int x = 0; x < 4; x++) {
          if (a[i + dx[x]][j + dy[x]] == '#') {
            l = 1;
          }
        }
        if (l == 0) {
          cout << "No";
          return 0;
        }
      }
    }
    //	cout << endl;
  }
  cout << "Yes";

  return 0;
}