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

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

int main() {
  int n, m, t;
  cin >> n >> m >> t;
  int s[11451] = {};
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s[a - m + 1]++;
    s[a + m + 1]--;
  }

  for (int i = 1; i < 11450; i++) {
    s[i] += s[i - 1];
  }

  int ans = 0;
  for (int i = 1; i <= t; i++) {
    if (!s[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, m, t;
  cin >> n >> m >> t;
  int s[114514] = {};
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s[a - m + 1]++;
    s[a + m + 1]--;
  }

  for (int i = 1; i < 11450; i++) {
    s[i] += s[i - 1];
  }

  int ans = 0;
  for (int i = 1; i <= t; i++) {
    if (!s[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p01840 (C++)

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

int main(void) {
  int n, m, t;
  int x;
  cin >> n >> m >> t;
  int b[10101] = {};
  for (int i = 0; i < n; i++) {
    cin >> x;
    b[x - m]--;
    b[x + m]++;
  }
  int ans = 0;
  if (b[0] == 0)
    ans++;
  for (int i = 1; i < t; i++) {
    b[i] += b[i - 1];
    if (b[i] == 0)
      ans++;
  }
  cout << ans << endl;
}

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

int main(void) {
  int n, m, t;
  int x;
  cin >> n >> m >> t;
  int b[10101] = {};
  for (int i = 0; i < n; i++) {
    cin >> x;
    b[x - m]--;
    if (x + m <= t)
      b[x + m]++;
  }
  int ans = 0;
  if (b[0] == 0)
    ans++;
  for (int i = 1; i < t; i++) {
    b[i] += b[i - 1];
    if (b[i] == 0)
      ans++;
  }
  cout << ans << endl;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

int main() {
  int N, M, T;
  cin >> N >> M >> T;
  int t[10001];
  fill(t, t + 10001, 1);
  int let;
  int ans = 0;
  REP(i, 0, N) {
    cin >> let;
    REP(j, let - M + 1, let + M + 1) { t[j] = 0; }
  }
  REP(i, 1, T + 1) { ans += t[i]; }
  p(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

int main() {
  int N, M, T;
  cin >> N >> M >> T;
  int t[10001];
  fill(t, t + 10001, 1);
  int let;
  int ans = 0;
  REP(i, 0, N) {
    cin >> let;
    REP(j, let - M + 1, let + M + 1) {
      if (j > 10000)
        break;
      t[j] = 0;
    }
  }
  REP(i, 1, T + 1) { ans += t[i]; }
  p(ans);
  return 0;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  Delivery to a Luxurious House
//
//  Created by x15071xx on 2017/06/19.
//  Copyright ?? 2017??´ AIT. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;
int main(int argc, const char *argv[]) {

  int n, m, t, taroSc[10000] = {0}, time = 0;
  vector<int> cometime;
  cin >> n >> m >> t;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cometime.push_back(tmp);
  }

  for (int i = 0; i < n; i++) {
    for (int j = cometime[i] - m; j < cometime[i] + m; j++) {
      taroSc[j] = 1;
    }
  }

  for (int i = 0; i < t; i++) {
    if (taroSc[i] == 0) {
      time++;
    }
  }

  cout << time << endl;
  return 0;
}

Fixed submission:
//
//  main.cpp
//  Delivery to a Luxurious House
//
//  Created by x15071xx on 2017/06/19.
//  Copyright ?? 2017??´ AIT. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;
int main(int argc, const char *argv[]) {

  int n, m, t, taroSc[100000] = {0}, time = 0;
  vector<int> cometime;
  cin >> n >> m >> t;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cometime.push_back(tmp);
  }

  for (int i = 0; i < n; i++) {
    for (int j = cometime[i] - m; j < cometime[i] + m; j++) {
      taroSc[j] = 1;
    }
  }

  for (int i = 0; i < t; i++) {
    if (taroSc[i] == 0) {
      time++;
    }
  }

  cout << time << endl;
  return 0;
}
Problem p01840 (C++)

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()
typedef long long ll;

int main() {
  int n, m, t;
  cin >> n >> m >> t;
  int a;
  vector<int> tim(t, 1);
  REP(i, n) {
    cin >> a;
    a--;
    for (int j = a - m + 1; j <= a + m; j++) {
      tim[j] = 0;
    }
  }
  int sum = 0;
  REP(i, t) { sum += tim[i]; }
  cout << sum << endl;
}


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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()
typedef long long ll;

int main() {
  int n, m, t;
  cin >> n >> m >> t;
  int a;
  vector<int> tim(t, 1);
  REP(i, n) {
    cin >> a;
    a--;
    for (int j = a - m + 1; j <= a + m && j < t; j++) {
      tim[j] = 0;
    }
  }
  int sum = 0;
  REP(i, t) { sum += tim[i]; }
  cout << sum << endl;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long int mod = 1000000007;

int main(void) {

  int n, m, t;
  cin >> n >> m >> t;

  vector<bool> a(10000 + 5, false);
  for (int i = 1; i < n + 1; i++) {
    int tmp;
    cin >> tmp;
    for (int j = tmp - m; j < tmp + m; j++)
      a[j] = true;
  }

  int cnt = 0;
  for (int i = 0; i < t; i++) {
    cnt += a[i];
  }
  cout << t - cnt << endl;

  return 0;
}

//

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long int mod = 1000000007;

int main(void) {

  int n, m, t;
  cin >> n >> m >> t;

  vector<bool> a(10000 + 5, false);
  for (int i = 1; i < n + 1; i++) {
    int tmp;
    cin >> tmp;
    for (int j = max(0, tmp - m); j < min(10005, tmp + m); j++)
      a[j] = true;
  }

  int cnt = 0;
  for (int i = 0; i < t; i++) {
    cnt += a[i];
  }
  cout << t - cnt << endl;

  return 0;
}

//
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#define all(c) ((c).begin(), (c).end())
#define rall(c) ((c).rbegin(), (c).rend())
#define ll long long
#define fi first
#define se second
#define inf (999999999)
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int n, m, t;
int a[110];

int main() {
  cin >> n >> m >> t;
  bool b[10000];
  for (int i = 0; i < t; i++)
    b[i] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = a[i] - m; j <= a[i] + m - 1; j++) {
      b[j] = 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < t; i++) {
    if (b[i] == 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#define all(c) ((c).begin(), (c).end())
#define rall(c) ((c).rbegin(), (c).rend())
#define ll long long
#define fi first
#define se second
#define inf (999999999)
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int n, m, t;
int a[110];

int main() {
  cin >> n >> m >> t;
  bool b[20100];
  for (int i = 0; i < t; i++)
    b[i] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = a[i] - m; j <= a[i] + m - 1; j++) {
      b[j] = 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < t; i++) {
    if (b[i] == 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p01841 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

struct Node {
  int l, r;
  int num;
  Node() { l = r = -1; }
};

int number(string &s, int &i) {
  int res = 0;
  while (isdigit(s[i])) {
    res *= 10;
    res += s[i] - '0';
    i++;
  }
  return res;
}

int dfs(vector<Node> &node, string &s, int l, int r, int &idx) {
  if (r - l == 0)
    return -1;

  int cur = idx;

  int cnt = 0, i = l;
  while (true) {
    if (s[i] == '(')
      cnt++;
    else if (s[i] == ')')
      cnt--;
    else if (cnt == 0) {
      idx++;
      node[cur].l = dfs(node, s, l + 1, i - 1, idx);
      i++;
      node[cur].num = number(s, i);
      i++;
      idx++;
      node[cur].r = dfs(node, s, i + 1, r - 1, idx);
      break;
    }
    i++;
  }
  return cur;
}

string merge(vector<Node> &x, int xi, vector<Node> &y, int yi) {
  if (xi == -1 or yi == -1)
    return "";

  string left = merge(x, x[xi].l, y, y[yi].l);
  string num = to_string(x[xi].num + y[yi].num);
  string right = merge(x, x[xi].r, y, y[yi].r);

  return "(" + left + ")" + "[" + num + "]" + "(" + right + ")";
}

int main() {
  string a, b;
  cin >> a >> b;

  vector<Node> x(200), y(200);
  int idx = 0;
  dfs(x, a, 0, a.size(), idx);
  idx = 0;
  dfs(y, b, 0, b.size(), idx);

  string ans = merge(x, 0, y, 0);
  cout << merge(x, 0, y, 0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

struct Node {
  int l, r;
  int num;
  Node() { l = r = -1; }
};

int number(string &s, int &i) {
  int res = 0;
  while (isdigit(s[i])) {
    res *= 10;
    res += s[i] - '0';
    i++;
  }
  return res;
}

int dfs(vector<Node> &node, string &s, int l, int r, int &idx) {
  if (r - l == 0)
    return -1;

  int cur = idx;

  int cnt = 0, i = l;
  while (true) {
    if (s[i] == '(')
      cnt++;
    else if (s[i] == ')')
      cnt--;
    else if (cnt == 0) {
      idx++;
      node[cur].l = dfs(node, s, l + 1, i - 1, idx);
      i++;
      node[cur].num = number(s, i);
      i++;
      idx++;
      node[cur].r = dfs(node, s, i + 1, r - 1, idx);
      break;
    }
    i++;
  }
  return cur;
}

string merge(vector<Node> &x, int xi, vector<Node> &y, int yi) {
  if (xi == -1 or yi == -1)
    return "";

  string left = merge(x, x[xi].l, y, y[yi].l);
  string num = to_string(x[xi].num + y[yi].num);
  string right = merge(x, x[xi].r, y, y[yi].r);

  return "(" + left + ")" + "[" + num + "]" + "(" + right + ")";
}

int main() {
  string a, b;
  cin >> a >> b;

  vector<Node> x(10000), y(10000);
  int idx = 0;
  dfs(x, a, 0, a.size(), idx);
  idx = 0;
  dfs(y, b, 0, b.size(), idx);

  string ans = merge(x, 0, y, 0);
  cout << merge(x, 0, y, 0) << endl;
}
Problem p01845 (C++)

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

int main() {
  int r, w, c, R;
  while (1) {
    int count = 0;
    cin >> r >> w >> c >> R;
    if (r == 0)
      break;
    while (1) {
      if ((double)(r + count * R) / w == c) {
        break;
      } else if ((double)(r + count * R) / w < c) {
        count++;
      } else if ((double)(r + count * R) / w > c) {
        w++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

int main() {
  int r, w, c, R;
  while (1) {
    int count = 0;
    cin >> r >> w >> c >> R;
    if (r == 0)
      break;
    if (w * c <= r) {
    } else if ((w * c - r) % R == 0) {
      count = (w * c - r) / R;
    } else
      count = (w * c - r) / R + 1;
    cout << count << endl;
  }
  return 0;
}
Problem p01845 (C++)

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

using namespace std;

int main() {
  int r, w, c, R;
  while (cin >> r >> w >> c >> R) {
    int ans = (w * c - r) / R;
    if ((w * c - r) % R)
      ++ans;
    if (w * c <= r)
      ans = 0;
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int r, w, c, R;
  while (cin >> r >> w >> c >> R, r) {
    int ans = (w * c - r) / R;
    if ((w * c - r) % R)
      ++ans;
    if (w * c <= r)
      ans = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p01845 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  double R0, W0, C, R;
  while (cin >> R0 >> W0 >> C >> R, R0 + W0 + C + R != 0) {
    int ans = 0;
    while (!(R0 / W0 == C)) {
      if (R0 / W0 > C) {
        W0 += 1.0;
      } else if (R0 / W0 < C) {
        R0 += R;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  double R0, W0, C, R;
  while (cin >> R0 >> W0 >> C >> R, R0 + W0 + C + R != 0) {
    int ans = 0;
    while (R0 / W0 < C) {
      R0 += R;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, char> P;
typedef long long ll;
const int INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};

vector<vi> s, h;
int n, m;
double dp[222][222];

double solve(int i, int w) {
  if (i == n)
    return 0;
  double res = -1;
  int tmp = 0, full = s[i][0] * h[i][0];
  double tmph = 0;
  res = max(res, solve(i + 1, w));
  FOR(j, 1, w + 1) {
    if (j > full) {
      tmp++;
      if (tmp == s[i].size())
        break;
      full += s[i][tmp] * h[i][tmp];
    }
    tmph += 1.0 / s[i][tmp];
    res = max(res, solve(i + 1, w - j) + tmph);
  }
  return dp[i][w] = res;
}

int main() {
  MS(dp, -1);
  cin >> n >> m;
  s.resize(n), h.resize(n);
  REP(i, n) {
    int k;
    cin >> k;
    s[i].resize(k), h[i].resize(k);
    REP(j, k) cin >> s[i][j] >> h[i][j];
  }
  cout << D10 << solve(0, m) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, char> P;
typedef long long ll;
const int INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};

vector<vi> s, h;
int n, m;
double dp[222][222];

double solve(int i, int w) {
  if (i == n)
    return 0;
  if (dp[i][w] >= 0)
    return dp[i][w];
  double res = -1;
  int tmp = 0, full = s[i][0] * h[i][0];
  double tmph = 0;
  res = max(res, solve(i + 1, w));
  FOR(j, 1, w + 1) {
    if (j > full) {
      tmp++;
      if (tmp == s[i].size())
        break;
      full += s[i][tmp] * h[i][tmp];
    }
    tmph += 1.0 / s[i][tmp];
    res = max(res, solve(i + 1, w - j) + tmph);
  }
  return dp[i][w] = res;
}

int main() {
  MS(dp, -1);
  cin >> n >> m;
  s.resize(n), h.resize(n);
  REP(i, n) {
    int k;
    cin >> k;
    s[i].resize(k), h[i].resize(k);
    REP(j, k) cin >> s[i][j] >> h[i][j];
  }
  cout << D10 << solve(0, m) << endl;
  return 0;
}
Problem p01856 (C++)

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

using namespace std;

int D[523][523];
int g[523][523];
int cnt, fx, fy;

void find(int y, int x) {
  if (D[y][x]) {
    fy = y + 1;
    fx = x + 1;
    cnt += D[y][x];
    int p = D[y][x];
    g[fy][fx] = p;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        D[y + i][x + j] -= p;
      }
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> D[i][j];
    }
  }
  for (int i = 0; i < W; i++) {
    int y = 0, x = i;
    for (; x >= 0; y++, x--) {
      find(y, x);
    }
  }
  for (int i = 1; i < H; i++) {
    int y = i, x = W - 1;
    for (; y < H; y++, x--) {
      find(y, x);
    }
  }
  vector<pair<int, int>> ans;
  if (cnt == 1) {
    ans.emplace_back(fy, fx);
  } else {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (g[i][j]) {
          int d = g[i][j] - 1;
          for (int k = -1; k <= 1; k++) {
            int ny = i + k;
            if (ny < 0 || H <= ny)
              continue;
            for (int l = -1; l <= 1; l++) {
              int nx = j + l;
              if (nx < 0 || W <= nx || k == 0 && l == 0)
                continue;
              d += g[ny][nx];
            }
          }
          if (d % 2) {
            ans.emplace_back(i, j);
          }
        }
      }
    }
  }
  cout << ans.back().first << ' ' << ans.back().second << ' ' << ans[0].first
       << ' ' << ans[0].second << endl;
}

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

using namespace std;

int D[523][523];
int g[523][523];
int cnt, fx, fy;

void find(int y, int x) {
  if (D[y][x]) {
    fy = y + 1;
    fx = x + 1;
    cnt++;
    int p = D[y][x];
    g[fy][fx] = p;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        D[y + i][x + j] -= p;
      }
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> D[i][j];
    }
  }
  for (int i = 0; i < W; i++) {
    int y = 0, x = i;
    for (; x >= 0; y++, x--) {
      find(y, x);
    }
  }
  for (int i = 1; i < H; i++) {
    int y = i, x = W - 1;
    for (; y < H; y++, x--) {
      find(y, x);
    }
  }
  vector<pair<int, int>> ans;
  if (cnt == 1) {
    ans.emplace_back(fy, fx);
  } else {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (g[i][j]) {
          int d = g[i][j] - 1;
          for (int k = -1; k <= 1; k++) {
            int ny = i + k;
            if (ny < 0 || H <= ny)
              continue;
            for (int l = -1; l <= 1; l++) {
              int nx = j + l;
              if (nx < 0 || W <= nx || k == 0 && l == 0)
                continue;
              d += g[ny][nx];
            }
          }
          if (d % 2) {
            ans.emplace_back(i, j);
          }
        }
      }
    }
  }
  cout << ans.back().first << ' ' << ans.back().second << ' ' << ans[0].first
       << ' ' << ans[0].second << endl;
}
Problem p01865 (C++)

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

int main() {
  int L, N, W[100] = {};
  cin >> L >> N;
  for (int i = 0; i < N; i++) {
    int x, w;
    cin >> x >> w;
    W[x + 50] += w;
  }

  vector<pair<int, int>> ret;
  for (int i = -2 * L; i < 0; i++) {
    int P = W[50 + i];
    int Q = W[50 - i];
    int R = max(W[50 + i], W[50 - i]);
    for (int j = W[50 + i]; j < R; j++) {
      ret.push_back({i, 1});
    }
    for (int j = W[50 - i]; j < R; j++) {
      ret.push_back({-i, 1});
    }
  }
  cout << ret.size() << endl;
  for (auto k : ret) {
    cout << k.first << " " << k.second << endl;
  }
}

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

int main() {
  int L, N, W[100] = {};
  cin >> L >> N;
  for (int i = 0; i < N; i++) {
    int x, w;
    cin >> x >> w;
    W[x + 50] += w;
  }

  vector<pair<int, int>> ret;
  for (int i = -L; i < 0; i++) {
    int P = W[50 + i];
    int Q = W[50 - i];
    int R = max(W[50 + i], W[50 - i]);
    for (int j = W[50 + i]; j < R; j++) {
      ret.push_back({i, 1});
    }
    for (int j = W[50 - i]; j < R; j++) {
      ret.push_back({-i, 1});
    }
  }
  cout << ret.size() << endl;
  for (auto k : ret) {
    cout << k.first << " " << k.second << endl;
  }
}
Problem p01866 (C++)

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

int n;
int d;
char S[1001];
vector<int> ones;

int main() {
  scanf("%d", &n);
  scanf("%s", S);
  scanf("%d", &d);

  for (int i = 0; i < n && d > 0; i++) {
    if (S[i] == '1')
      ones.push_back(i);
    else if (S[i] == '0') {
      S[i] = '1';
      --d;
    }
  }

  while (d > 0) {
    int idx = ones.back();
    assert(S[idx] == '1');
    S[idx] = '0';
    d--;
  }

  puts(S);
}

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

int n;
int d;
char S[1001];
vector<int> ones;

int main() {
  scanf("%d", &n);
  scanf("%s", S);
  scanf("%d", &d);

  for (int i = 0; i < n && d > 0; i++) {
    if (S[i] == '1')
      ones.push_back(i);
    else if (S[i] == '0') {
      S[i] = '1';
      --d;
    }
  }

  while (d > 0) {
    int idx = ones.back();
    assert(S[idx] == '1');
    S[idx] = '0';
    d--;
    ones.pop_back();
  }

  puts(S);
}
Problem p01868 (C++)

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

const int INF = 1 << 28;

int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> T(N);
  for (auto &i : T)
    cin >> i, sum += i;

  vector<vector<vector<int>>> dp(
      55, vector<vector<int>>(1111, vector<int>(1111, INF)));
  dp[0][0][0] = 0;

  int res = INF;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        if (!dp[i][j][k]) {
          if (j + T[i] < 1000)
            dp[i + 1][j + T[i]][k] = 0;
          if (k + T[i] < 1000)
            dp[i + 1][j][k + T[i]] = 0;
          res = min(res, max(j + T[i], max(k, sum - (j + k + T[i]))));
          res = min(res, max(j, max(k + T[i], sum - (j + k + T[i]))));
          dp[i + 1][j][k] = 0;
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}

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

const int INF = 1 << 28;

int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> T(N);
  for (auto &i : T)
    cin >> i, sum += i;

  vector<vector<vector<int>>> dp(
      51, vector<vector<int>>(1000, vector<int>(1000, INF)));
  dp[0][0][0] = 0;

  int res = INF;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        if (!dp[i][j][k]) {
          if (j + T[i] < 1000)
            dp[i + 1][j + T[i]][k] = 0;
          if (k + T[i] < 1000)
            dp[i + 1][j][k + T[i]] = 0;
          res = min(res, max(j + T[i], max(k, sum - (j + k + T[i]))));
          res = min(res, max(j, max(k + T[i], sum - (j + k + T[i]))));
          dp[i + 1][j][k] = 0;
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}
Problem p01869 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int ans = -1;
ll n;
vector<ll> a;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

void saiki(ll num, int idx, int sum) {
  if (num == 1)
    ans = max(sum, ans);
  if (num < idx || idx == a.size())
    return;
  if (num % a[idx] == 0)
    saiki(num / a[idx], idx, sum + 1);
  saiki(num, idx + 1, sum);
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  if (n > 1)
    saiki(n, 0, 0);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int ans = -1;
ll n;
vector<ll> a;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

void saiki(ll num, int idx, int sum) {
  if (num == 1)
    ans = max(sum, ans);
  if (num < a[idx] || idx == a.size())
    return;
  if (num % a[idx] == 0)
    saiki(num / a[idx], idx, sum + 1);
  saiki(num, idx + 1, sum);
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  if (n > 1)
    saiki(n, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p01869 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

vector<ll> V;

int ans = -1;

void recursive(ll N, int index, int count) {

  if (N == 1) {
    ans = max(ans, count);
    return;
  }

  if (N < V[index] || index == (int)V.size())
    return;

  recursive(N, index + 1, count);

  while (N % V[index] == 0) {
    N /= V[index];
    recursive(N, index + 1, ++count);
  }
}

int main() {

  ll POW[18];

  for (int i = 0; i <= 17; i++) {
    POW[i] = pow(10, i);
  }

  ll tmp;
  int limit;

  ll N;
  scanf("%lld", &N);
  if (N == 1) {
    printf("-1\n");
    return 0;
  }

  for (int length = 1; length <= 18; length++) {
    limit = pow(2, length);
    for (int state = 0; state < limit; state++) {
      tmp = 0;
      for (int loop = 0; loop < length; loop++) {
        if (state & (1 << loop)) {
          tmp += 8 * POW[loop];
        } else {
          tmp += 2 * POW[loop];
        }
      }
      if (N % tmp == 0) { //?????????????????°????????§??????!!!
        V.push_back(tmp);
      }
    }
  }

  sort(V.begin(), V.end());

  recursive(N, 0, 0);

  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

vector<ll> V;

int ans = -1;

void recursive(ll N, int index, int count) {

  if (N == 1) {
    ans = max(ans, count);
    return;
  }

  if (N < V[index] || index == (int)V.size())
    return;

  recursive(N, index + 1, count);

  while (N % V[index] == 0) {
    N /= V[index];
    recursive(N, index + 1, ++count);
  }
}

int main() {

  ll POW[18];

  for (int i = 0; i <= 17; i++) {
    POW[i] = pow(10, i);
  }

  ll tmp;
  int limit;

  ll N;
  scanf("%lld", &N);
  if (N == 1) {
    printf("-1\n");
    return 0;
  }

  for (int length = 1; length <= 18; length++) {
    limit = pow(2, length);
    for (int state = 0; state < limit; state++) {
      tmp = 0;
      for (int loop = 0; loop < length; loop++) {
        if (state & (1 << loop)) {
          tmp += 8 * POW[loop];
        } else {
          tmp += 2 * POW[loop];
        }
      }
      if (N % tmp == 0) { //?????????????????°????????§??????!!!
        V.push_back(tmp);
      }
    }
  }

  sort(V.begin(), V.end());

  if (V.size() == 0) {
    printf("-1\n");
    return 0;
  }

  recursive(N, 0, 0);

  printf("%d\n", ans);

  return 0;
}
Problem p01869 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

vector<long long int> v;
map<pair<long long int, long long int>, int> mp;
int solve(long long int n, long long int from) {
  if (mp.find(make_pair(from, n)) == mp.end()) {

    {
      int ans = -1000;
      for (int i = 0; i <= from; ++i) {
        long long int a = v[i];
        if (n % a == 0) {
          if (a == n)
            ans = max(1, ans);
          else {

            ans = max(ans, 1 + solve(n / a, a));
          }
        }
      }
      mp[make_pair(from, n)] = ans;
    }
  }
  return mp[make_pair(from, n)];
}

int main() {
  long long int N;
  cin >> N;
  for (int i = 1; i <= 18; ++i) {
    for (int j = 0; j < (1 << i); ++j) {
      bitset<18> bs(j);
      long long int n = 0;
      for (int k = 0; k < i; ++k) {
        n *= 10;
        n += bs[i - k - 1] ? 8 : 2;
      }
      v.emplace_back(n);
    }
  }
  int ans = solve(N, v.size() - 1);
  if (ans < 0)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

vector<long long int> v;
map<pair<long long int, long long int>, int> mp;
int solve(long long int n, long long int from) {
  if (mp.find(make_pair(from, n)) == mp.end()) {

    {
      int ans = -1000;
      for (int i = 0; i <= from; ++i) {
        long long int a = v[i];
        if (n % a == 0) {
          if (a == n)
            ans = max(1, ans);
          else {

            ans = max(ans, 1 + solve(n / a, i));
          }
        }
      }
      mp[make_pair(from, n)] = ans;
    }
  }
  return mp[make_pair(from, n)];
}

int main() {
  long long int N;
  cin >> N;
  for (int i = 1; i <= 18; ++i) {
    for (int j = 0; j < (1 << i); ++j) {
      bitset<18> bs(j);
      long long int n = 0;
      for (int k = 0; k < i; ++k) {
        n *= 10;
        n += bs[i - k - 1] ? 8 : 2;
      }
      v.emplace_back(n);
    }
  }
  int ans = solve(N, v.size() - 1);
  if (ans < 0)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p01869 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int d[2] = {2, 8}, ans = -1;
vector<ll> v;

void func(int x, ll k) {
  if (x)
    v.push_back(k);
  if (x == 18)
    return;
  for (int i = 0; i < 2; i++)
    func(x + 1, k * 10 + d[i]);
}

void func2(int x, ll k, int sum) {
  if (k == 1 && sum)
    ans = max(ans, sum);
  if (v[x] > k)
    return;
  if (k % !v[x])
    func2(x, k / v[x], sum + 1);
  if (x + 1 != v.size())
    func2(x + 1, k, sum);
}

int main() {
  ll n;
  cin >> n;
  func(0, 0);
  sort(v.begin(), v.end());
  func2(0, n, 0);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int d[2] = {2, 8}, ans = -1;
vector<ll> v;

void func(int x, ll k) {
  if (x)
    v.push_back(k);
  if (x == 18)
    return;
  for (int i = 0; i < 2; i++)
    func(x + 1, k * 10 + d[i]);
}

void func2(int x, ll k, int sum) {
  if (k == 1 && sum)
    ans = max(ans, sum);
  if (v[x] > k)
    return;
  if (!(k % v[x]))
    func2(x, k / v[x], sum + 1);
  if (x + 1 != v.size())
    func2(x + 1, k, sum);
}

int main() {
  ll n;
  cin >> n;
  func(0, 0);
  sort(v.begin(), v.end());
  func2(0, n, 0);
  cout << ans << endl;
  return 0;
}
Problem p01869 (C++)

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

typedef long long int ll;
const ll INF = 1LL << 28;

ll N;
vector<ll> num;
map<ll, ll> memo;
queue<ll> q;
function<ll(ll)> seki;

int main() {
  cin >> N;
  q.push(2LL);
  q.push(8LL);
  while (q.size()) {
    ll n = q.front();
    q.pop();
    if (n <= 1000000000000000000) {
      num.push_back(n);
      q.push(n * 10LL + 8LL);
      q.push(n * 10LL + 2LL);
    }
  }

  sort(num.begin(), num.end());

  seki = [&](ll n) -> ll {
    if (memo.find(n) != memo.end())
      return memo[n];

    int i = (int)(lower_bound(num.begin(), num.end(), n) - num.begin());
    memo[n] = -INF;
    if (num[i] == n)
      memo[n] = 1;
    for (; i > -1; i--) {
      if (n % num[i] == 0) {
        memo[n] = max(memo[n], seki(n / num[i]) + seki(num[i]));
      }
    }

    return memo[n];
  };

  int ans = seki(N);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}

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

typedef long long int ll;
const ll INF = 1LL << 28;

ll N;
vector<ll> num;
map<ll, ll> memo;
queue<ll> q;
function<ll(ll)> seki;

int main() {
  cin >> N;
  q.push(2LL);
  q.push(8LL);
  while (q.size()) {
    ll n = q.front();
    q.pop();
    num.push_back(n);
    if (n <= 1e18) {
      q.push(n * 10LL + 8LL);
      q.push(n * 10LL + 2LL);
    }
  }

  sort(num.begin(), num.end());

  seki = [&](ll n) -> ll {
    if (memo.find(n) != memo.end())
      return memo[n];

    int i = (int)(lower_bound(num.begin(), num.end(), n) - num.begin());
    memo[n] = -INF;
    if (num[i] == n)
      memo[n] = 1;
    for (; i > -1; i--) {
      if (n % num[i] == 0) {
        memo[n] = max(memo[n], seki(n / num[i]) + seki(num[i]));
      }
    }

    return memo[n];
  };

  int ans = seki(N);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}
Problem p01869 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
typedef pair<int, P> iP;
ll n;
vector<ll> a;
queue<iP> Q;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

int bfs() {
  int ans = -1;
  Q.push(iP(0, P(0, n)));
  while (!Q.empty()) {
    iP t = Q.front();
    Q.pop();
    int idx = t.first, sum = t.second.first;
    ll num = t.second.second;
    if (num == 1)
      ans = max(ans, sum);
    if (num < a[idx] || idx == a.size())
      continue;
    if (num % a[idx] == 0)
      Q.push(iP(idx, P(sum + 1, num / a[idx])));
    Q.push(iP(idx + 1, P(sum, num)));
  }
  return ans;
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  cout << bfs() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
typedef pair<int, P> iP;
ll n;
vector<ll> a;
queue<iP> Q;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

int bfs() {
  if (n == 1)
    return -1;
  int ans = -1;
  Q.push(iP(0, P(0, n)));
  while (!Q.empty()) {
    iP t = Q.front();
    Q.pop();
    int idx = t.first, sum = t.second.first;
    ll num = t.second.second;
    if (num == 1)
      ans = max(ans, sum);
    if (num < a[idx] || idx == a.size())
      continue;
    if (num % a[idx] == 0)
      Q.push(iP(idx, P(sum + 1, num / a[idx])));
    Q.push(iP(idx + 1, P(sum, num)));
  }
  return ans;
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  cout << bfs() << endl;
  return 0;
}
Problem p01873 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int main() {

  int N;
  scanf("%d", &N);

  int *table = new int[N];
  for (int i = 0; i < N; i++) {
    scanf("%d", &table[i]);
  }

  bool FLG;
  int num;

  for (num = 1; num < N; num++) {
    FLG = true;
    for (int loc = 0; loc + num < N; loc++) {
      if (table[loc] != table[loc + num]) {
        FLG = false;
        break;
      }
    }
    if (FLG)
      break;
  }

  printf("%d\n", N / num);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int main() {

  int N;
  scanf("%d", &N);

  int *table = new int[N];
  for (int i = 0; i < N; i++) {
    scanf("%d", &table[i]);
  }

  bool FLG;
  int num;

  for (num = 1; num < N; num++) {
    if (N % num != 0)
      continue;
    FLG = true;
    for (int loc = 0; loc + num < N; loc++) {
      if (table[loc] != table[loc + num]) {
        FLG = false;
        break;
      }
    }
    if (FLG)
      break;
  }

  printf("%d\n", N / num);

  return 0;
}
Problem p01873 (C++)

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

using namespace std;

typedef long long ll;

const int inf = 1e9;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &a[i]);
  }
  set<int> s;
  for (int i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      s.insert(i);
      s.insert(N / i);
    }
  }
  int ans = 1;
  for (int k : s) {
    int t = N / k;
    bool f = true;
    for (int i = 0; i < t; ++i) {
      for (int j = i; j < N; j += k) {
        if (a[i] != a[j]) {
          f = false;
          break;
        }
      }
      if (!f)
        break;
    }
    if (f)
      ans = max(ans, t);
  }
  cout << ans << endl;
}

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

using namespace std;

typedef long long ll;

const int inf = 1e9;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &a[i]);
  }
  set<int> s;
  for (int i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      s.insert(i);
      s.insert(N / i);
    }
  }
  int ans = 1;
  for (int k : s) {
    int t = N / k;
    bool f = true;
    for (int i = 0; i < k; ++i) {
      for (int j = i; j < N; j += k) {
        if (a[i] != a[j]) {
          f = false;
          break;
        }
      }
      if (!f)
        break;
    }
    if (f)
      ans = max(ans, t);
  }
  cout << ans << endl;
}
Problem p01880 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  L
//
//  Created by LucienShui on 2017/4/20.
//  Copyright ? 2017年 LucienShui. All rights reserved.
//

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

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

using namespace std;

long long a[1005];

bool panduan(long long a) {
  while (a / 10) {
    long long t = a % 10;
    a /= 10;
    if (a % 10 != t - 1)
      return false;
  }
  return true;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  long long n, cnt = 0, result = -1, t;
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++)
    scanf("%lld", a + i);
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      t = a[i] * a[j];
      if (panduan(t))
        result = max(t, result);
    }
  }
  printf("%lld\n", result);
  return 0;
}

Fixed submission:
//
//  main.cpp
//  L
//
//  Created by LucienShui on 2017/4/20.
//  Copyright ? 2017年 LucienShui. All rights reserved.
//

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

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

using namespace std;

long long a[1005];

bool panduan(long long a) {
  while (a / 10) {
    long long t = a % 10;
    a /= 10;
    if (a % 10 != t - 1)
      return false;
  }
  return true;
}
int main() {
  long long n, cnt = 0, result = -1, t;
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++)
    scanf("%lld", a + i);
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      t = a[i] * a[j];
      if (panduan(t))
        result = max(t, result);
    }
  }
  printf("%lld\n", result);
  return 0;
}
Problem p01880 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }

int main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) { cin >> v[i]; }
  vs s;
  string t = "123456789";
  rep(i, n - 1) range(j, i + 1, n) {
    string x = to_string(v[i] * v[j]);
    if (t.find(x) != string::npos)
      s.push_back(x);
  }
  sort(all(s), [](string a, string b) {
    return a.length() > b.length() || (a.length() == b.length() && a[0] > b[0]);
  });
  cout << s[0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }

int main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) { cin >> v[i]; }
  vs s;
  string t = "123456789";
  rep(i, n - 1) range(j, i + 1, n) {
    string x = to_string(v[i] * v[j]);
    if (t.find(x) != string::npos)
      s.push_back(x);
  }
  sort(all(s), [](string a, string b) {
    return a.length() > b.length() || (a.length() == b.length() && a[0] > b[0]);
  });
  if (s.size()) {
    cout << s[0] << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p01881 (C++)

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

using namespace std;

typedef pair<int, int> P;
const int INF = numeric_limits<int>::max() / 2;

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

  int h, w;
  vector<string> v(h);
  int sx, sy, px, py;
  for (int i = 0; i < h; i++) {
    cin >> v[i];
    for (int j = 0; j < w; j++) {
      if (v[i][j] == '%') {
        sx = i;
        sy = j;
      } else if (v[i][j] == '@') {
        px = i;
        py = j;
      }
    }
  }
  vector<vector<int>> dis(300, vector<int>(300, INF));
  dis[sx][sy] = 0;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  queue<P> q;
  q.push(P(sx, sy));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      if (v[nx][ny] == '#')
        continue;
      if (dis[nx][ny] != INF)
        continue;
      dis[nx][ny] = dis[x][y] + 1;
      q.push(P(nx, ny));
    }
  }
  int pdis = dis[px][py];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] != '$')
        continue;
      if (dis[i][j] <= pdis) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> P;
const int INF = numeric_limits<int>::max() / 2;

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

  int h, w;
  cin >> h >> w;
  vector<string> v(h);
  int sx, sy, px, py;
  for (int i = 0; i < h; i++) {
    cin >> v[i];
    for (int j = 0; j < w; j++) {
      if (v[i][j] == '%') {
        sx = i;
        sy = j;
      } else if (v[i][j] == '@') {
        px = i;
        py = j;
      }
    }
  }
  vector<vector<int>> dis(300, vector<int>(300, INF));
  dis[sx][sy] = 0;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  queue<P> q;
  q.push(P(sx, sy));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      if (v[nx][ny] == '#')
        continue;
      if (dis[nx][ny] != INF)
        continue;
      dis[nx][ny] = dis[x][y] + 1;
      q.push(P(nx, ny));
    }
  }
  int pdis = dis[px][py];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] != '$')
        continue;
      if (dis[i][j] <= pdis) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p01881 (C++)

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

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, (n))
#define ALL(a) (a.begin(), a.end())
typedef vector<int> vi;

constexpr int INF = 1000000000;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> t(h);
  int pi, pj, hi, hj;
  vi si, sj;
  REP(i, h) {
    cin >> t[i];
    REP(j, w) {
      switch (t[i][j]) {
      case '@':
        pi = i;
        pj = j;
        break;
      case '%':
        hi = i;
        hj = j;
        break;
      case '$':
        si.push_back(i);
        sj.push_back(j);
      }
    }
  }
  vector<vi> dist(h, vi(w, INF));
  dist[hi][hj] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(hi, hj));
  while (!que.empty()) {
    int i, j;
    tie(i, j) = que.front();
    que.pop();
    int di[] = {1, 0, -1, 0};
    int dj[] = {0, 1, 0, -1};
    REP(k, 4) {
      int ni = i + di[k];
      int nj = j + dj[k];
      if (ni < 0 || nj < 0 || ni >= h || nj >= w)
        continue;
      if (t[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= dist[i][j])
        continue;
      dist[ni][nj] = dist[i][j] + 1;
      que.push(make_pair(ni, nj));
    }
  }
  int pd = dist[pi][pj];
  bool valid = true;
  REP(i, si.size()) {
    if (dist[si[i]][sj[i]] <= pd) {
      valid = false;
    }
  }
  if (valid) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

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

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, (n))
#define ALL(a) (a.begin(), a.end())
typedef vector<int> vi;

constexpr int INF = 1000000000;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> t(h);
  int pi, pj, hi, hj;
  vi si, sj;
  REP(i, h) {
    cin >> t[i];
    REP(j, w) {
      switch (t[i][j]) {
      case '@':
        pi = i;
        pj = j;
        break;
      case '%':
        hi = i;
        hj = j;
        break;
      case '$':
        si.push_back(i);
        sj.push_back(j);
      }
    }
  }
  vector<vi> dist(h, vi(w, INF));
  dist[hi][hj] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(hi, hj));
  while (!que.empty()) {
    int i, j;
    tie(i, j) = que.front();
    que.pop();
    int di[] = {1, 0, -1, 0};
    int dj[] = {0, 1, 0, -1};
    REP(k, 4) {
      int ni = i + di[k];
      int nj = j + dj[k];
      if (ni < 0 || nj < 0 || ni >= h || nj >= w)
        continue;
      if (t[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= dist[i][j] + 1)
        continue;
      dist[ni][nj] = dist[i][j] + 1;
      que.push(make_pair(ni, nj));
    }
  }
  int pd = dist[pi][pj];
  bool valid = true;
  REP(i, si.size()) {
    if (dist[si[i]][sj[i]] <= pd) {
      valid = false;
    }
  }
  if (valid) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}