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

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <map>

using namespace std;

int main() {
  for (int N; cin >> N, N;) {
    int X[51234], R[51234];
    for (int i = 0; i < N; i++) {
      cin >> X[i] >> R[i];
    }
    double l = 0, h = 1e9;
    for (int i = 0; i < 88; i++) {
      double m = (l + h) / 2;
      map<double, int> mp;
      for (int j = 0; j < N; j++) {
        double inr = 1. * R[j] * R[j] - m * m;
        if (inr >= 0) {
          mp[-sqrt(inr) + X[j]]++;
          mp[sqrt(inr) + X[j]]--;
        }
      }
      int c = 0;
      double start;
      bool f = false;
      for (auto e : mp) {
        if (c == 0) {
          start = e.first;
        }
        c += e.second;
        if (c == 0) {
          f |= e.first - start >= m * 2;
        }
      }
      (f ? l : h) = m;
    }
    cout << fixed << l * 2 << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <map>

using namespace std;

int main() {
  for (int N; cin >> N, N;) {
    int X[51234], R[51234];
    for (int i = 0; i < N; i++) {
      cin >> X[i] >> R[i];
    }
    double l = 0, h = 1e9;
    for (int i = 0; i < 66; i++) {
      double m = (l + h) / 2;
      map<double, int> mp;
      for (int j = 0; j < N; j++) {
        double inr = 1. * R[j] * R[j] - m * m;
        if (inr >= 0) {
          mp[-sqrt(inr) + X[j]]++;
          mp[sqrt(inr) + X[j]]--;
        }
      }
      int c = 0;
      double start;
      bool f = false;
      for (auto e : mp) {
        if (c == 0) {
          start = e.first;
        }
        c += e.second;
        if (c == 0) {
          f |= e.first - start >= m * 2;
        }
      }
      (f ? l : h) = m;
    }
    cout << fixed << l * 2 << endl;
  }
}
Problem p01707 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void solve(int d, int x, vector<long long> &dp) {
  if (d == 1) {
    for (int i = 0; i < dp.size() && i < x; i++)
      dp[i] = 1;
    return;
  }
  vector<long long> A(dp.size());
  if (d & 1) {
    solve(d - 1, x, A);
    for (int i = 0; i < x; i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j];
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  } else {
    solve(d >> 1, x, A);
    for (int i = 0; i < dp.size(); i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j] * A[i] % mod;
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  }
}

int main() {
  int x, n;
  long long d;
  while (~scanf("%d%lld%d", &n, &d, &x)) {
    if (!n && !d && !x)
      break;
    vector<long long> dp(n + 1);
    solve(d, x, dp);
    printf("%lld\n", dp[n]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void solve(long long d, int x, vector<long long> &dp) {
  if (d == 1) {
    for (int i = 0; i < dp.size() && i < x; i++)
      dp[i] = 1;
    return;
  }
  vector<long long> A(dp.size());
  if (d & 1) {
    solve(d - 1, x, A);
    for (int i = 0; i < x; i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j];
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  } else {
    solve(d >> 1, x, A);
    for (int i = 0; i < dp.size(); i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j] * A[i] % mod;
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  }
}

int main() {
  int x, n;
  long long d;
  while (~scanf("%d%lld%d", &n, &d, &x)) {
    if (!n && !d && !x)
      break;
    vector<long long> dp(n + 1);
    solve(d, x, dp);
    printf("%lld\n", dp[n]);
  }
  return 0;
}
Problem p01707 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
const int MOD = 1000000007;
typedef long long ll;
ll N, D, X;
typedef vector<int> Vec;
typedef vector<Vec> Mat;
void mat_mul(Vec &A, const Vec &B) {
  const int n = A.size();
  Vec col(n);
  REP(j, n) {
    ll s = 0;
    REP(k, n) {
      ll a = (j - k >= 0) ? A[j - k] : 0;
      ll b = (k - 0 >= 0) ? B[k - 0] : 0;
      s += a * b;
      s %= MOD;
    }
    col[j] = s;
  }
  A = move(col);
}
Mat mat_pow(Mat A, ll n) {
  Vec col(A.size(), 0);
  col[0] = 1;
  Vec a(A.size());
  REP(i, A.size()) a[i] = A[i][0];
  for (; n > 0; n >>= 1) {
    if (n & 1)
      mat_mul(col, a);
    mat_mul(a, a);
  }
  Mat res(A.size(), Vec(A.size(), 0));
  REP(i, A.size()) REP(j, A.size()) {
    if (i - j >= 0)
      res[i][j] = col[i - j];
  }
  return res;
}
void mat_print(Mat A) {
  const int n = A.size();
  REP(i, n) {
    REP(j, n) { cerr << ' ' << A[i][j]; }
    cerr << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0)
      break;
    Mat A(N + 1, Vec(N + 1, 0));
    REP(i, N + 1) {
      for (int j = max<int>(i - X + 1, 0); j <= i; ++j) {
        A[i][j] = 1;
      }
    }
    Mat B = mat_pow(A, D);
    cout << B[N][0] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
const int MOD = 1000000007;
typedef long long ll;
ll N, D, X;
typedef vector<int> Vec;
typedef vector<Vec> Mat;
void mat_mul(Vec &A, const Vec &B) {
  const int n = A.size();
  Vec col(n);
  REP(j, n) {
    ll s = 0;
    for (int k = 0; k <= j; ++k) {
      ll a = A[j - k];
      ll b = B[k];
      s += a * b;
      s %= MOD;
    }
    col[j] = s;
  }
  A = move(col);
}
Mat mat_pow(Mat A, ll n) {
  Vec col(A.size(), 0);
  col[0] = 1;
  Vec a(A.size());
  REP(i, A.size()) a[i] = A[i][0];
  for (; n > 0; n >>= 1) {
    if (n & 1)
      mat_mul(col, a);
    mat_mul(a, a);
  }
  Mat res(A.size(), Vec(A.size(), 0));
  REP(i, A.size()) REP(j, A.size()) {
    if (i - j >= 0)
      res[i][j] = col[i - j];
  }
  return res;
}
void mat_print(Mat A) {
  const int n = A.size();
  REP(i, n) {
    REP(j, n) { cerr << ' ' << A[i][j]; }
    cerr << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0)
      break;
    Mat A(N + 1, Vec(N + 1, 0));
    REP(i, N + 1) {
      for (int j = max<int>(i - X + 1, 0); j <= i; ++j) {
        A[i][j] = 1;
      }
    }
    Mat B = mat_pow(A, D);
    cout << B[N][0] << endl;
  }
}
Problem p01712 (C++)

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

int main() {
  int N, W, H;
  cin >> N >> W >> H;

  int vertical[W + 1];
  int horizon[H + 1];
  for (int i = 0; i < W + 1; i++) {
    vertical[i] = 0;
  }
  for (int i = 0; i < H + 1; i++) {
    horizon[i] = 0;
  }

  for (int times = 0; times < N; times++) {
    int x, y, w;
    cin >> x >> y >> w;
    int temp;
    temp = (x - w >= 0) ? x - w : 0;
    vertical[temp]++;
    temp = (x + w <= W) ? x + w : W;
    vertical[temp]--;

    temp = (y - w >= 0) ? y - w : 0;
    horizon[temp]++;
    temp = (y + w <= H) ? y + w : H;
    vertical[temp]--;
  }

  for (int i = 1; i < W + 1; i++) {
    vertical[i] += vertical[i - 1];
  }
  for (int i = 1; i < H + 1; i++) {
    horizon[i] += horizon[i - 1];
  }
  bool flagv = true;
  bool flagh = true;
  for (int i = 0; i < W; i++) {
    if (vertical[i] == 0) {
      flagv = false;
      break;
    }
  }
  for (int i = 0; i < H; i++) {
    if (horizon[i] == 0) {
      flagh = false;
      break;
    }
  }

  if (!flagv && !flagh)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

  return 0;
}

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

int main() {
  int N, W, H;
  cin >> N >> W >> H;

  int vertical[W + 1];
  int horizon[H + 1];
  for (int i = 0; i < W + 1; i++) {
    vertical[i] = 0;
  }
  for (int i = 0; i < H + 1; i++) {
    horizon[i] = 0;
  }

  for (int times = 0; times < N; times++) {
    int x, y, w;
    cin >> x >> y >> w;
    int temp;
    temp = (x - w >= 0) ? x - w : 0;
    vertical[temp]++;
    temp = (x + w <= W) ? x + w : W;
    vertical[temp]--;

    temp = (y - w >= 0) ? y - w : 0;
    horizon[temp]++;
    temp = (y + w <= H) ? y + w : H;
    horizon[temp]--;
  }

  for (int i = 1; i < W + 1; i++) {
    vertical[i] += vertical[i - 1];
  }
  for (int i = 1; i < H + 1; i++) {
    horizon[i] += horizon[i - 1];
  }
  bool flagv = true;
  bool flagh = true;
  for (int i = 0; i < W; i++) {
    if (vertical[i] == 0) {
      flagv = false;
      break;
    }
  }
  for (int i = 0; i < H; i++) {
    if (horizon[i] == 0) {
      flagh = false;
      break;
    }
  }

  if (!flagv && !flagh)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

  return 0;
}
Problem p01712 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int X[10005], Y[100005];

int main() {
  int n, W, H;
  cin >> n >> W >> H;

  memset(X, 0, sizeof(X));
  memset(Y, 0, sizeof(Y));

  rep(i, n) {
    int x, y, w;
    cin >> x >> y >> w;

    X[max(0, x - w)]++;
    X[min(W, x + w)]--;

    Y[max(0, y - w)]++;
    Y[min(H, y + w)]--;
  }

  REP(i, 1, 100005) {
    X[i] += X[i - 1];
    Y[i] += Y[i - 1];
  }

  bool a = true, b = true;
  REP(i, 0, W) {
    if (X[i] == 0)
      a = false;
  }

  REP(i, 0, H) {
    if (Y[i] == 0)
      b = false;
  }

  if (a || b)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int X[100005], Y[100005];

int main() {
  int n, W, H;
  cin >> n >> W >> H;

  memset(X, 0, sizeof(X));
  memset(Y, 0, sizeof(Y));

  rep(i, n) {
    int x, y, w;
    cin >> x >> y >> w;

    X[max(0, x - w)]++;
    X[min(W, x + w)]--;

    Y[max(0, y - w)]++;
    Y[min(H, y + w)]--;
  }

  REP(i, 1, 100005) {
    X[i] += X[i - 1];
    Y[i] += Y[i - 1];
  }

  bool a = true, b = true;
  REP(i, 0, W) {
    if (X[i] == 0)
      a = false;
  }

  REP(i, 0, H) {
    if (Y[i] == 0)
      b = false;
  }

  if (a || b)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p01712 (C++)

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

class S {
public:
  S(int length) : length(length) {
    part.insert(make_pair(length + 1, length + 1));
  }
  void add_ap(int pos, int power) {
    int start = max(pos - power, 0), end = min(pos + power, length);
    auto it = part.lower_bound(start);
    if (it != part.begin())
      it++;
    while (true) {
      if (end < it->first) { // (A)(B)
        part.insert(make_pair(start, end));
        break;
      } else if (start < it->first) {
        if (end <= it->second) { // (A()B)
          part.insert(make_pair(start, it->second));
          part.erase(it);
          break;
        } else { // (A(B)...
          part.erase(it++);
        }
      } else if (start <= it->second) {
        if (end <= it->second) { // (B(A))
          break;
        } else { // (B()A...
          start = it->first;
          part.erase(it++);
        }
      } else { // (B)(A...
        it++;
      }
    }
  }
  bool covered() {
    auto f = part.begin();
    return f != part.end() & f->first == 0 & f->second == length;
  }

private:
  int length;
  map<int, int> part;
};

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  S xs = S(w), ys = S(h);
  bool covered = false;
  int x, y, p;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> p;
    xs.add_ap(x, p);
    ys.add_ap(y, p);
    if (xs.covered() | ys.covered()) {
      covered = true;
      break;
    }
  }
  cout << (covered ? "Yes" : "No") << endl;
  return 0;
}

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

class S {
public:
  S(int length) : length(length) {
    part.insert(make_pair(length + 1, length + 1));
  }
  void add_ap(int pos, int power) {
    int start = max(pos - power, 0), end = min(pos + power, length);
    auto it = part.lower_bound(start);
    if (it != part.begin())
      it--;
    while (true) {
      if (end < it->first) { // (A)(B)
        part.insert(make_pair(start, end));
        break;
      } else if (start < it->first) {
        if (end <= it->second) { // (A()B)
          part.insert(make_pair(start, it->second));
          part.erase(it);
          break;
        } else { // (A(B)...
          part.erase(it++);
        }
      } else if (start <= it->second) {
        if (end <= it->second) { // (B(A))
          break;
        } else { // (B()A...
          start = it->first;
          part.erase(it++);
        }
      } else { // (B)(A...
        it++;
      }
    }
  }
  bool covered() {
    auto f = part.begin();
    return f != part.end() & f->first == 0 & f->second == length;
  }

private:
  int length;
  map<int, int> part;
};

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  S xs = S(w), ys = S(h);
  bool covered = false;
  int x, y, p;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> p;
    xs.add_ap(x, p);
    ys.add_ap(y, p);
    if (xs.covered() | ys.covered()) {
      covered = true;
      break;
    }
  }
  cout << (covered ? "Yes" : "No") << endl;
  return 0;
}
Problem p01712 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int mx[10020], my[10020];
int main() {
  int n, h, w;
  cin >> n >> w >> h;

  while (n--) {
    int x, y, c;
    cin >> x >> y >> c;
    mx[max(0, x - c)]++;
    mx[min(w + 1, x + c)]--;
    my[max(0, y - c)]++;
    my[min(h + 1, y + c)]--;
  }
  int ans = 0;
  for (int i = 0; i < w - 1; i++)
    mx[i + 1] += mx[i];
  for (int i = 0; i < h - 1; i++)
    my[i + 1] += my[i];
  for (int i = 0; i < w; i++)
    if (mx[i] == 0) {
      ans++;
      break;
    }
  for (int i = 0; i < h; i++)
    if (my[i] == 0) {
      ans++;
      break;
    }

  if (ans != 2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int mx[100020], my[100020];
int main() {
  int n, h, w;
  cin >> n >> w >> h;

  while (n--) {
    int x, y, c;
    cin >> x >> y >> c;
    mx[max(0, x - c)]++;
    mx[min(w + 1, x + c)]--;
    my[max(0, y - c)]++;
    my[min(h + 1, y + c)]--;
  }
  int ans = 0;
  for (int i = 0; i < w - 1; i++)
    mx[i + 1] += mx[i];
  for (int i = 0; i < h - 1; i++)
    my[i + 1] += my[i];
  for (int i = 0; i < w; i++)
    if (mx[i] == 0) {
      ans++;
      break;
    }
  for (int i = 0; i < h; i++)
    if (my[i] == 0) {
      ans++;
      break;
    }

  if (ans != 2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01712 (C++)

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

using namespace std;

int n, w, h;

int r(int a, int b) {
  int c = a + b;
  if (c < 0)
    return 0;
  else if (c > h)
    return h;
  else
    return c;
}

int main(void) {
  cin >> n >> w >> h;
  vector<int> x(n), y(n), z(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> z[i];

  vector<vector<int>> g(h, vector<int>(w, 0));
  for (int i = 0; i < h; i++) {
  }

  vector<int> gh(h + 1, 0);
  vector<int> gw(w + 1, 0);
  for (int i = 0; i < n; i++) {
    gh[r(y[i], z[i] * -1)]++;
    gh[r(y[i], z[i])]--;
  }
  for (int i = 0; i < n; i++) {
    gw[r(x[i], z[i] * -1)]++;
    gw[r(x[i], z[i])]--;
  }

  for (int i = 0; i < h; i++)
    gh[i + 1] += gh[i];
  for (int i = 0; i < w; i++)
    gw[i + 1] += gw[i];

  bool f1 = true, f2 = true;
  for (int i = 0; i < h; i++)
    if (gh[i] == 0)
      f1 = false;
  for (int i = 0; i < w; i++)
    if (gw[i] == 0)
      f2 = false;
  if (f1 || f2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

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

using namespace std;

int n, w, h;

int r(int a, int b) {
  int c = a + b;
  if (c < 0)
    return 0;
  else if (c > h)
    return h;
  else
    return c;
}

int main(void) {
  cin >> n >> w >> h;
  vector<int> x(n), y(n), z(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> z[i];

  vector<int> gh(h + 1, 0);
  vector<int> gw(w + 1, 0);
  for (int i = 0; i < n; i++) {
    gh[r(y[i], z[i] * -1)]++;
    gh[r(y[i], z[i])]--;
  }
  for (int i = 0; i < n; i++) {
    gw[r(x[i], z[i] * -1)]++;
    gw[r(x[i], z[i])]--;
  }

  for (int i = 0; i < h; i++)
    gh[i + 1] += gh[i];
  for (int i = 0; i < w; i++)
    gw[i + 1] += gw[i];

  bool f1 = true, f2 = true;
  for (int i = 0; i < h; i++)
    if (gh[i] == 0)
      f1 = false;
  for (int i = 0; i < w; i++)
    if (gw[i] == 0)
      f2 = false;
  if (f1 || f2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p01712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
int miti[10020];
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  int wifi[n][3];
  REP(i, n) { cin >> wifi[i][0] >> wifi[i][1] >> wifi[i][2]; }
  // x軸を調べる
  REP(i, 10020) miti[i] = 0;

  REP(i, n) {
    int a = wifi[i][0];
    int b = wifi[i][2];
    int minx = max(0, a - b);
    int maxx = a + b;

    miti[minx]++;
    miti[maxx]--;
  }
  int total = 0;
  bool flag = true;
  REP(i, w) {
    total += miti[i];
    // cout << miti[i] << i <<endl;
    if (total <= 0) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    return 0;
  }

  REP(i, 10020) miti[i] = 0;

  REP(i, n) {
    int a = wifi[i][1];
    int b = wifi[i][2];
    int miny = max(0, a - b);
    int maxy = a + b;

    miti[miny]++;
    miti[maxy]--;
  }
  total = 0;
  flag = true;
  REP(i, h) {
    total += miti[i];
    // cout << miti[i] <<' ' <<i<<endl;
    if (total <= 0) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    return 0;
  }

  cout << "No" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
int miti[300000];
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  int wifi[n][3];
  REP(i, n) { cin >> wifi[i][0] >> wifi[i][1] >> wifi[i][2]; }
  // x軸を調べる
  REP(i, 10020) miti[i] = 0;

  REP(i, n) {
    int a = wifi[i][0];
    int b = wifi[i][2];
    int minx = max(0, a - b);
    int maxx = a + b;

    miti[minx]++;
    miti[maxx]--;
  }
  int total = 0;
  bool flag = true;
  REP(i, w) {
    total += miti[i];
    // cout << miti[i] << i <<endl;
    if (total <= 0) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    return 0;
  }

  REP(i, 10020) miti[i] = 0;

  REP(i, n) {
    int a = wifi[i][1];
    int b = wifi[i][2];
    int miny = max(0, a - b);
    int maxy = a + b;

    miti[miny]++;
    miti[maxy]--;
  }
  total = 0;
  flag = true;
  REP(i, h) {
    total += miti[i];
    // cout << miti[i] <<' ' <<i<<endl;
    if (total <= 0) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    return 0;
  }

  cout << "No" << endl;

  return 0;
}
Problem p01712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, w, h, ans = 0;
  pair<int, int> han[2][10001];
  cin >> n >> w >> h;
  for (int i = 0, x, y, z; i < n; i++) {
    cin >> x >> y >> z;
    han[0][i] = P(x - z, x + z);
    han[1][i] = P(y - z, y + z);
  }
  han[0][n] = P(w, w);
  han[1][n++] = P(h, h);
  for (int i = 0; i < 2; i++)
    sort(han[i], han[i] + n);
  for (int i = 0; i < 2; i++) {
    int mx = 0, f = 1;
    for (int j = 0; j < n; j++) {
      if (han[i][j].first > mx)
        f = 0;
      mx = max(mx, han[i][j].second);
    }
    if (f)
      ans = 1;
  }
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, w, h, ans = 0;
  pair<int, int> han[2][100001];
  cin >> n >> w >> h;
  for (int i = 0, x, y, z; i < n; i++) {
    cin >> x >> y >> z;
    han[0][i] = P(x - z, x + z);
    han[1][i] = P(y - z, y + z);
  }
  han[0][n] = P(w, w);
  han[1][n++] = P(h, h);
  for (int i = 0; i < 2; i++)
    sort(han[i], han[i] + n);
  for (int i = 0; i < 2; i++) {
    int mx = 0, f = 1;
    for (int j = 0; j < n; j++) {
      if (han[i][j].first > mx)
        f = 0;
      mx = max(mx, han[i][j].second);
    }
    if (f)
      ans = 1;
  }
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01712 (C++)

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

using namespace std;

int xdp[108000], ydp[108000];
int n, W, H;
int x, y, w;
int main() {
  cin >> n >> W >> H;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> w;
    xdp[max(x - w, 0)]++;
    xdp[x + w]--;
    ydp[max(y - w, 0)]++;
    ydp[y + w]--;
  }
  int ok = 2;
  for (int i = 0; i < W; i++) {
    if (i)
      xdp[i] += xdp[i - 1];
    if (xdp[i] == 0) {
      ok--;
      break;
    }
  }

  for (int i = 0; i < H; i++) {
    if (i)
      ydp[i] += ydp[i - 1];
    if (ydp[i] == 0) {
      ok--;
      break;
    }
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


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

using namespace std;

int xdp[216000], ydp[216000];
int n, W, H;
int x, y, w;
int main() {
  cin >> n >> W >> H;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> w;
    xdp[max(x - w, 0)]++;
    xdp[x + w]--;
    ydp[max(y - w, 0)]++;
    ydp[y + w]--;
  }
  int ok = 2;
  for (int i = 0; i < W; i++) {
    if (i)
      xdp[i] += xdp[i - 1];
    if (xdp[i] == 0) {
      ok--;
      break;
    }
  }

  for (int i = 0; i < H; i++) {
    if (i)
      ydp[i] += ydp[i - 1];
    if (ydp[i] == 0) {
      ok--;
      break;
    }
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01713 (C++)

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

int main() {
  int W;
  cin >> W;
  vector<int> a(W), dp1(W + 2, -1e9), dp2(W + 2, -1e9);
  for (int i = 1; i <= W; i++)
    cin >> a[i];

  for (int i = 1; i <= W; i++) {
    if (a[i] == 0)
      dp1[i] = 1e9;
    else if (a[i] < 0)
      dp1[i] = min(dp1[i - 1] - 1, -a[i]);
    else
      dp1[i] = dp1[i - 1] - 1;
  }
  for (int i = W; i >= 1; i--) {
    if (a[i] == 0)
      dp2[i] = 1e9;
    else if (a[i] < 0)
      dp2[i] = min(dp2[i + 1] - 1, -a[i]);
    else
      dp2[i] = dp2[i + 1] - 1;
  }

  int ans = 0;
  for (int i = 1; i <= W; i++)
    ans += max(0, min(max(dp1[i], dp2[i]), a[i]));
  cout << ans << endl;
}

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

int main() {
  int W;
  cin >> W;
  vector<int> a(W + 2), dp1(W + 2, -1e9), dp2(W + 2, -1e9);
  for (int i = 1; i <= W; i++)
    cin >> a[i];

  for (int i = 1; i <= W; i++) {
    if (a[i] == 0)
      dp1[i] = 1e9;
    else if (a[i] < 0)
      dp1[i] = min(dp1[i - 1] - 1, -a[i]);
    else
      dp1[i] = dp1[i - 1] - 1;
  }
  for (int i = W; i >= 1; i--) {
    if (a[i] == 0)
      dp2[i] = 1e9;
    else if (a[i] < 0)
      dp2[i] = min(dp2[i + 1] - 1, -a[i]);
    else
      dp2[i] = dp2[i + 1] - 1;
  }

  int ans = 0;
  for (int i = 1; i <= W; i++)
    ans += max(0, min(max(dp1[i], dp2[i]), a[i]));
  cout << ans << endl;
}
Problem p01715 (C++)

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

int dp[2222][2222];
int R[2222][2222];
int S, N, M;
const int INF = (1000000009);

bool update(int &a, int b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int solvek() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int kid = 0;
    for (int j = 1; j <= N; j++) {
      while (kid < j && update(dp[i][j], dp[i - 1][kid] + R[j][kid]))
        kid++;
      kid--;
      cout << kid << " ";
    }
    cout << endl;
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int solve() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int minik = 0;
    for (int j = 1; j <= N; j++) {
      for (int kid = minik; kid < N; kid++) {
        if (update(dp[i][j], dp[i - 1][kid] + R[j][kid])) {
          minik = min(minik, kid);
        }
      }
    }
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int main() {
  cin >> S >> N >> M;
  int X[2222], Y[2222];
  for (int i = 0; i < S; i++)
    cin >> X[i];

  for (int i = 0; i < N; i++) {
    int t, p;
    cin >> t >> p;
    p--;
    Y[i] = t - X[p];
  }

  sort(Y, Y + N);

  for (int i = 0; i < N; i++) {
    int sum = 0;
    // cout << " y " << Y[i] << endl;
    for (int j = i; j > -1; j--) {
      sum += Y[i] - Y[j];
      R[i + 1][j] = sum;
      // cout << "[ " << j << ", " << i+1 << " ) = " << R[i+1][j] << endl;
    }
  }

  cout << solve() << endl;
}

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

int dp[2222][2222];
int R[2222][2222];
int S, N, M;
const int INF = (1000000009);

bool update(int &a, int b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int solvek() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int kid = 0;
    for (int j = 1; j <= N; j++) {
      while (kid < j && update(dp[i][j], dp[i - 1][kid] + R[j][kid]))
        kid++;
      kid--;
      cout << kid << " ";
    }
    cout << endl;
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int solve() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int minik = 0;
    for (int j = 1; j <= N; j++) {
      for (int kid = minik; kid < N; kid++) {
        if (update(dp[i][j], dp[i - 1][kid] + R[j][kid])) {
          minik = max(minik, kid);
        }
      }
    }
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int main() {
  cin >> S >> N >> M;
  int X[2222], Y[2222];
  for (int i = 0; i < S; i++)
    cin >> X[i];

  for (int i = 0; i < N; i++) {
    int t, p;
    cin >> t >> p;
    p--;
    Y[i] = t - X[p];
  }

  sort(Y, Y + N);

  for (int i = 0; i < N; i++) {
    int sum = 0;
    // cout << " y " << Y[i] << endl;
    for (int j = i; j > -1; j--) {
      sum += Y[i] - Y[j];
      R[i + 1][j] = sum;
      // cout << "[ " << j << ", " << i+1 << " ) = " << R[i+1][j] << endl;
    }
  }

  cout << solve() << endl;
}
Problem p01719 (C++)

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

using namespace std;

struct Item {
  int value, weight;
};

int dp[11][10001];
int knapsack(vector<Item> &items, int W) {
  fill(dp[0], dp[11], -1);
  dp[0][0] = 0;
  int N = items.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (dp[i][j] < 0)
        continue;
      int nj = j + items[i].weight;
      if (nj <= W) {
        dp[i][nj] = max(dp[i][nj], dp[i][j] + items[i].value);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }
  int res = 0;
  for (int i = 0; i <= W; ++i) {
    res = max(res, dp[N][i]);
  }
  return res;
}

int main() {
  int N, D, X;
  cin >> N >> D >> X;
  vector<vector<int>> P(D, vector<int>(N));
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }
  vector<vector<Item>> items(D - 1, vector<Item>(N));
  for (int i = 0; i < D - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      items[i][j] = (Item){P[i + 1][j] - P[i][j], P[i][j]};
    }
  }
  for (int i = 0; i < items.size(); ++i) {
    X += knapsack(items[i], X);
  }
  cout << X << endl;
}

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

using namespace std;

struct Item {
  int value, weight;
};

int dp[11][100001];
int knapsack(vector<Item> &items, int W) {
  fill(dp[0], dp[11], -1);
  dp[0][0] = 0;
  int N = items.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (dp[i][j] < 0)
        continue;
      int nj = j + items[i].weight;
      if (nj <= W) {
        dp[i][nj] = max(dp[i][nj], dp[i][j] + items[i].value);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }
  int res = 0;
  for (int i = 0; i <= W; ++i) {
    res = max(res, dp[N][i]);
  }
  return res;
}

int main() {
  int N, D, X;
  cin >> N >> D >> X;
  vector<vector<int>> P(D, vector<int>(N));
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }
  vector<vector<Item>> items(D - 1, vector<Item>(N));
  for (int i = 0; i < D - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      items[i][j] = (Item){P[i + 1][j] - P[i][j], P[i][j]};
    }
  }
  for (int i = 0; i < items.size(); ++i) {
    X += knapsack(items[i], X);
  }
  cout << X << endl;
}
Problem p01719 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, d, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I; i < n; i++)
    if (p[day][i] / p[day - 1][i] && x / p[day - 1][i]) {
      f = 0;
      saiki(sum + p[day][i], x - p[day - 1][i], day, i);
    }
  if (f)
    ans = max(ans, sum + x);
}

int main() {
  cin >> n >> d >> ans;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, d, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I; i < n; i++)
    if (p[day][i] / p[day - 1][i] && x / p[day - 1][i]) {
      f = 0;
      saiki(sum + p[day][i], x - p[day - 1][i], day, i);
    }
  if (f)
    ans = max(ans, sum + x);
}

int main() {
  cin >> n >> d >> ans;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, 0);
  cout << ans << endl;
  return 0;
}
Problem p01719 (C++)

Buggy submission (status: Runtime Error):
class in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    int operator*() { return it; }
    bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr in(int n) : i(0), n(n) {}
  explicit constexpr in(int i, int n) : i(i, n < i), n(n) {}
  const my_iterator &begin() { return i; }
  const my_iterator &end() { return n; }
};

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, d, x;
  cin >> n >> d >> x;
  vector<vector<int>> p(d, vector<int>(n));
  for (auto &v : p)
    for (auto &a : v)
      cin >> a;
  for (int i : in(d - 1)) {
    vector<int> dp(x + 1, 0);
    for (int j : in(n))
      for (int k : in(p[i][j], x + 1))
        dp[k] = max(dp[k], dp[k - p[i][j]] + p[i + 1][j] - p[i][j]);
    x += dp[x];
  }
  cout << x << endl;
  return 0;
}

Fixed submission:
class in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    int operator*() { return it; }
    bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr in(int n) : i(0), n(n) {}
  explicit constexpr in(int i, int n) : i(i, n < i), n(n) {}
  const my_iterator &begin() { return i; }
  const my_iterator &end() { return n; }
};

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, d, x;
  cin >> n >> d >> x;
  vector<vector<int>> p(d, vector<int>(n));
  for (auto &v : p)
    for (auto &a : v)
      cin >> a;
  for (int i : in(d - 1)) {
    vector<int> dp(x + 1, 0);
    for (int j : in(n))
      if (p[i][j] <= x)
        for (int k : in(p[i][j], x + 1))
          dp[k] = max(dp[k], dp[k - p[i][j]] + p[i + 1][j] - p[i][j]);
    x += dp[x];
  }
  cout << x << endl;
  return 0;
}
Problem p01719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I + 1; i < n; i++)
    for (int j = 1; x / (p[day - 1][i] * j); j++)
      f = 0, saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i);

  if (f)
    ans = max(ans, sum + x);
}

int main() {
  int d, x;
  cin >> n >> d >> x;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  ans = x;
  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I + 1; i < n; i++)
    for (int j = 1; (p[day][i] / p[day - 1][i]) && (x / (p[day - 1][i] * j));
         j++) {
      f = 0;
      saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i);
    }
  if (f)
    ans = max(ans, sum + x);
}

int main() {
  int d, x;
  cin >> n >> d >> x;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  ans = x;
  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}
Problem p01719 (C++)

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

#define MAX_N 20
#define MAX_M 20000000
#define MAX_P 300000

int d[MAX_N][MAX_N];
int dp[MAX_M];
int dp2[MAX_M];
int DP[MAX_N];
int main() {
  int n, m, p;
  cin >> m >> n >> p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> d[i][j];
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    DP[i] = 0;
  }
  DP[0] = p;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < MAX_M; j++) {
      dp[j] = 0;
      dp2[j] = 0;
    }
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < MAX_M - MAX_P; k++) {
        dp[k + d[i][j]] = max(dp[k + d[i][j]], dp[k] + d[i + 1][j]);
      }
    }
    for (int j = 1; j < MAX_M; j++) {
      dp2[j] = max(dp2[j], max(dp[j] - j, dp2[j - 1]));
    }
    DP[i + 1] = DP[i] + dp2[DP[i]];
  }
  cout << DP[n - 1] << endl;
  return 0;
}

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

#define MAX_N 20
#define MAX_M 6000000
#define MAX_P 300000

int d[MAX_N][MAX_N];
int dp[MAX_M];
int dp2[MAX_M];
int DP[MAX_N];
int main() {
  int n, m, p;
  cin >> m >> n >> p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> d[i][j];
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    DP[i] = 0;
  }
  DP[0] = p;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < MAX_M; j++) {
      dp[j] = 0;
      dp2[j] = 0;
    }
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < MAX_M - MAX_P; k++) {
        dp[k + d[i][j]] = max(dp[k + d[i][j]], dp[k] + d[i + 1][j]);
      }
    }
    for (int j = 1; j < MAX_M; j++) {
      dp2[j] = max(dp2[j], max(dp[j] - j, dp2[j - 1]));
    }
    DP[i + 1] = DP[i] + dp2[DP[i]];
  }
  cout << DP[n - 1] << endl;
  return 0;
}
Problem p01724 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define inf 1000000000

using namespace std;

const int W = 15, H = 20;
char map[W][H];
int sx, sy;
const int vx[] = {1, 1, 0, -1, -1, -1, 0, 1},
          vy[] = {0, -1, -1, -1, 0, 1, 1, 1};

int dfs(int x, int y) {
  int nx, ny;
  int ret = inf;
  if (y >= H - 2)
    return 0;

  for (int d = 0; d < 8; d++) {
    nx = x + vx[d], ny = y + vy[d];
    if (nx < 0 || nx >= W || ny < 0 || ny >= H)
      continue;
    if (map[nx][ny] == '.')
      continue;
    while (1) {
      nx += vx[d], ny += vy[d];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H)
        continue;
      if (map[nx][ny] == '.') {
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = '.';
        ret = min(ret, dfs(nx, ny));
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = 'X';
        break;
      }
    }
  }
  return ret + 1;
}

int main(void) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cin >> map[x][y];
      if (map[x][y] == 'O') {
        sx = x, sy = y;
        map[x][y] = '.';
      }
    }
  }
  for (int x = 0; x < W; x++)
    map[x][H - 1] = '.';

  int ans = dfs(sx, sy);
  if (ans >= inf)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#define inf 1000000000

using namespace std;

const int W = 15, H = 20;
char map[W][H];
int sx, sy;
const int vx[] = {1, 1, 0, -1, -1, -1, 0, 1},
          vy[] = {0, -1, -1, -1, 0, 1, 1, 1};

int dfs(int x, int y) {
  int nx, ny;
  int ret = inf;
  if (y >= H - 2)
    return 0;

  for (int d = 0; d < 8; d++) {
    nx = x + vx[d], ny = y + vy[d];
    if (nx < 0 || nx >= W || ny < 0 || ny >= H)
      continue;
    if (map[nx][ny] == '.')
      continue;
    while (1) {
      nx += vx[d], ny += vy[d];
      if (ny >= H - 1) {
        ret = 0;
        break;
      }
      if (nx < 0 || nx >= W || ny < 0)
        break;
      if (map[nx][ny] == '.') {
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = '.';
        ret = min(ret, dfs(nx, ny));
        for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++)
          map[x + k * vx[d]][y + k * vy[d]] = 'X';
        break;
      }
    }
  }
  return ret + 1;
}

int main(void) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cin >> map[x][y];
      if (map[x][y] == 'O') {
        sx = x, sy = y;
        map[x][y] = '.';
      }
    }
  }
  for (int x = 0; x < W; x++)
    map[x][H - 1] = '.';

  int ans = dfs(sx, sy);
  if (ans >= inf)
    ans = -1;
  cout << ans << endl;

  return 0;
}
Problem p01730 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<double, double> P;

double D(P a, P b) {
  int x1 = floor(a.F / 10) * 10, x2 = floor(b.F / 10) * 10,
      y1 = floor(a.S / 10) * 10, y2 = floor(b.S / 10) * 10;
  if (x1 != a.F && x2 != b.F && x1 == x2 && y1 != y2) {
    double d1 = fabs(a.F - x1), d2 = fabs(b.F - x2);
    return fabs(a.S - b.S) + min(d1 + d2, 20 - d1 - d2);
  } else if (y1 != a.S && y2 != b.S && y1 == y2 && x1 != x2) {
    double d1 = fabs(a.S - y1), d2 = fabs(b.S - y2);
    return fabs(a.F - b.F) + min(d1 + d2, 20 - d1 - d2);
  }
  return fabs(a.F - b.F) + fabs(a.S - b.S);
}

int n, M = 1 << 29;
P a[11111];
double solve(int x, int y, double s, double t) {
  double ans = M;
  for (double l = x - 100; l <= x + 100; l += s) {
    for (double r = y - 100; r <= y + 100; r += t) {
      double d = 0;
      for (int i = 0; i < n; i++)
        d = max(d, D(a[i], P(l, r)));
      ans = min(ans, d);
    }
  }
  return ans;
}

int main() {
  cin >> n;
  double Max = -M, Mix = M, May = -M, Miy = M;
  for (int i = 0; i < n; i++) {
    cin >> a[i].F >> a[i].S;
    Max = max(Max, a[i].F + a[i].S);
    Mix = min(Mix, a[i].F + a[i].S);
    May = max(May, a[i].F - a[i].S);
    Miy = min(Miy, a[i].F - a[i].S);
  }
  int xx = (Max + Mix) / 2, yy = (May + Miy) / 2;
  int x = floor((double)(xx + yy) / 20) * 10,
      y = floor((double)(xx - yy) / 20) * 10;
  printf("%.10f\n", min(solve(x, y, 10, 0.5), solve(x, y, 0.5, 10)));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<double, double> P;

double D(P a, P b) {
  int x1 = floor(a.F / 10) * 10, x2 = floor(b.F / 10) * 10,
      y1 = floor(a.S / 10) * 10, y2 = floor(b.S / 10) * 10;
  if (x1 != a.F && x2 != b.F && x1 == x2 && y1 != y2) {
    double d1 = fabs(a.F - x1), d2 = fabs(b.F - x2);
    return fabs(a.S - b.S) + min(d1 + d2, 20 - d1 - d2);
  } else if (y1 != a.S && y2 != b.S && y1 == y2 && x1 != x2) {
    double d1 = fabs(a.S - y1), d2 = fabs(b.S - y2);
    return fabs(a.F - b.F) + min(d1 + d2, 20 - d1 - d2);
  }
  return fabs(a.F - b.F) + fabs(a.S - b.S);
}

int n, M = 1 << 29;
P a[11111];
double solve(int x, int y, double s, double t) {
  double ans = M;
  for (double l = x - 30; l <= x + 30; l += s) {
    for (double r = y - 30; r <= y + 30; r += t) {
      double d = 0;
      for (int i = 0; i < n; i++)
        d = max(d, D(a[i], P(l, r)));
      ans = min(ans, d);
    }
  }
  return ans;
}

int main() {
  cin >> n;
  double Max = -M, Mix = M, May = -M, Miy = M;
  for (int i = 0; i < n; i++) {
    cin >> a[i].F >> a[i].S;
    Max = max(Max, a[i].F + a[i].S);
    Mix = min(Mix, a[i].F + a[i].S);
    May = max(May, a[i].F - a[i].S);
    Miy = min(Miy, a[i].F - a[i].S);
  }
  int xx = (Max + Mix) / 2, yy = (May + Miy) / 2;
  int x = floor((double)(xx + yy) / 20) * 10,
      y = floor((double)(xx - yy) / 20) * 10;
  printf("%.10f\n", min(solve(x, y, 10, 0.5), solve(x, y, 0.5, 10)));
  return 0;
}
Problem p01731 (C++)

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n;
int k[55];
string s[55];

void dfs(int d, int dd) {
  while (1) {
    int ni = -1;
    rep(i, n) {
      if (k[i] == d) {
        ni = i;
        break;
      }
    }
    if (ni == -1)
      return;
    rep(i, dd) cout << ".";
    cout << s[ni] << endl;
    k[ni] = -1;
    dfs(ni + 1, dd + 1);
  }
}

int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> k[i] >> s[i];
  dfs(0, 0);
  return 0;
}

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n;
int k[1111];
string s[1111];

void dfs(int d, int dd) {
  while (1) {
    int ni = -1;
    rep(i, n) {
      if (k[i] == d) {
        ni = i;
        break;
      }
    }
    if (ni == -1)
      return;
    rep(i, dd) cout << ".";
    cout << s[ni] << endl;
    k[ni] = -1;
    dfs(ni + 1, dd + 1);
  }
}

int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> k[i] >> s[i];
  dfs(0, 0);
  return 0;
}
Problem p01733 (C++)

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

using namespace std;

map<int, int> pop[123456];
int x[123456], y[123456];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int w;
    cin >> x[i] >> y[i] >> w;
    pop[y[i]][x[i]] = w;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = -1; j <= 0; j++) {
      for (int k = -1; k <= 0; k++) {
        int c = 0;
        for (int l = 0; l < 2; l++) {
          for (int m = 0; m < 2; m++) {
            c += pop[y[i] + j + l][x[i] + k + m];
          }
        }
        ans = max(ans, c);
      }
    }
  }
  cout << ans << " / 1" << endl;
}

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

using namespace std;

map<int, map<int, int>> pop;
int x[123456], y[123456];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int w;
    cin >> x[i] >> y[i] >> w;
    pop[y[i]][x[i]] = w;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = -1; j <= 0; j++) {
      for (int k = -1; k <= 0; k++) {
        int c = 0;
        for (int l = 0; l < 2; l++) {
          for (int m = 0; m < 2; m++) {
            c += pop[y[i] + j + l][x[i] + k + m];
          }
        }
        ans = max(ans, c);
      }
    }
  }
  cout << ans << " / 1" << endl;
}