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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int n;
vector<int> s(50);
vector<int> a(52);

int dfs(int k) {
  if (k == n) {
    int ret = 24;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int r = abs(s[i] - s[j]);
        ret = min(ret, min(24 - r, r));
      }
    }
    return ret;
  }
  int ret = 0;
  if (a[k] == 0 || a[k] == 12) {
    s[k] = a[k];
    ret = max(ret, dfs(k + 1));
  } else {
    s[k] = a[k];
    ret = max(ret, dfs(k + 1));
    s[k] = 24 - a[k];
    ret = max(ret, dfs(k + 1));
  }
  return ret;
}

int main() {
  cin >> n;
  a[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
  }
  n++;
  if (n > 24) {
    puts("0");
    return 0;
  }
  cout << dfs(0) << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int n;
vector<int> s(50);
vector<int> a(52);

int dfs(int k) {
  if (k == n) {
    int ret = 24;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int r = abs(s[i] - s[j]);
        ret = min(ret, min(24 - r, r));
      }
    }
    return ret;
  }
  int ret = 0;
  if (a[k] == 0 || a[k] == 12) {
    s[k] = a[k];
    ret = max(ret, dfs(k + 1));
  } else {
    s[k] = a[k];
    ret = max(ret, dfs(k + 1));
    s[k] = 24 - a[k];
    ret = max(ret, dfs(k + 1));
  }
  return ret;
}

int main() {
  cin >> n;
  a[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
  }
  n++;
  if (n > 24) {
    puts("0");
    return 0;
  }
  cout << dfs(0) << endl;
  return 0;
}
Problem p03525 (C++)

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

signed main() {
#ifdef LOCAL
  assert(freopen("c.in", "r", stdin));
#endif
  int n;
  cin >> n;
  vector<int> d(n);
  for (int i = 0; i < n; ++i) {
    cin >> d[i];
  }
  if (n >= 24) {
    cout << 0 << '\n';
    return 0;
  }
  int res = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    vector<int> pos(n + 1);
    pos[n] = 0;
    for (int i = 0; i < n; ++i) {
      if (mask & (1 << i)) {
        pos[i] = d[i];
      } else {
        pos[i] = 24 - d[i];
      }
    }
    sort(pos.begin(), pos.end());
    int cur = 24;
    for (int i = 0; i < n; ++i) {
      cur = min(cur, pos[i + 1] - pos[i]);
    }
    cur = min(cur, 24 - pos[n]);
    res = max(res, cur);
  }
  cout << res << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

signed main() {
#ifdef LOCAL
  assert(freopen("c.in", "r", stdin));
#endif
  int n;
  cin >> n;
  vector<int> d(n);
  for (int i = 0; i < n; ++i) {
    cin >> d[i];
  }
  if (n >= 24) {
    cout << 0 << '\n';
    return 0;
  }
  if (n >= 12) {
    map<int, int> cnt;
    cnt[0]++;
    cnt[0]++;
    cnt[12]++;
    for (int x : d) {
      if (++cnt[x] > 2) {
        cout << 0 << '\n';
        return 0;
      }
    }
    cout << 1 << '\n';
    return 0;
  }
  int res = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    vector<int> pos(n + 1);
    pos[n] = 0;
    for (int i = 0; i < n; ++i) {
      if (mask & (1 << i)) {
        pos[i] = d[i];
      } else {
        pos[i] = 24 - d[i];
      }
    }
    sort(pos.begin(), pos.end());
    int cur = 24;
    for (int i = 0; i < n; ++i) {
      cur = min(cur, pos[i + 1] - pos[i]);
    }
    cur = min(cur, 24 - pos[n]);
    res = max(res, cur);
  }
  cout << res << '\n';
}
Problem p03525 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll, ll> pii;

int main() {
  ll N;
  cin >> N;
  vi E(N);
  REP(i, N) cin >> E[i];
  if (N >= 24) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  E.PB(0);
  N += 1;
  vi v(N);
  REP(i, (1ll << N)) {
    REP(j, N)
    if (i & (1ll << j))
      v[j] = E[j];
    else
      v[j] = 24 - E[j];
    sort(ALL(v));
    ll mi = INF;
    REP(j, N - 1) mi = min(mi, v[j + 1] - v[j]);
    mi = min(mi, v[0] + 24 - v[N - 1]);
    ans = max(ans, mi);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll, ll> pii;

int main() {
  ll N;
  cin >> N;
  vi E(N);
  REP(i, N) cin >> E[i];
  if (N == 23) {
    UM<ll, ll> cnt;
    for (ll i : E)
      cnt[i]++;
    cnt[0]++;
    bool flag = false;
    REP(i, 12) if (cnt[i] > 2) flag = true;
    if (cnt[12] > 1)
      flag = true;
    if (flag)
      cout << 0 << endl;
    else
      cout << 1 << endl;
    return 0;
  }
  if (N >= 24) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  E.PB(0);
  N += 1;
  vi v(N);
  REP(i, (1ll << N)) {
    REP(j, N)
    if (i & (1ll << j))
      v[j] = E[j];
    else
      v[j] = 24 - E[j];
    sort(ALL(v));
    ll mi = INF;
    REP(j, N - 1) mi = min(mi, v[j + 1] - v[j]);
    mi = min(mi, v[0] + 24 - v[N - 1]);
    ans = max(ans, mi);
  }
  cout << ans << endl;
}
Problem p03525 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P>> G;
void g_dir(G &graph, ll a, ll b, ll w = 1) { graph[a].push_back(P(b, w)); }
void g_undir(G &graph, ll a, ll b, ll w = 1) {
  g_dir(graph, a, b, w);
  g_dir(graph, b, a, w);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; (i)--)
template <class T> void chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
  }
}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

int main() {

  ll N;
  cin >> N;
  V cnt(13, 0);
  cnt[0] = 1;
  rep(i, N) {
    ll a;
    cin >> a;
    cnt[a]++;
  }
  ll flg = 0;
  if (cnt[0] >= 2 || cnt[12] >= 2)
    flg = 1;
  rep(i, 13) if (cnt[i] >= 3) flg = 1;
  if (flg) {
    cout << 0 << endl;
    return 0;
  }
  return 1;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P>> G;
void g_dir(G &graph, ll a, ll b, ll w = 1) { graph[a].push_back(P(b, w)); }
void g_undir(G &graph, ll a, ll b, ll w = 1) {
  g_dir(graph, a, b, w);
  g_dir(graph, b, a, w);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; (i)--)
template <class T> void chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
  }
}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

int main() {

  ll N;
  cin >> N;
  V cnt(13, 0);
  cnt[0] = 1;
  rep(i, N) {
    ll a;
    cin >> a;
    cnt[a]++;
  }
  ll flg = 0;
  if (cnt[0] >= 2 || cnt[12] >= 2)
    flg = 1;
  rep(i, 13) if (cnt[i] >= 3) flg = 1;
  if (flg) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  rep(i, 1 << 13) {
    V v(25, 0);
    rep(j, 13) {
      ll a = j, b = 24 - j;
      if (cnt[j] == 0)
        continue;
      if (cnt[j] == 2) {
        v[a] = 1;
        v[b] = 1;
        continue;
      }
      if (i & (1 << j))
        v[a] = 1;
      else
        v[b] = 1;
    }
    ll tmp = 999;
    rep(j, 25) rep(k, 25) if (v[j] && v[k] && j != k)
        chmin(tmp, min(abs(j - k), 24 - abs(j - k)));
    chmax(ans, tmp);
  }
  cout << ans << endl;

  return 0;
}
Problem p03526 (C++)

Buggy submission (status: Runtime Error):
// dile a la jardinera que traigo flores
// corner cases // int vs ll // cin vs scanf
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())
#define fill(x, v) memset(x, v, sizeof(x))
#define FER(i, a, b) for (int i = int(a); i < int(b); ++i)
#define trace(x) cout << #x << " = " << x << endl
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define N 1024
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
const long long INF = 1e18;

ll dp[N][N];
vector<ii> vec;

bool cmp(const ii &a, const ii &b) { return b.ff + b.ss > a.ff + a.ss; }

int main() {

  fastio;
  int n;
  cin >> n;
  vec.resize(n);
  // first -> strength second -> weight
  FER(i, 0, n) cin >> vec[i].ff >> vec[i].ss;
  sort(vec.begin(), vec.end(), cmp);

  int ans = 1;
  FER(i, 0, N) FER(j, 0, N) dp[i][j] = INF;
  for (int i = 0; i < n; ++i)
    dp[0][i] = vec[i].ss;
  for (int i = 1; i < n; ++i) {
    ll min_weight = INF;
    FER(j, 0, i) min_weight = min(min_weight, dp[i - 1][j]);
    for (int j = i; j < n; ++j) {
      if (vec[j].ff >= min_weight)
        dp[i][j] = min_weight + vec[j].ss, ans = i + 1;
      min_weight = min(min_weight, dp[i - 1][j]);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// dile a la jardinera que traigo flores
// corner cases // int vs ll // cin vs scanf
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())
#define fill(x, v) memset(x, v, sizeof(x))
#define FER(i, a, b) for (int i = int(a); i < int(b); ++i)
#define trace(x) cout << #x << " = " << x << endl
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define N 5050
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
const long long INF = 1e18;

ll dp[N][N];
vector<ii> vec;

bool cmp(const ii &a, const ii &b) { return b.ff + b.ss > a.ff + a.ss; }

int main() {

  fastio;
  int n;
  cin >> n;
  vec.resize(n);
  // first -> strength second -> weight
  FER(i, 0, n) cin >> vec[i].ff >> vec[i].ss;
  sort(vec.begin(), vec.end(), cmp);

  int ans = 1;
  FER(i, 0, N) FER(j, 0, N) dp[i][j] = INF;
  for (int i = 0; i < n; ++i)
    dp[0][i] = vec[i].ss;
  for (int i = 1; i < n; ++i) {
    ll min_weight = INF;
    FER(j, 0, i) min_weight = min(min_weight, dp[i - 1][j]);
    for (int j = i; j < n; ++j) {
      if (vec[j].ff >= min_weight)
        dp[i][j] = min_weight + vec[j].ss, ans = i + 1;
      min_weight = min(min_weight, dp[i - 1][j]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03526 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long

using namespace std;

const int N = 5010;
const LL inf = 0x3f3f3f3f3f3f3f3f;
struct node {
  LL s, w;
} f[N];
LL dp[N];

int cmp(const node &a, const node &b) {
  if (a.s >= b.w && a.w > b.s)
    return 0;
  return 1;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &f[i].s, &f[i].w);
  sort(f + 1, f + n + 1, cmp);
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = n; j; j--) {
      if (f[i].s >= dp[j - 1])
        dp[j] = min(dp[j], dp[j - 1] + f[i].w);
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (dp[i] < inf)
      ans = i;
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long

using namespace std;

const int N = 5010;
const LL inf = 0x3f3f3f3f3f3f3f3f;
struct node {
  LL s, w;
} f[N];
LL dp[N];

int cmp(const node &a, const node &b) { return a.s + a.w < b.s + b.w; }

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &f[i].s, &f[i].w);
  sort(f + 1, f + n + 1, cmp);
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = n; j; j--) {
      if (f[i].s >= dp[j - 1])
        dp[j] = min(dp[j], dp[j - 1] + f[i].w);
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (dp[i] < inf)
      ans = i;
  printf("%d\n", ans);
}
Problem p03526 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 2000 + 10;
int n, dp[maxn][maxn];
vector<pii> v;

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  sort(v.begin() + 1, v.end(),
       [](auto x, auto y) { return x.first + x.second < y.first + y.second; });
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i < (int)v.size(); i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] = dp[i - 1][j];
      if (dp[i - 1][j - 1] <= v[i].first)
        dp[i][j] = min(dp[i - 1][j - 1] + v[i].second, dp[i][j]);
    }
  }
  int ans = 0;
  for (int i = 1; i < (int)v.size(); i++) {
    if (dp[n][i] != dp[1][0])
      ans = i;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 5000 + 10;
int n;
long long dp[maxn][maxn];
vector<pii> v;

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  sort(v.begin() + 1, v.end(),
       [](auto x, auto y) { return x.first + x.second < y.first + y.second; });
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i < (int)v.size(); i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] = dp[i - 1][j];
      if (dp[i - 1][j - 1] <= v[i].first)
        dp[i][j] = min(dp[i - 1][j - 1] + v[i].second, dp[i][j]);
    }
  }
  int ans = 0;
  for (int i = 1; i < (int)v.size(); i++) {
    if (dp[n][i] != dp[1][0])
      ans = i;
  }
  cout << ans << endl;

  return 0;
}
Problem p03526 (C++)

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

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll, unordered_map<ll, ll>> graph;

const ll e5 = 1 << 20;
const ll mod = 1000003;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

ll n;
ll h[e3], p[e3];
ll idx[e3];
ll dp[e3][e3];

int main() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> h[i] >> p[i];
  iota(idx, idx + n, 0ll);
  sort(idx, idx + n, [](ll a, ll b) { return h[a] + p[a] < h[b] + p[b]; });
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= n; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      if (dp[i][j] <= h[idx[i]]) {
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[idx[i]]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  for (ll j = n; j >= 0; j--) {
    if (dp[n][j] < INF) {
      cout << j << endl;
      return 0;
    }
  }
}


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

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll, unordered_map<ll, ll>> graph;

const ll e5 = 1 << 20;
const ll mod = 1000003;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

ll n;
ll h[e3], p[e3];
ll idx[e3];
ll dp[5002][5002];

int main() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> h[i] >> p[i];
  iota(idx, idx + n, 0ll);
  sort(idx, idx + n, [](ll a, ll b) { return h[a] + p[a] < h[b] + p[b]; });
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= n; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      if (dp[i][j] <= h[idx[i]]) {
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[idx[i]]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  for (ll j = n; j >= 0; j--) {
    if (dp[n][j] < INF) {
      cout << j << endl;
      return 0;
    }
  }
}
Problem p03527 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define FOR(x, xs) for (auto &x : xs)

using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;

struct uf {
  VI dat;
  uf(int n) : dat(n) {
    REP(i, 0, n) { dat[i] = i; }
  }
  int find(int x) {
    if (dat[x] == x) {
      return x;
    }
    return x = find(dat[x]);
  }
  void unite(int x, int y) { dat[find(x)] = find(y); }
};

int main() {
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int l = s.size();
  VI xs(l + 2);
  xs[0] = xs[l + 1] = 0;
  transform(s.begin(), s.end(), xs.begin() + 1, [](char c) { return c - 'a'; });
  transform(xs.begin(), xs.end() - 1, xs.begin() + 1, xs.begin(),
            [](int x, int y) { return (y + 26 - x) % 26; });
  xs.pop_back();
  uf u(l + 1);
  int N;
  cin >> N;
  REP(i, 0, N) {
    int l, r;
    cin >> l >> r;
    u.unite(l - 1, r);
  }
  for (int i = 0, j = l; i < j; i++, j--) {
    u.unite(i, j);
  }
  VI sum(l + 1, 0);
  REP(i, 0, l + 1) {
    sum[u.find(i)] += xs[i];
    sum[u.find(i)] %= 26;
  }
  bool ans = all_of(sum.begin(), sum.end(), [](int x) { return x == 0; });
  cout << (ans ? "YES" : "NO") << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define FOR(x, xs) for (auto &x : xs)

using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;

struct uf {
  VI dat;
  uf(int n) : dat(n) {
    REP(i, 0, n) { dat[i] = i; }
  }
  int find(int x) {
    if (dat[x] == x) {
      return x;
    }
    return dat[x] = find(dat[x]);
  }
  void unite(int x, int y) { dat[find(x)] = find(y); }
};

int main() {
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int l = s.size();
  VI xs(l + 2);
  xs[0] = xs[l + 1] = 0;
  transform(s.begin(), s.end(), xs.begin() + 1, [](char c) { return c - 'a'; });
  transform(xs.begin(), xs.end() - 1, xs.begin() + 1, xs.begin(),
            [](int x, int y) { return (y + 26 - x) % 26; });
  xs.pop_back();
  uf u(l + 1);
  int N;
  cin >> N;
  REP(i, 0, N) {
    int l, r;
    cin >> l >> r;
    u.unite(l - 1, r);
  }
  for (int i = 0, j = l; i < j; i++, j--) {
    u.unite(i, j);
  }
  VI sum(l + 1, 0);
  REP(i, 0, l + 1) {
    sum[u.find(i)] += xs[i];
    sum[u.find(i)] %= 26;
  }
  bool ans = all_of(sum.begin(), sum.end(), [](int x) { return x == 0; });
  cout << (ans ? "YES" : "NO") << endl;

  return 0;
}
Problem p03533 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

const int iinf = 1 << 29;
const long long linf = 1ll << 61;

#define modp(x, y) (((y) + (x) % (y)) % (y))
template <typename T> inline T gcd(T x, T y) {
  if (x < y)
    swap(x, y);
  T mod = x % y;
  if (mod == 0)
    return y;
  return gcd(y, mod);
}
template <typename T> inline T lmc(T x, T y) { return x / gcd(x, y) * y; }

int main(int argc, char *argv[]) {
  char S[32];
  char model[] = "AKIHABARA";
  int L = strlen(model);
  scanf("%s", S);

  int t = 0;
  for (int i = 0; i < L; i++) {
    if (model[i] != S[t]) {
      if (model[i] != 'A') {
        printf("NO\n");
        return 0;
      }
    } else {
      t++;
    }
  }
  printf("YES\n");

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

const int iinf = 1 << 29;
const long long linf = 1ll << 61;

#define modp(x, y) (((y) + (x) % (y)) % (y))
template <typename T> inline T gcd(T x, T y) {
  if (x < y)
    swap(x, y);
  T mod = x % y;
  if (mod == 0)
    return y;
  return gcd(y, mod);
}
template <typename T> inline T lmc(T x, T y) { return x / gcd(x, y) * y; }

int main(int argc, char *argv[]) {
  string S;
  cin >> S;
  if (S == "AKIHABARA" || S == "KIHABARA" || S == "AKIHBARA" ||
      S == "KIHBARA" || S == "AKIHABRA" || S == "KIHABRA" || S == "AKIHBRA" ||
      S == "KIHBRA" || S == "AKIHABAR" || S == "KIHABAR" || S == "AKIHBAR" ||
      S == "KIHBAR" || S == "AKIHABR" || S == "KIHABR" || S == "AKIHBR" ||
      S == "KIHBR")
    printf("YES\n");
  else
    printf("NO\n");

  return 0;
}
Problem p03533 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int popcount(int x) {
  int ret = 0;
  while (x) {
    x &= x - 1;
    ret++;
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int id[4] = {0, 4, 6, 8};
  rep(mask, 0, (1 << 4)) {
    string t = "AKIHABARA";
    rep(i, 0, 4) {
      if ((mask >> i) & 1) {
        t.erase(t.begin() + id[i]);
      }
    }
    if (s == t) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int popcount(int x) {
  int ret = 0;
  while (x) {
    x &= x - 1;
    ret++;
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int id[4] = {0, 4, 6, 8};
  rep(mask, 0, (1 << 4)) {
    string t = "AKIHABARA";
    rrep(i, 0, 4) {
      if ((mask >> i) & 1) {
        t.erase(t.begin() + id[i]);
      }
    }
    if (s == t) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
  return 0;
}
Problem p03533 (C++)

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

int main() {
  string s;
  cin >> s;
  int t[4] = {0, 4, 6, 8};
  for (int i = 0; i < 4; i++) {
    if (s[t[i]] != 'A')
      s.insert(s.begin() + t[i], 'A');
  }
  if (s == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

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

int main() {
  string s;
  cin >> s;
  int t[4] = {0, 4, 6, 8};
  if (s.size() >= 5) {
    for (int i = 0; i < 4; i++) {
      if (s[t[i]] != 'A' && i != 3)
        s.insert(s.begin() + t[i], 'A');
      else if (s.size() == 8)
        s.push_back('A');
    }
  }
  if (s == "AKIHABARA")
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03533 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
char s[16][10];
void init() {
  strcpy(s[0], "AKIHABARA");
  strcpy(s[1], "AKIHABAR");
  strcpy(s[2], "AKIHABRA");
  strcpy(s[3], "AKIHBARA");
  strcpy(s[4], "KIHABARA");
  strcpy(s[5], "AKIHABR");
  strcpy(s[6], "AKIHBAR");
  strcpy(s[7], "KIHABAR");
  strcpy(s[8], "AKIHBRA");
  strcpy(s[9], "KIHABRA");
  strcpy(s[10], "KIHBARA");
  strcpy(s[11], "AKIHBR");
  strcpy(s[12], "KIHABR");
  strcpy(s[13], "KIHBAR");
  strcpy(s[14], "KIHBRA");
  strcpy(s[15], "KIHBR");
}

int main() {
  init();
  int i, ans;
  char S[10];
  while (scanf("%s", S) != EOF) {
    ans = 0;
    for (i = 0; i < 16; i++) {
      if (strcmp(S, s[i]) == 0)
        ans = 1;
    }
    if (ans)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>
char s[16][10];
void init() {
  strcpy(s[0], "AKIHABARA");
  strcpy(s[1], "AKIHABAR");
  strcpy(s[2], "AKIHABRA");
  strcpy(s[3], "AKIHBARA");
  strcpy(s[4], "KIHABARA");
  strcpy(s[5], "AKIHABR");
  strcpy(s[6], "AKIHBAR");
  strcpy(s[7], "KIHABAR");
  strcpy(s[8], "AKIHBRA");
  strcpy(s[9], "KIHABRA");
  strcpy(s[10], "KIHBARA");
  strcpy(s[11], "AKIHBR");
  strcpy(s[12], "KIHABR");
  strcpy(s[13], "KIHBAR");
  strcpy(s[14], "KIHBRA");
  strcpy(s[15], "KIHBR");
}

int main() {
  init();
  int i, ans;
  char S[55];
  while (scanf("%s", S) != EOF) {
    ans = 0;
    for (i = 0; i < 16; i++) {
      if (strcmp(S, s[i]) == 0)
        ans = 1;
    }
    if (ans)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
Problem p03533 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; --(i))
#define nfor(i, a, b) for (ll(i) = (a); (i) < (b); ++(i))
#define ll long long
#define mod (1000000007)
#define YES cout << "YES" << endl;
#define Yes cout << "Yes" << endl;
#define yes cout << "yes" << endl;
#define NO cout << "NO" << endl;
#define No cout << "No" << endl;
#define no cout << "no" << endl;
#define all(x) (x).begin(), (x).end()

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll fac(ll n) {
  ll t = 1;
  rep1(i, n + 1) t *= i;
  return t;
}

int main() {
  string s, t = "AKIHABARA";
  cin >> s;
  rep0(i, 9) {
    if (s[i] != t[i] && t[i] == 'A') {
      if (i == 8) {
        s += 'A';
      } else {
        s = s.substr(0, i) + 'A' + s.substr(i);
      }
    }
  }
  if (s == t) {
    YES;
  } else {
    NO;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; --(i))
#define nfor(i, a, b) for (ll(i) = (a); (i) < (b); ++(i))
#define ll long long
#define mod (1000000007)
#define YES cout << "YES" << endl;
#define Yes cout << "Yes" << endl;
#define yes cout << "yes" << endl;
#define NO cout << "NO" << endl;
#define No cout << "No" << endl;
#define no cout << "no" << endl;
#define all(x) (x).begin(), (x).end()

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll fac(ll n) {
  ll t = 1;
  rep1(i, n + 1) t *= i;
  return t;
}

int main() {
  string s, t = "AKIHABARA";
  cin >> s;
  rep0(i, 9) {
    if (s[i] != t[i]) {
      if (i == 8) {
        s += 'A';
      } else {
        s = s.substr(0, i) + 'A' + s.substr(i);
      }
    }
  }
  if (s == t) {
    YES;
  } else {
    NO;
  }
  return 0;
}
Problem p03534 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

char s[10005];
int main() {
  int len, i;
  int a, b, c, t;
  while (scanf("%s", s) != EOF) {
    a = 0;
    b = 0;
    c = 0;
    len = strlen(s);
    for (i = 0; i < len; i++) {
      if (s[i] == 'a')
        a++;
      else if (s[i] == 'b')
        b++;
      else
        c++;
    }
    if (a < b) {
      t = a;
      a = b;
      b = t;
    }
    if (a < c) {
      t = a;
      a = c;
      c = t;
    }
    if (b < c) {
      t = b;
      b = c;
      c = t;
    }

    if (a - c >= 2)
      printf("NO\n");
    else
      printf("YES\n");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

char s[100005];
int main() {
  int len, i;
  int a, b, c, t;
  while (scanf("%s", s) != EOF) {
    a = 0;
    b = 0;
    c = 0;
    len = strlen(s);
    for (i = 0; i < len; i++) {
      if (s[i] == 'a')
        a++;
      else if (s[i] == 'b')
        b++;
      else
        c++;
    }
    if (a < b) {
      t = a;
      a = b;
      b = t;
    }
    if (a < c) {
      t = a;
      a = c;
      c = t;
    }
    if (b < c) {
      t = b;
      b = c;
      c = t;
    }

    if (a - c >= 2)
      printf("NO\n");
    else
      printf("YES\n");
  }
  return 0;
}
Problem p03534 (C++)

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

int main() {
  string S;
  int C[3] = {};
  cin >> S;
  for (int i = 0; i < S.size(); i++) {
    C[i - 97]++;
  }
  sort(C, C + 3);
  if (C[2] - C[0] <= 1)
    cout << "YES";
  else
    cout << "NO";
}

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

int main() {
  string S;
  int C[3] = {};
  cin >> S;
  for (int i = 0; i < S.size(); i++) {
    C[S[i] - 97]++;
  }
  sort(C, C + 3);
  if (C[2] - C[0] <= 1)
    cout << "YES";
  else
    cout << "NO";
}
Problem p03534 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

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

string S;

int main() {
  cin >> S;
  int n = S.size();
  int c[3] = {};
  rep(i, n) c[S[i] - 'a']++;
  sort(c, c + n);
  if (c[2] - c[0] <= 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

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

string S;

int main() {
  cin >> S;
  int n = S.size();
  int c[3] = {};
  rep(i, n) c[S[i] - 'a']++;
  sort(c, c + 3);
  if (c[2] - c[0] <= 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03534 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
int read() {
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  int k = 1, kk = 0;
  if (c == '-')
    c = getchar(), k = -1;
  while (c >= '0' && c <= '9')
    kk = kk * 10 + c - '0', c = getchar();
  return k * kk;
}
using namespace std;
char s[1000000];
int f[10], l;
int main() {
  scanf("%s", s);
  l = strlen(s);
  for (int i = 0; i < l; i++)
    f[s[i] - 'A']++;
  if (max(f[0], max(f[1], f[2])) <= 1 + min(f[0], min(f[1], f[2])))
    puts("YES");
  else
    puts("NO");
}


Fixed submission:
#include <bits/stdc++.h>
int read() {
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  int k = 1, kk = 0;
  if (c == '-')
    c = getchar(), k = -1;
  while (c >= '0' && c <= '9')
    kk = kk * 10 + c - '0', c = getchar();
  return k * kk;
}
using namespace std;
char s[1000000];
int f[10], l;
int main() {
  scanf("%s", s);
  l = strlen(s);
  for (int i = 0; i < l; i++)
    f[s[i] - 'a']++;
  if (max(f[0], max(f[1], f[2])) <= 1 + min(f[0], min(f[1], f[2])))
    puts("YES");
  else
    puts("NO");
}
Problem p03535 (C++)

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

int d[60];
bool dp[2][1 << 24];
int main() {
  int n;
  scanf("%d", &n);
  int Max = 0;
  rep(i, n) { scanf("%d", &d[i]); }
  dp[1][0] = true;
  rep(i, n) {
    rep(j, 1 << 24) dp[i % 2][j] = false;
    rep(j, 1 << 24) {
      if (dp[!(i % 2)][j] == false)
        continue;
      dp[i % 2][j | 1 << d[i]] = true;
      if (j)
        dp[i % 2][j | 1 << (24 - d[i])] = true;
    }
  }
  rep(i, 1 << 24) {
    if (!dp[!(n % 2)][i])
      continue;
    vector<int> v;
    rep(j, 24) {
      if (i >> j & 1)
        v.push_back(j);
    }
    int sum = INT_MAX;
    rep(i, v.size()) {
      sum = min(sum, d[i]);
      if (n > 1) {
        if (i < v.size() - 1) {
          int A = v[i + 1] - v[i];
          sum = min({sum, A, 24 - A});
        } else {
          int A = v.back() - v.front();
          sum = min({sum, A, 24 - A});
        }
      }
    }
    Max = max(Max, sum);
  }
  cout << Max << endl;
}

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

int d[60];
bool dp[2][1 << 24];
int main() {
  int n;
  scanf("%d", &n);
  int Max = 0;
  rep(i, n) { scanf("%d", &d[i]); }
  dp[1][0] = true;
  rep(i, n) {
    rep(j, 1 << 24) dp[i % 2][j] = false;
    rep(j, 1 << 24) {
      if (dp[!(i % 2)][j] == false)
        continue;
      if (!(j >> d[i] & 1))
        dp[i % 2][j | 1 << d[i]] = true;
      if (d[i] && !(j >> (24 - d[i]) & 1))
        dp[i % 2][j | 1 << (24 - d[i])] = true;
    }
  }
  rep(i, 1 << 24) {
    if (!dp[!(n % 2)][i])
      continue;
    vector<int> v;
    rep(j, 24) {
      if (i >> j & 1)
        v.push_back(j);
    }
    int sum = INT_MAX;
    rep(i, v.size()) {
      sum = min(sum, d[i]);
      if (n > 1) {
        if (i < v.size() - 1) {
          int A = v[i + 1] - v[i];
          sum = min({sum, A, 24 - A});
        } else {
          int A = v.back() - v.front();
          sum = min({sum, A, 24 - A});
        }
      }
    }
    Max = max(Max, sum);
  }
  cout << Max << endl;
}
Problem p03535 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "climits"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "set"
#include "stack"
#include "string"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  vector<int> ti(13);
  for (int i = 0; i < N; i++) {
    ti[v[i]]++;
    if (ti[v[i]] == 3) {
      cout << 0 << endl;
      return 0;
    }
  }
  // if (N > 12) {
  //	cout << 1 << endl;
  //	return 0;
  // }
  int ans = 0;
  for (int i = 0; i < pow(2, N); i++) {
    vector<int> box;
    box.push_back(0);
    for (int j = 0; j < N; j++) {
      if (i >> j & 1) {
        box.push_back(v[j]);
      } else {
        box.push_back(-v[j]);
      }
    }
    sort(box.begin(), box.end());
    int bag = 100;
    for (int i = 0; i < N; i++) {
      bag = min(bag, box[i + 1] - box[i]);
    }
    bag = min(bag, box[0] + 24 - box[N]);
    ans = max(ans, bag);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "algorithm"
#include "climits"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "set"
#include "stack"
#include "string"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  vector<int> ti(13);
  for (int i = 0; i < N; i++) {
    ti[v[i]]++;
    if (ti[v[i]] == 3) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (ti[0]) {
    cout << 0 << endl;
    return 0;
  }
  if (ti[12] == 2) {
    cout << 0 << endl;
    return 0;
  }
  if (N > 13) {
    cout << 1 << endl;
    return 0;
  }
  int ans = 0;
  for (int i = 0; i < pow(2, N); i++) {
    vector<int> box;
    box.push_back(0);
    for (int j = 0; j < N; j++) {
      if (i >> j & 1) {
        box.push_back(v[j]);
      } else {
        box.push_back(-v[j]);
      }
    }
    sort(box.begin(), box.end());
    int bag = 100;
    for (int i = 0; i < N; i++) {
      bag = min(bag, box[i + 1] - box[i]);
    }
    bag = min(bag, box[0] + 24 - box[N]);
    ans = max(ans, bag);
  }
  cout << ans << endl;
  return 0;
}
Problem p03535 (C++)

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EPS (1e-7)
#define INF 1e18

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

int vx[] = {0, 1, 0, -1};
int vy[] = {1, 0, -1, 0};

int dx[] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy[] = {1, 1, 1, 0, 0, -1, -1, -1};

const ll mod = 1e9 + 7;
const int MAX_N = 300;

int main() {
  int n;
  int ans = 0;
  cin >> n;
  int d[n + 1];
  int min_id = 0;
  d[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> d[i];
  }
  if (n >= 24)
    ans = 0;
  else {
    for (int i = 0; i < (1 << n); i++) {
      int min_time = 12;
      int time[n + 1];
      time[0] = 0;
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1) == 1)
          time[j + 1] = 24 - d[j + 1];
        else
          time[j + 1] = d[j + 1];
      }
      for (int j = 0; j < n + 1; j++) {
        for (int k = j + 1; k < n + 1; k++) {
          min_time = min(min_time, min(24 - abs(time[j] - time[k]),
                                       abs(time[j] - time[k])));
        }
      }
      ans = max(ans, min_time);
    }
  }
  cout << ans << endl;
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EPS (1e-7)
#define INF 1e18

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

int vx[] = {0, 1, 0, -1};
int vy[] = {1, 0, -1, 0};

int dx[] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy[] = {1, 1, 1, 0, 0, -1, -1, -1};

const ll mod = 1e9 + 7;
const int MAX_N = 300;

int main() {
  int n;
  int ans = 0;
  cin >> n;
  int d[n + 1];
  int min_id = 0;
  d[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> d[i];
  }
  if (n >= 24)
    ans = 0;
  else if (n == 23) {
    int hours[13];
    fill(hours, hours + 13, 0);
    for (int i = 0; i < n + 1; i++) {
      hours[d[i]]++;
    }
    bool judge = true;
    for (int i = 0; i < 13; i++) {
      if (i == 0 || i == 12) {
        if (hours[i] > 1)
          judge = false;
      } else {
        if (hours[i] > 2)
          judge = false;
      }
    }
    if (judge)
      ans = 1;
  } else {
    for (int i = 0; i < (1 << n); i++) {
      int min_time = 12;
      int time[n + 1];
      time[0] = 0;
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1) == 1)
          time[j + 1] = 24 - d[j + 1];
        else
          time[j + 1] = d[j + 1];
      }
      for (int j = 0; j < n + 1; j++) {
        for (int k = j + 1; k < n + 1; k++) {
          min_time = min(min_time, min(24 - abs(time[j] - time[k]),
                                       abs(time[j] - time[k])));
        }
      }
      ans = max(ans, min_time);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03535 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int m;
vector<int> d(0);

void dfs(vector<int> dd, int id, int n) {
  if (id == n) {
    int tempmin = 24;
    vector<int> cp(dd.size());
    cp = dd;
    for (int i = 0; i < dd.size(); i++) {
      for (int j = 0; j < i; j++) {
        tempmin =
            min(tempmin, min(24 - abs(dd[i] - dd[j]), abs(dd[i] - dd[j])));
      }
    }
    m = max(m, tempmin);
  } else {
    dd.push_back(d[id]);
    dfs(dd, id + 1, n);
    dd.pop_back();
    dd.push_back(-d[id]);
    dfs(dd, id + 1, n);
    dd.pop_back();
  }
}

int main(void) {
  int n, a;

  cin >> n;

  vector<int> dd(1);
  dd[0] = 0;

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

  m = 0;

  if (n >= 24) {
    ;
  }
  /*	else if (n == 23){
                  sort(d.begin(), d.end());
                  int ok = 1;
                  for (int i = 0; i < n; i++){
                          if (d[i] != i/2 + 1)
                                  ok = 0;
                  }
                  ok = m;
          }
  */
  else {
    dfs(dd, 0, n);
  }

  cout << m << endl;
}

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

using namespace std;

int m;
vector<int> d(0);

void dfs(vector<int> dd, int id, int n) {
  if (id == n) {
    int tempmin = 24;
    vector<int> cp(dd.size());
    cp = dd;
    for (int i = 0; i < dd.size(); i++) {
      for (int j = 0; j < i; j++) {
        tempmin =
            min(tempmin, min(24 - abs(dd[i] - dd[j]), abs(dd[i] - dd[j])));
      }
    }
    m = max(m, tempmin);
  } else {
    dd.push_back(d[id]);
    dfs(dd, id + 1, n);
    dd.pop_back();
    dd.push_back(-d[id]);
    dfs(dd, id + 1, n);
    dd.pop_back();
  }
}

int main(void) {
  int n, a;

  cin >> n;

  vector<int> dd(1);
  dd[0] = 0;

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

  m = 0;

  if (n >= 24) {
    ;
  } else if (n == 23) {
    sort(d.begin(), d.end());
    int ok = 1;
    for (int i = 0; i < n; i++) {
      if (d[i] != i / 2 + 1)
        ok = 0;
    }
    m = ok;
  } else {
    dfs(dd, 0, n);
  }

  cout << m << endl;
}
Problem p03536 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

P p[6000];
ll dp[6000][6000];

int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) { scanf("%d%d", &p[i].first, &p[i].second); }
  sort(p, p + n,
       [](P a, P b) { return a.first + a.second < b.first + b.second; });
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  rep(i, n) rep(j, n + 1) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    if (dp[i][j] <= p[i].first)
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[i].second);
  }
  int ans = 0;
  rep(i, n + 1) {
    if (dp[n][i] != INF)
      ans = i;
  }
  cout << ans << endl;
}

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

P p[6000];
ll dp[5001][5001];

int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) { scanf("%d%d", &p[i].first, &p[i].second); }
  sort(p, p + n,
       [](P a, P b) { return a.first + a.second < b.first + b.second; });
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  rep(i, n) rep(j, n + 1) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    if (dp[i][j] <= p[i].first)
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[i].second);
  }
  int ans = 0;
  rep(i, n + 1) {
    if (dp[n][i] != INF)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03536 (C++)

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

typedef long long ll;

const int N = 5e3 + 7;
const int INF = 1e18 + 7;
pair<ll, ll> p[N];
ll dp[N][N];

#define F first
#define S second

bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.S < b.F)
    return true;
  if (b.S < a.F)
    return false;
  return a.F < b.F;
}

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].F >> p[i].S;
  sort(p, p + n, cmp);
  memset(dp, 63, sizeof dp);
  dp[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (dp[i][j] <= p[i].F)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[i].S);
    }
  for (int i = n; i >= 0; i--)
    if (dp[n][i] <= INF) {
      cout << i << "\n";
      return 0;
    }

  return 0;
}


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

typedef long long ll;

const int N = 5e3 + 7;
const int INF = 1e18 + 7;
pair<ll, ll> p[N];
ll dp[N][N];

#define F first
#define S second

bool cmp(pair<int, int> a, pair<int, int> b) { return a.F + a.S < b.F + b.S; }

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].F >> p[i].S;
  sort(p, p + n, cmp);
  memset(dp, 63, sizeof dp);
  dp[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (dp[i][j] <= p[i].F)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[i].S);
    }
  for (int i = n; i >= 0; i--)
    if (dp[n][i] <= INF) {
      cout << i << "\n";
      return 0;
    }

  return 0;
}