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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
int main() {
  double p[100005];
  int n;
  cin >> n;
  rep(i, 2 * n) cin >> p[i];
  sort(p, p + 2 * n, greater<double>());
  double sum = 0;
  rep(i, 2 * n) sum += (2.0 * n - i - 1.0) / (2.0 * n - 1.0) * p[i];
  printf("%.10f\n", sum);
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
int main() {
  double p[200005];
  int n;
  cin >> n;
  rep(i, 2 * n) cin >> p[i];
  sort(p, p + 2 * n, greater<double>());
  double sum = 0;
  rep(i, 2 * n) sum += (2.0 * n - i - 1.0) / (2.0 * n - 1.0) * p[i];
  printf("%.10f\n", sum);
}
Problem p03514 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[111111], n;
int main() {
  scanf("%d", &n);
  n *= 2;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  double res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += a[i] / (n - 1.0) * i;
  }
  printf("%.12lf\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[222222], n;
int main() {
  scanf("%d", &n);
  n *= 2;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  double res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += a[i] / (n - 1.0) * i;
  }
  printf("%.12lf\n", res);
  return 0;
}
Problem p03515 (C++)

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

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 5;

array<int, 3> edge[maxn];
int N, lab[maxn];
ll res[maxn];
int info[maxn], par[maxn], sz[maxn];

int finds(int u) {
  if (lab[u] == u)
    return u;
  return lab[u] = finds(lab[u]);
}

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N;
  for (int i = 1; i < N; ++i) {
    cin >> edge[i][1] >> edge[i][2] >> edge[i][0];
  }
  sort(edge + 1, edge + N);
  for (int i = 1; i <= N; ++i) {
    lab[i] = info[i] = i;
    sz[i] = 1;
  }
  int cnt = N + 1;
  for (int i = N - 1; i >= 1; --i) {
    int u = edge[i][1], v = edge[i][2], w = edge[i][0];
    u = finds(u);
    v = finds(v);
    par[info[u]] = cnt;
    par[info[v]] = cnt;
    res[info[u]] = 1ll * sz[v] * w;
    res[info[v]] = 1ll * sz[u] * w;
    info[v] = cnt;
    sz[v] += sz[u];
    lab[u] = v;
    ++cnt;
  }
  for (int i = cnt; i >= 1; --i) {
    res[i] += res[par[i]];
  }
  for (int i = 1; i <= N; ++i) {
    cout << res[i] << '\n';
  }
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 2e5 + 5;

array<int, 3> edge[maxn];
int N, lab[maxn];
ll res[maxn];
int info[maxn], par[maxn], sz[maxn];

int finds(int u) {
  if (lab[u] == u)
    return u;
  return lab[u] = finds(lab[u]);
}

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N;
  for (int i = 1; i < N; ++i) {
    cin >> edge[i][1] >> edge[i][2] >> edge[i][0];
  }
  sort(edge + 1, edge + N);
  for (int i = 1; i <= N; ++i) {
    lab[i] = info[i] = i;
    sz[i] = 1;
  }
  int cnt = N + 1;
  for (int i = N - 1; i >= 1; --i) {
    int u = edge[i][1], v = edge[i][2], w = edge[i][0];
    u = finds(u);
    v = finds(v);
    par[info[u]] = cnt;
    par[info[v]] = cnt;
    res[info[u]] = 1ll * sz[v] * w;
    res[info[v]] = 1ll * sz[u] * w;
    info[v] = cnt;
    sz[v] += sz[u];
    lab[u] = v;
    ++cnt;
  }
  for (int i = cnt; i >= 1; --i) {
    res[i] += res[par[i]];
  }
  for (int i = 1; i <= N; ++i) {
    cout << res[i] << '\n';
  }
}
Problem p03516 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int D[210][210][410], w[210], n, Mod = 1000000007, F[410], InvF[410], res;

int Pow(int a, int b) {
  int r = 1;
  while (b) {
    if (b & 1)
      r = 1ll * r * a % Mod;
    a = 1ll * a * a % Mod;
    b >>= 1;
  }
  return r;
}

int main() {
  int i, j, k, s = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &w[i]);
  F[0] = InvF[0] = 1;
  for (i = 1; i <= 400; i++) {
    F[i] = 1ll * F[i - 1] * i % Mod;
    InvF[i] = Pow(F[i], Mod - 2);
  }
  D[0][0][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < i; j++) {
      for (k = 0; k <= s; k++) {
        D[i][j][k] = (D[i][j][k] + 1ll * D[i - 1][j][k] * InvF[w[i] - 1]) % Mod;
        if (w[i] >= 2)
          D[i][j + 1][k + w[i] - 2] = (D[i][j + 1][k + w[i] - 2] +
                                       1ll * D[i - 1][j][k] * InvF[w[i] - 2]) %
                                      Mod;
      }
    }
    if (w[i] >= 2)
      s += w[i] - 2;
  }
  for (i = 3; i <= n; i++) {
    for (j = 0; j <= s; j++) {
      if (i != n && j == 0)
        continue;
      int t = 1ll * D[n][i][j] * F[i - 1] % Mod * InvF[2] % Mod * F[j] % Mod;
      if (i == n)
        res = (res + t) % Mod;
      else
        res = (res + 1ll * t * F[n - i - 1] % Mod * InvF[j - 1]) % Mod;
    }
  }
  printf("%d\n", res);
}

Fixed submission:
#include <cstdio>
int D[310][310][610], w[310], n, Mod = 1000000007, F[410], InvF[410], res;

int Pow(int a, int b) {
  int r = 1;
  while (b) {
    if (b & 1)
      r = 1ll * r * a % Mod;
    a = 1ll * a * a % Mod;
    b >>= 1;
  }
  return r;
}

int main() {
  int i, j, k, s = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &w[i]);
  F[0] = InvF[0] = 1;
  for (i = 1; i <= 400; i++) {
    F[i] = 1ll * F[i - 1] * i % Mod;
    InvF[i] = Pow(F[i], Mod - 2);
  }
  D[0][0][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < i; j++) {
      for (k = 0; k <= s; k++) {
        D[i][j][k] = (D[i][j][k] + 1ll * D[i - 1][j][k] * InvF[w[i] - 1]) % Mod;
        if (w[i] >= 2)
          D[i][j + 1][k + w[i] - 2] = (D[i][j + 1][k + w[i] - 2] +
                                       1ll * D[i - 1][j][k] * InvF[w[i] - 2]) %
                                      Mod;
      }
    }
    if (w[i] >= 2)
      s += w[i] - 2;
  }
  for (i = 3; i <= n; i++) {
    for (j = 0; j <= s; j++) {
      if (i != n && j == 0)
        continue;
      int t = 1ll * D[n][i][j] * F[i - 1] % Mod * InvF[2] % Mod * F[j] % Mod;
      if (i == n)
        res = (res + t) % Mod;
      else
        res = (res + 1ll * t * F[n - i - 1] % Mod * InvF[j - 1]) % Mod;
    }
  }
  printf("%d\n", res);
}
Problem p03517 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
bool chmax(ll &a, ll b) {
  if (a > b)
    return 0;
  a = b;
  return 1;
}
bool chmin(ll &a, ll b) {
  if (a < b)
    return 0;
  a = b;
  return 1;
}

#define N 100010
ll n, m, k;
ll col[N];
struct edge {
  ll a, b, cost;
  bool operator<(const edge &key) const { return this->cost < key.cost; }
};
vector<edge> v;

namespace uf {
ll p[2 * N];
bool t[2 * N];
ll rank;
ll par(ll x) { return (x == p[x] ? x : par(p[x])); }
ll unite(ll a, ll b, ll cost) {
  a = par(a), b = par(b);
  if (a == b) {
    return 0;
  }
  p[a] = b;
  rank--;
  return cost;
}
} // namespace uf
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> col[i];
  }
  for (int i = 0; i < m; i++) {
    edge e;
    cin >> e.a >> e.b >> e.cost;
    v.push_back(e);
  }
  sort(v.begin(), v.end());
  uf::rank = k - 1;
  for (int i = 0; i < 2 * N; i++) {
    uf::p[i] = i;
  }
  ll ans = 0;
  for (auto e : v) {
    if (uf::rank <= 0) {
      cout << ans << endl;
      return 0;
    }
    if (col[e.a] > 0)
      e.a = col[e.a] + N;
    if (col[e.b] > 0)
      e.b = col[e.b] + N;
    ans += uf::unite(e.a, e.b, e.cost);
  }
  if (uf::rank <= 0)
    cout << ans << endl;
  else
    cout << -1 << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
bool chmax(ll &a, ll b) {
  if (a > b)
    return 0;
  a = b;
  return 1;
}
bool chmin(ll &a, ll b) {
  if (a < b)
    return 0;
  a = b;
  return 1;
}

#define N 100010
ll n, m, k;
ll col[N];
struct edge {
  ll a, b, cost;
  bool operator<(const edge &key) const { return this->cost < key.cost; }
};
vector<edge> v;

namespace uf {
ll p[2 * N];
bool t[2 * N];
ll rank;
ll par(ll x) { return p[x] = (x == p[x] ? x : par(p[x])); }
ll unite(ll a, ll b, ll cost) {
  a = par(a), b = par(b);
  if (a == b) {
    return 0;
  }
  p[a] = b;
  rank--;
  return cost;
}
} // namespace uf
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> col[i];
  }
  for (int i = 0; i < m; i++) {
    edge e;
    cin >> e.a >> e.b >> e.cost;
    v.push_back(e);
  }
  sort(v.begin(), v.end());
  uf::rank = k - 1;
  for (int i = 0; i < 2 * N; i++) {
    uf::p[i] = i;
  }
  ll ans = 0;
  for (auto e : v) {
    if (uf::rank <= 0) {
      cout << ans << endl;
      return 0;
    }
    if (col[e.a] > 0)
      e.a = col[e.a] + N;
    if (col[e.b] > 0)
      e.b = col[e.b] + N;
    ans += uf::unite(e.a, e.b, e.cost);
  }
  if (uf::rank <= 0)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
Problem p03519 (C++)

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

typedef long long ll;

int N, Q;
ll A[100010], B[100010];
ll ans;
int cnt;
multiset<ll> ms;

int main() {
  cin >> N >> Q;
  for (int i = 0; i < N * 2; i++)
    cin >> A[i];
  for (int i = 0; i < N * 2; i++)
    cin >> B[i];

  for (int i = 1; i < N * 2 - 1; i++) {
    if (A[i] > B[i]) {
      ans += A[i];
      cnt++;
    } else {
      ans += B[i];
    }
    ms.insert(abs(A[i] - B[i]));
  }

  for (int i = 0; i < Q; i++) {
    int id;
    ll a, b;
    cin >> id >> a >> b;
    if (id == 1 || id == 2 * N) {
      A[id - 1] = a;
      B[id - 1] = b;
    } else {
      cnt -= (A[id - 1] > B[id - 1]);
      cnt += (a > b);
      ans += -max(A[id - 1], B[id - 1]) + max(a, b);
      ms.erase(ms.lower_bound(abs(A[id - 1] - B[id - 1])));
      ms.insert(abs(a - b));
      A[id - 1] = a;
      B[id - 1] = b;
    }
    if (cnt % 2 == 0)
      cout << ans + A[0] + A[N * 2 - 1] << endl;
    else
      cout << ans - (*ms.begin()) + A[0] + A[2 * N - 1] << endl;
  }
}


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

typedef long long ll;

int N, Q;
ll A[200010], B[200010];
ll ans;
int cnt;
multiset<ll> ms;

int main() {
  cin >> N >> Q;
  for (int i = 0; i < N * 2; i++)
    cin >> A[i];
  for (int i = 0; i < N * 2; i++)
    cin >> B[i];

  for (int i = 1; i < N * 2 - 1; i++) {
    if (A[i] > B[i]) {
      ans += A[i];
      cnt++;
    } else {
      ans += B[i];
    }
    ms.insert(abs(A[i] - B[i]));
  }

  for (int i = 0; i < Q; i++) {
    int id;
    ll a, b;
    cin >> id >> a >> b;
    if (id == 1 || id == 2 * N) {
      A[id - 1] = a;
      B[id - 1] = b;
    } else {
      cnt -= (A[id - 1] > B[id - 1]);
      cnt += (a > b);
      ans += -max(A[id - 1], B[id - 1]) + max(a, b);
      ms.erase(ms.lower_bound(abs(A[id - 1] - B[id - 1])));
      ms.insert(abs(a - b));
      A[id - 1] = a;
      B[id - 1] = b;
    }
    if (cnt % 2 == 0)
      cout << ans + A[0] + A[N * 2 - 1] << endl;
    else
      cout << ans - (*ms.begin()) + A[0] + A[2 * N - 1] << endl;
  }
}
Problem p03519 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 200100
using namespace std;
typedef long long ll;
typedef pair<ll, ll> par;
par s[maxn << 2];
int n, a[maxn], b[maxn], q;
void upd(int o) {
  s[o].first = max(s[o << 1].first + s[o << 1 | 1].first,
                   s[o << 1].second + s[o << 1 | 1].second);
  s[o].second = max(s[o << 1].second + s[o << 1 | 1].first,
                    s[o << 1].first + s[o << 1 | 1].second);
}
void build(int o, int l, int r) {
  if (l == r) {
    s[o] = par(b[l], a[l]);
    return;
  }
  int mid = l + r >> 1;
  build(o << 1, l, mid);
  build(o << 1 | 1, mid + 1, r);
  upd(o);
}
void mdy(int o, int l, int r, int k, par p) {
  if (l == r) {
    s[o] = p;
    return;
  }
  int mid = l + r >> 1;
  if (k <= mid)
    mdy(o << 1, l, mid, k, p);
  else
    mdy(o << 1 | 1, mid + 1, r, k, p);
  upd(o);
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= 2 * n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= 2 * n; ++i)
    scanf("%d", &b[i]);
  build(1, 2, 2 * n - 1);
  for (int i = 1, p, x, y; i <= q; ++i) {
    scanf("%d%d%d", &p, &x, &y);
    if (2 <= p && p <= 2 * n - 1)
      mdy(1, 2, 2 * n - 1, p, par(y, x));
    else
      a[p] = x, b[p] = y;
    printf("%lld\n", a[1] + a[2 * n] + s[1].first);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 200100
using namespace std;
typedef long long ll;
typedef pair<ll, ll> par;
par s[maxn << 2];
int n, a[maxn], b[maxn], q;
void upd(int o) {
  s[o].first = max(s[o << 1].first + s[o << 1 | 1].first,
                   s[o << 1].second + s[o << 1 | 1].second);
  s[o].second = max(s[o << 1].second + s[o << 1 | 1].first,
                    s[o << 1].first + s[o << 1 | 1].second);
}
void build(int o, int l, int r) {
  if (l == r) {
    s[o] = par(b[l], a[l]);
    return;
  }
  int mid = l + r >> 1;
  build(o << 1, l, mid);
  build(o << 1 | 1, mid + 1, r);
  upd(o);
}
void mdy(int o, int l, int r, int k, par p) {
  if (l == r) {
    s[o] = p;
    return;
  }
  int mid = l + r >> 1;
  if (k <= mid)
    mdy(o << 1, l, mid, k, p);
  else
    mdy(o << 1 | 1, mid + 1, r, k, p);
  upd(o);
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= 2 * n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= 2 * n; ++i)
    scanf("%d", &b[i]);
  if (n > 1)
    build(1, 2, 2 * n - 1);
  for (int i = 1, p, x, y; i <= q; ++i) {
    scanf("%d%d%d", &p, &x, &y);
    if (2 <= p && p <= 2 * n - 1)
      mdy(1, 2, 2 * n - 1, p, par(y, x));
    else
      a[p] = x, b[p] = y;
    printf("%lld\n", a[1] + a[2 * n] + s[1].first);
  }
}
Problem p03519 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define mp make_pair
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 60;
const double eps = 1e-5;
const ll MOD = 1000000007LL;
template <typename T> void chmin(T &a, T b) { a = min(a, b); };
template <typename T> void chmax(T &a, T b) { a = max(a, b); };
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int n, q;
  cin >> n >> q;
  n *= 2;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<ll> st;
  int c = 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (i == 0 || i == n - 1) {
      ans += a[i];
    } else {
      st.insert(abs(a[i] - b[i]));
      ans += max(a[i], b[i]);
      if (a[i] > b[i])
        c ^= 1;
    }
  }
  while (q--) {
    ll p, x, y;
    cin >> p >> x >> y;
    --p;
    if (p == 0 || p == 2 * n - 1) {
      ans += x - a[p];
      a[p] = x;
    } else {
      if (a[p] > b[p])
        c ^= 1;
      if (x > y)
        c ^= 1;
      ans += max(x, y) - max(a[p], b[p]);
      st.erase(st.lower_bound(abs(a[p] - b[p])));
      st.insert(abs(x - y));
      a[p] = x, b[p] = y;
    }
    if (c) {
      cout << ans - (*st.begin()) << endl;
    } else {
      cout << ans << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define mp make_pair
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 60;
const double eps = 1e-5;
const ll MOD = 1000000007LL;
template <typename T> void chmin(T &a, T b) { a = min(a, b); };
template <typename T> void chmax(T &a, T b) { a = max(a, b); };
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int n, q;
  cin >> n >> q;
  n *= 2;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<ll> st;
  int c = 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (i == 0 || i == n - 1) {
      ans += a[i];
    } else {
      st.insert(abs(a[i] - b[i]));
      ans += max(a[i], b[i]);
      if (a[i] > b[i])
        c ^= 1;
    }
  }
  while (q--) {
    ll p, x, y;
    cin >> p >> x >> y;
    --p;
    if (p == 0 || p == n - 1) {
      ans += x - a[p];
      a[p] = x;
    } else {
      if (a[p] > b[p])
        c ^= 1;
      if (x > y)
        c ^= 1;
      ans += max(x, y) - max(a[p], b[p]);
      st.erase(st.lower_bound(abs(a[p] - b[p])));
      st.insert(abs(x - y));
      a[p] = x, b[p] = y;
    }
    if (c) {
      cout << ans - (*st.begin()) << endl;
    } else {
      cout << ans << endl;
    }
  }
}
Problem p03523 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define DEC(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
typedef long long ll;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const ll LINF = (int)1e18;
const double EPS = 1e-9;

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

  string S;
  cin >> S;
  vector<int> pos{0, 4, 6, 8};
  REP(i, pos.size()) {
    if (S[pos[i]] != 'A')
      S.insert(pos[i], "A");
  }
  if (S == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define DEC(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
typedef long long ll;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const ll LINF = (int)1e18;
const double EPS = 1e-9;

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

  string S;
  cin >> S;
  vector<int> pos{0, 4, 6, 8};
  REP(i, pos.size()) {
    if (pos[i] > S.size())
      break;
    if (S[pos[i]] != 'A')
      S.insert(pos[i], "A");
  }
  if (S == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03523 (C++)

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

int main() {
  string str;
  cin >> str;
  bool chk = true;
  int index = 0;

  if (str.at(0) != 'A') {
    str = 'A' + str;
  }

  if (str.at(4) != 'A') {
    str = str.substr(0, 4) + 'A' + str.substr(4);
  }

  if (str.at(6) != 'A') {
    str = str.substr(0, 6) + 'A' + str.substr(6);
  }

  if (str.size() == 8) {
    str = str + 'A';
  }

  if (str == "AKIHABARA")
    cout << "YES";
  else
    cout << "NO";
}


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

int main() {
  string str;
  cin >> str;
  bool chk = true;
  int index = 0;

  if (str.size() < 5) {
    cout << "NO";
    return 0;
  }

  if (str.at(0) != 'A') {
    str = 'A' + str;
  }

  if (str.at(4) != 'A') {
    str = str.substr(0, 4) + 'A' + str.substr(4);
  }

  if (str.at(6) != 'A') {
    str = str.substr(0, 6) + 'A' + str.substr(6);
  }

  if (str.size() == 8) {
    str = str + 'A';
  }

  if (str == "AKIHABARA")
    cout << "YES";
  else
    cout << "NO";
}
Problem p03523 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const string w = "AKIHABARA";
int main() {
  string s;
  cin >> s;

  for (int i = 0; i < w.size(); i++) {
    if (s.size() < i || (w[i] != s[i] && w[i] == 'A')) {
      string t = "A";
      s.insert(i, t);
    }
  }
  if (s != w) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const string w = "AKIHABARA";
int main() {
  string s;
  cin >> s;

  for (int i = 0; i < w.size(); i++) {
    if (s.size() - 1 < i || (w[i] != s[i] && w[i] == 'A')) {
      string t = "A";
      s.insert(i, t);
    }
  }
  if (s != w) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
  }
}
Problem p03523 (C++)

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

int main() {
  string S;
  cin >> S;
  if (S[0] != 'A')
    S = "A" + S;
  if (S[4] != 'A')
    S = S.substr(0, 4) + "A" + S.substr(4);
  if (S[6] != 'A')
    S = S.substr(0, 6) + "A" + S.substr(6);
  if (S[8] != 'A')
    S = S + "A";
  if (S == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

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

int main() {
  string S;
  cin >> S;
  int N = S.length();
  if (N < 5) {
    cout << "NO" << endl;
    return 0;
  }
  if (S[0] != 'A')
    S = "A" + S;
  if (S[4] != 'A')
    S = S.substr(0, 4) + "A" + S.substr(4);
  if (S[6] != 'A')
    S = S.substr(0, 6) + "A" + S.substr(6);
  if (S[8] != 'A')
    S = S + "A";
  if (S == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03523 (C++)

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

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}

// 実行部
void Main() {
  str s;
  cin >> s;
  str flag = "AKIHABARA";
  if (s[0] != 'A') {
    s = "A" + s;
  }
  if (s[4] != 'A') {
    s = s.substr(0, 4) + "A" + s.substr(4);
  }
  if (s[6] != 'A') {
    s = s.substr(0, 6) + "A" + s.substr(6);
  }
  if (s[8] != 'A') {
    s = s + "A";
  }
  if (s == flag)
    fin("YES");
  fin("NO");
}

// 前処理
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20); // 高精度少数表示
  Main();
  return 0;
}

/*
//test時
  g++ test.cpp -o test;./test
*/


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

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}

// 実行部
void Main() {
  str s;
  cin >> s;
  if (s.size() < 5)
    fin("NO");
  str flag = "AKIHABARA";
  if (s[0] != 'A') {
    s = "A" + s;
  }
  if (s[4] != 'A') {
    s = s.substr(0, 4) + "A" + s.substr(4);
  }
  if (s[6] != 'A') {
    s = s.substr(0, 6) + "A" + s.substr(6);
  }
  if (s[8] != 'A') {
    s = s + "A";
  }
  if (s == flag)
    fin("YES");
  fin("NO");
}

// 前処理
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20); // 高精度少数表示
  Main();
  return 0;
}

/*
//test時
  g++ test.cpp -o test;./test
*/
Problem p03523 (C++)

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

signed main(void) {
  string s;
  int a[4];

  a[0] = 0;
  a[1] = 4;
  a[2] = 6;
  a[3] = 8;
  cin >> s;
  rep(i, 4) {
    if (s[a[i]] == 'A')
      continue;
    else
      s.insert(s.begin() + a[i], 'A');
  }
  if (s == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


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

signed main(void) {
  string s;
  int a[4];

  a[0] = 0;
  a[1] = 4;
  a[2] = 6;
  a[3] = 8;
  cin >> s;
  rep(i, 4) {
    if (s.size() >= a[i]) {
      if (s[a[i]] == 'A')
        continue;
      else
        s.insert(s.begin() + a[i], 'A');
    }
  }
  if (s == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03523 (Python)

Buggy submission (status: Runtime Error):
from itertools import product


candidates = []
non_as = ["KIH", "B", "R", ""]
for seps in product(["A", ""], repeat=4):
    candidates.add("".append(sep + non_a for sep, non_a in zip(seps, non_as)))

print("YES") if input() in candidates else print("NO")


Fixed submission:
from itertools import product


candidates = []
non_as = ["KIH", "B", "R", ""]
for seps in product(["A", ""], repeat=4):
    candidates.append("".join(sep + non_a for sep, non_a in zip(seps, non_as)))

print("YES") if input() in candidates else print("NO")
Problem p03524 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
char s[N];
int cnt[3], n, mx;
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  fp(i, 1, n)++ cnt[s[i] - 'a'];
  mx = max(cnt[0], max(cnt[1], cnt[2]));
  if (!cnt[0] || !cnt[1] || !cnt[2])
    return puts(mx <= 1 ? "YES" : "NO");
  fp(i, 0, 2) if (cnt[i] < mx - 1) return puts("NO"), 0;
  puts("YES");
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
char s[N];
int cnt[3], n, mx;
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  fp(i, 1, n)++ cnt[s[i] - 'a'];
  mx = max(cnt[0], max(cnt[1], cnt[2]));
  if (!cnt[0] || !cnt[1] || !cnt[2])
    return puts(mx <= 1 ? "YES" : "NO"), 0;
  fp(i, 0, 2) if (cnt[i] < mx - 1) return puts("NO"), 0;
  puts("YES");
  return 0;
}
Problem p03524 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 5;
char str[N];
int cnt[3];
int main() {
  scanf("%s", str);
  for (char c : str) {
    ++cnt[c - 'a'];
  }
  sort(cnt, cnt + 3);
  printf(cnt[2] - cnt[0] > 1 ? "NO\n" : "YES\n");
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 5;
char str[N];
int cnt[3];
int main() {
  scanf("%s", str);
  for (int i = 0; str[i]; ++i) {
    ++cnt[str[i] - 'a'];
  }
  sort(cnt, cnt + 3);
  printf(cnt[2] - cnt[0] > 1 ? "NO\n" : "YES\n");
}
Problem p03524 (C++)

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

vector<vector<int>> memo;
int solve(vector<int> v, int now, const vector<int> &nums) {
  if (now == 13) {
  }
  return 1;
}
int main() {
  memo = vector<vector<int>>(13, vector<int>((1 << 24)));

  string st;
  cin >> st;
  vector<int> nums(3);
  for (auto c : st) {
    nums[c - 'a']++;
  }
  bool ok = true;
  if (st.size() == 1) {
    ok = true;
  } else {
    sort(nums.begin(), nums.end());
    for (auto n : nums) {
      if (n <= nums[0] + 1) {

      } else {
        ok = false;
      }
    }
  }
  if (ok)
    cout << "YES" << endl;
  else {
    cout << "NO" << endl;
  }

  // int N; cin >> N;
  // vector<int>nums(13);
  // for (int i = 0; i < N; ++i) {
  //	int a; cin >> a;
  //	nums[a]++;
  // }

  return 0;
}

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

vector<vector<int>> memo;
int solve(vector<int> v, int now, const vector<int> &nums) {
  if (now == 13) {
  }
  return 1;
}
int main() {
  memo = vector<vector<int>>(13, vector<int>((1 << 12)));

  string st;
  cin >> st;
  vector<int> nums(3);
  for (auto c : st) {
    nums[c - 'a']++;
  }
  bool ok = true;
  if (st.size() == 1) {
    ok = true;
  } else {
    sort(nums.begin(), nums.end());
    for (auto n : nums) {
      if (n <= nums[0] + 1) {

      } else {
        ok = false;
      }
    }
  }
  if (ok)
    cout << "YES" << endl;
  else {
    cout << "NO" << endl;
  }

  // int N; cin >> N;
  // vector<int>nums(13);
  // for (int i = 0; i < N; ++i) {
  //	int a; cin >> a;
  //	nums[a]++;
  // }

  return 0;
}
Problem p03524 (Python)

Buggy submission (status: Runtime Error):
s = input()
K = int(input())
s_dic = {c: [] for c in "abcdefghijklmnopqrstuvwxyz"}
for i, c in enumerate(s):
    s_dic[c].append(i)
substrings = set()
for lc in "abcdefghijklmnopqrstuvwxyz":
    for i in s_dic[lc]:
        for j in range(i + 1, len(s) + 1):
            substrings.add(s[i:j])
    if len(substrings) >= K:
        break
print(sorted(list(substrings))[K - 1])


Fixed submission:
from collections import Counter


S = input()
C = Counter(S)
vs = sorted(list(C.values()))
if len(vs) == 1:
    print("YES") if vs[0] == 1 else print("NO")
elif len(vs) == 2:
    print("YES") if vs[0] == vs[1] and vs[0] == 1 else print("NO")
else:
    print("YES") if vs[2] - 1 <= vs[0] and vs[2] - 1 <= vs[1] else print("NO")
Problem p03525 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;

  vector<int> d(n);
  rep(i, n) { cin >> d[i]; }

  if (24 <= n) {
    cout << 0 << endl;
    return 0;
  }

  int ans = 0;
  for (int b = 0; b < (1 << n); b++) {

    vector<int> t(n + 1);
    rep(i, n) {
      if (b & 1 << i) {
        t[i] = 24 - d[i];
      } else {
        t[i] = d[i];
      }
    }

    int u = 30;
    rep(i, n) {
      for (int j = i + 1; j <= n; j++) {
        int x = abs(t[i] - t[j]);
        int y = min(x, 24 - x);
        u = min(u, y);
      }
    }

    ans = max(ans, u);
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;

  vector<int> d(n);
  rep(i, n) { cin >> d[i]; }

  if (24 <= n) {
    cout << 0 << endl;
    return 0;
  }

  if (n == 23) {

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

    for (int i = 0; i <= 20; i += 2) {
      int k = i / 2 + 1;
      if (d[i] != k || d[i + 1] != k) {
        cout << 0 << endl;
        return 0;
      }
    }

    if (d[22] != 12) {
      cout << 0 << endl;
      return 0;
    }

    cout << 1 << endl;
    return 0;
  }

  int ans = 0;
  for (int b = 0; b < (1 << n); b++) {

    vector<int> t(n + 1);
    rep(i, n) {
      if (b & 1 << i) {
        t[i] = 24 - d[i];
      } else {
        t[i] = d[i];
      }
    }

    int u = 30;
    rep(i, n) {
      for (int j = i + 1; j <= n; j++) {
        int x = abs(t[i] - t[j]);
        int y = min(x, 24 - x);
        u = min(u, y);
      }
    }

    ans = max(ans, u);
  }

  cout << ans << endl;
  return 0;
}
Problem p03525 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
d = sorted(list(map(int, input().split())) + [0])

bit, cnt = [0] * 24, 1
for num in d:
    bit[(num - 12) * cnt + 12] += 1
    cnt *= -1

if any(i > 1 for i in bit):
    print(0)
    exit()
ans = [i for i, li in enumerate(bit * 2) if li == 1]
s = min(ans[i] - ans[i - 1] for i in range(1, len(ans)))
print(s)


Fixed submission:
n = int(input())
d = sorted(list(map(int, input().split())) + [0])

bit, cnt = [0] * 24, 1
for num in d:
    bit[((num - 12) * cnt + 12) % 24] += 1
    cnt *= -1

if any(i > 1 for i in bit):
    print(0)
    exit()
ans = [i for i, li in enumerate(bit * 2) if li == 1]
s = min(ans[i] - ans[i - 1] for i in range(1, len(ans)))
print(s)
Problem p03525 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  int d[51] = {};
  for (int i = 1; i < n + 1; i++) {
    cin >> d[i];
  }
  if (n + 1 >= 25) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    int nowd[51] = {};
    for (int j = 1; j < n + 1; j++) {
      if ((i & (1 << (j - 1))) == 0 || d[j] == 0)
        nowd[j] = d[j];
      else
        nowd[j] = -d[j];
    }
    sort(nowd, nowd + n + 1);
    int s = 24 - (nowd[n] - nowd[0]);
    // cout << i << endl;
    // for(int j = 0; j < n + 1; j++){
    // 	cout << nowd[j] << " ";
    // }
    // cout << endl;
    for (int j = 0; j < n; j++) {
      s = min(s, nowd[j + 1] - nowd[j]);
    }
    // cout << s << endl;
    ans = max(ans, s);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  int d[51] = {};
  for (int i = 1; i < n + 1; i++) {
    cin >> d[i];
  }
  if (n + 1 >= 25) {
    cout << 0 << endl;
    return 0;
  }
  if (n + 1 == 24) {
    int cnt[13] = {};
    for (int i = 0; i < n; i++) {
      cnt[d[i]]++;
    }
    int ans = 1;
    if (cnt[0] >= 2 || cnt[12] >= 2)
      ans = 0;
    for (int i = 1; i <= 11; i++) {
      if (cnt[i] > 2)
        ans = 0;
    }
    cout << ans << endl;
    return 0;
  }
  int ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    int nowd[51] = {};
    for (int j = 1; j < n + 1; j++) {
      if ((i & (1 << (j - 1))) == 0 || d[j] == 0)
        nowd[j] = d[j];
      else
        nowd[j] = -d[j];
    }
    sort(nowd, nowd + n + 1);
    int s = 24 - (nowd[n] - nowd[0]);
    // cout << i << endl;
    // for(int j = 0; j < n + 1; j++){
    // 	cout << nowd[j] << " ";
    // }
    // cout << endl;
    for (int j = 0; j < n; j++) {
      s = min(s, nowd[j + 1] - nowd[j]);
    }
    // cout << s << endl;
    ans = max(ans, s);
  }
  cout << ans << endl;
  return 0;
}
Problem p03525 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  int n;
  cin >> n;
  vector<int> d(n);
  rep(i, n) cin >> d[i];
  if (n >= 24) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  rep(i, 1 << n) {
    vector<int> v;
    rep(j, n) {
      if (i >> j & 1) {
        v.push_back(d[j]);
      } else
        v.push_back(24 - d[j]);
    }
    sort(v.begin(), v.end());
    int now = v[0];
    rep(i, n - 1) now = min(now, v[i + 1] - v[i]);
    now = min(now, 24 - v[(int)v.size() - 1]);
    ans = max(ans, now);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  int n;
  cin >> n;
  vector<int> d(n);
  rep(i, n) cin >> d[i];
  if (n >= 24) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> cnt(25);
  if (n == 23) {
    rep(i, n) cnt[d[i]]++;
    if (cnt[12] >= 2) {
      cout << 0 << endl;
      return 0;
    }
    bool ok = false;
    rep(i, n) {
      if (cnt[i] >= 3)
        ok = true;
    }
    cout << (ok ? 0 : 1) << endl;
    return 0;
  }
  int ans = 0;
  rep(i, 1 << n) {
    vector<int> v;
    rep(j, n) {
      if (i >> j & 1) {
        v.push_back(d[j]);
      } else
        v.push_back(24 - d[j]);
    }
    sort(v.begin(), v.end());
    int now = v[0];
    rep(i, n - 1) now = min(now, v[i + 1] - v[i]);
    now = min(now, 24 - v[(int)v.size() - 1]);
    ans = max(ans, now);
  }
  cout << ans << endl;
}
Problem p03525 (C++)

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

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e5 + 7;

int n, a[51], u[56];
int res = 0;
int main() {
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    u[a[i]]++;
  }
  a[n + 1] = 0;
  u[0]++;

  n++;

  for (int i = 0; i <= 12; i++) {
    if (u[a[i]] > 2) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (u[0] == 2 || n > 25) {
    cout << 0;
    return 0;
  }
  if (n == 25) {
    cout << 1 << endl;
    return 0;
  }
  sort(a + 1, a + n + 1);

  for (int i = 0; i < (1 << n); i++) {
    deque<int> v1, v;
    for (int j = 1; j <= n; j++) {
      if ((1 << (j - 1)) & i)
        v1.push_front(24 - a[j]);
      else
        v.pb(a[j]);
    }
    for (int j = 0; j < v1.size(); j++) {
      v.pb(v1[j]);
    }
    int ans = inf;
    for (int j = 0; j < v.size() - 1; j++) {
      ans = min(ans, min(abs(v[j] - v[j + 1]), abs(24 - abs(v[j] - v[j + 1]))));
    }
    ans = min(ans, min(abs(v[0] - v[v.size() - 1]),
                       abs(24 - abs(v[0] - v[v.size() - 1]))));
    res = max(res, ans);
  }

  cout << res << endl;
}


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

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e5 + 7;

int n, a[51], u[56];
int res = 0;
int main() {
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    u[a[i]]++;
  }
  a[n + 1] = 0;
  u[0]++;

  n++;

  for (int i = 0; i <= 12; i++) {
    if (u[a[i]] > 2) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (u[0] == 2 || n > 25) {
    cout << 0;
    return 0;
  }
  if (n >= 24) {
    cout << 1 << endl;
    return 0;
  }
  sort(a + 1, a + n + 1);

  for (int i = 0; i < (1 << n); i++) {
    deque<int> v1, v;
    for (int j = 1; j <= n; j++) {
      if ((1 << (j - 1)) & i)
        v1.push_front(24 - a[j]);
      else
        v.pb(a[j]);
    }
    for (int j = 0; j < v1.size(); j++) {
      v.pb(v1[j]);
    }
    int ans = inf;
    for (int j = 0; j < v.size() - 1; j++) {
      ans = min(ans, min(abs(v[j] - v[j + 1]), abs(24 - abs(v[j] - v[j + 1]))));
    }
    ans = min(ans, min(abs(v[0] - v[v.size() - 1]),
                       abs(24 - abs(v[0] - v[v.size() - 1]))));
    res = max(res, ans);
  }

  cout << res << endl;
}
Problem p03525 (C++)

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

int rec[55], tmp[55], cur[55];
int n, ans;

void dfs(int i) {
  if (i == n) {
    memcpy(tmp, cur, sizeof tmp);
    sort(tmp, tmp + n);
    int mx = 24;
    for (int i = 0, j = n - 1; i < n; j = i++) {
      int t = abs(tmp[i] - tmp[j]);
      mx = min(mx, min(t, 24 - t));
    }
    ans = max(ans, mx);
    return;
  }
  for (int j = -1; j < 2; j += 2) {
    cur[i] = rec[i] * j;
    dfs(i + 1);
    if (!rec[i] || rec[i] == 12) {
      break;
    }
  }
}

int main() {
  scanf("%d", &n);
  if (n > 23) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", rec + i);
  }
  n++;
  cur[1] = rec[1];
  dfs(2);
  printf("%d", ans);
  while (1)
    ;
}

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

int rec[55], tmp[55], cur[55];
int n, ans;

void dfs(int i) {
  if (i == n) {
    memcpy(tmp, cur, sizeof tmp);
    sort(tmp, tmp + n);
    int mx = 24;
    for (int i = 0, j = n - 1; i < n; j = i++) {
      int t = abs(tmp[i] - tmp[j]);
      mx = min(mx, min(t, 24 - t));
    }
    ans = max(ans, mx);
    return;
  }
  for (int j = -1; j < 2; j += 2) {
    cur[i] = rec[i] * j;
    dfs(i + 1);
    if (!rec[i] || rec[i] == 12) {
      break;
    }
  }
}

int main() {
  scanf("%d", &n);
  if (n > 23) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", rec + i);
  }
  n++;
  cur[1] = rec[1];
  dfs(2);
  printf("%d", ans);
}
Problem p03525 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> d(n);
  rep(snip_i, n) cin >> d.at(snip_i);
  sort(d.begin(), d.end());
  vector<vector<int>> time(24, vector<int>());
  rep(i, n) {
    int add = d.at(i);
    time.at(add).push_back(i);
  }
  bool flag = false;
  int bf = 0, ans = 25;
  rep(i, 24) {
    int comp = -1;
    int s = time.at(i).size();
    if (s == 0)
      continue;
    else if (s == 1) {
      if (i < 12) {
        if (flag == true) {
          int tr = 24 - i;
          time.at(tr).push_back(time.at(i).at(0));
          flag = false;
        } else {
          comp = i;
          flag = true;
        }
      } else {
        comp = i;
      }
    } else if (s == 2) {
      if (i >= 12) {
        cout << 0 << endl;
        return 0;
      }
      comp = i;
      int tr = 24 - i;
      time.at(tr).push_back(time.at(i).at(0));
    } else {
      cout << 0 << endl;
      return 0;
    }
    if (comp != -1) {
      int diff = comp - bf;
      ans = min(ans, diff);
      bf = comp;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> d(n);
  rep(snip_i, n) cin >> d.at(snip_i);
  sort(d.begin(), d.end());
  vector<vector<int>> time(24, vector<int>());
  rep(i, n) {
    int add = d.at(i);
    time.at(add).push_back(i);
  }
  bool flag = false;
  int bf = 0, ans = 25;
  rep(i, 24) {
    int comp = -1;
    int s = time.at(i).size();
    if (i == 0 && s != 0) {
      cout << 0 << endl;
      return 0;
    }
    if (s == 0)
      continue;
    else if (s == 1) {
      if (i < 12) {
        if (flag == true) {
          int tr = 24 - i;
          time.at(tr).push_back(time.at(i).at(0));
          flag = false;
        } else {
          comp = i;
          flag = true;
        }
      } else {
        comp = i;
      }
    } else if (s == 2) {
      if (i >= 12) {
        cout << 0 << endl;
        return 0;
      }
      comp = i;
      int tr = 24 - i;
      time.at(tr).push_back(time.at(i).at(0));
    } else {
      cout << 0 << endl;
      return 0;
    }
    if (comp != -1) {
      int diff = comp - bf;
      ans = min(ans, diff);
      bf = comp;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03525 (C++)

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

int main() {
  int N;
  cin >> N;

  vector<vector<bool>> dp(N + 1, vector<bool>(1 << 24, false));
  dp[0][1] = true;

  int D;
  for (int i = 1; i <= N; i++) {
    cin >> D;
    if (D == 0) {
      continue;
    }
    for (int j = 0; j < (1 << 24); j++) {
      if (dp[i - 1][j]) {
        if ((j >> D) ^ 1) {
          dp[i][(j | (1 << D))] = true;
        }
        if ((j >> (24 - D)) ^ 1) {
          dp[i][(j | (1 << (24 - D)))] = true;
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < (1 << 24); i++) {
    if (dp[N][i]) {
      int s = INT_MAX;
      bitset<24> b(i);
      for (int j = 0; j < 24; j++) {
        for (int k = 0; k < j; k++) {
          if (b[j] && b[k]) {
            s = min(s, min((j - k), 24 - (j - k)));
          }
        }
      }
      assert(b.count() == N + 1);
      if (s != INT_MAX && b.count() == N + 1) {
        ans = max(ans, s);
      }
    }
  }
  cout << ans << endl;
}


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

int main() {
  int N;
  cin >> N;

  vector<vector<bool>> dp(N + 1, vector<bool>(1 << 24, false));
  dp[0][1] = true;

  int D;
  for (int i = 1; i <= N; i++) {
    cin >> D;
    if (D == 0) {
      continue;
    }
    for (int j = 0; j < (1 << 24); j++) {
      if (dp[i - 1][j]) {
        if ((j >> D) ^ 1) {
          dp[i][(j | (1 << D))] = true;
        }
        if ((j >> (24 - D)) ^ 1) {
          dp[i][(j | (1 << (24 - D)))] = true;
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < (1 << 24); i++) {
    if (dp[N][i]) {
      int s = INT_MAX;
      bitset<24> b(i);
      for (int j = 0; j < 24; j++) {
        for (int k = 0; k < j; k++) {
          if (b[j] && b[k]) {
            s = min(s, min((j - k), 24 - (j - k)));
          }
        }
      }
      // assert(b.count() == N + 1);
      if (s != INT_MAX && b.count() == N + 1) {
        ans = max(ans, s);
      }
    }
  }
  cout << ans << endl;
}