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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define FOR(i, l, r) for (int i = l; i <= r; ++i)

using namespace std;

const int N = 200010;
const int mod = 998244353;

char s[N];
int n, fl, ans, tmp;
int vis[N], f[N][3], h[N][3][3];

void brute_force() {
  int tmp = 0;
  FOR(i, 1, n) tmp = tmp * 10 + s[i] - 'a' + 1;
  if (vis[tmp])
    return;
  else
    ++ans;

  FOR(i, 1, n - 1) if (s[i] != s[i + 1]) {
    char t1 = s[i], t2 = s[i + 1], t3 = 'a' + 'b' + 'c' - t1 - t2;
    s[i] = s[i + 1] = t3;
    brute_force();
    s[i] = t1, s[i + 1] = t2;
  }
}

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);

  FOR(i, 2, n) if (s[i] != s[1]) fl = 1;
  if (!fl) {
    puts("1");
    return 0;
  }

  if (n <= 3) {
    brute_force();
    printf("%d\n", ans);
    return 0;
  }

  FOR(i, 2, n) if (s[i] == s[i - 1]) fl = 0;
  if (fl)
    ans = 1;

  FOR(i, 1, n) tmp += s[i] - 'a';
  tmp %= 3;

  f[0][0] = 1;
  FOR(i, 0, n - 1)
  FOR(j, 0, 2) FOR(k, 0, 2)(f[i + 1][(j + k) % 3] += f[i][j]) %= mod;
  (ans += f[n][tmp]) %= mod;

  FOR(i, 0, 2) h[1][i][i] = 1;
  FOR(i, 1, n - 1)
  FOR(j, 0, 2) FOR(k, 0, 2)
      FOR(l, 0, 2) if (l != k)(h[i + 1][(j + l) % 3][l] += h[i][j][k]) %= mod;
  FOR(i, 0, 2)(ans += mod - h[n][tmp][i]) %= mod;

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

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#define FOR(i, l, r) for (int i = l; i <= r; ++i)

using namespace std;

const int N = 200010;
const int mod = 998244353;

char s[N];
int n, fl, ans, tmp;
int vis[N], f[N][3], h[N][3][3];

void brute_force() {
  int tmp = 0;
  FOR(i, 1, n) tmp = tmp * 10 + s[i] - 'a' + 1;
  if (vis[tmp])
    return;
  else
    ++ans, vis[tmp] = 1;

  FOR(i, 1, n - 1) if (s[i] != s[i + 1]) {
    char t1 = s[i], t2 = s[i + 1], t3 = 'a' + 'b' + 'c' - t1 - t2;
    s[i] = s[i + 1] = t3;
    brute_force();
    s[i] = t1, s[i + 1] = t2;
  }
}

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);

  FOR(i, 2, n) if (s[i] != s[1]) fl = 1;
  if (!fl) {
    puts("1");
    return 0;
  }

  if (n <= 3) {
    brute_force();
    printf("%d\n", ans);
    return 0;
  }

  FOR(i, 2, n) if (s[i] == s[i - 1]) fl = 0;
  if (fl)
    ans = 1;

  FOR(i, 1, n) tmp += s[i] - 'a';
  tmp %= 3;

  f[0][0] = 1;
  FOR(i, 0, n - 1)
  FOR(j, 0, 2) FOR(k, 0, 2)(f[i + 1][(j + k) % 3] += f[i][j]) %= mod;
  (ans += f[n][tmp]) %= mod;

  FOR(i, 0, 2) h[1][i][i] = 1;
  FOR(i, 1, n - 1)
  FOR(j, 0, 2) FOR(k, 0, 2)
      FOR(l, 0, 2) if (l != k)(h[i + 1][(j + l) % 3][l] += h[i][j][k]) %= mod;
  FOR(i, 0, 2)(ans += mod - h[n][tmp][i]) %= mod;

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

  return 0;
}
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, A) for (int i = 0; i < A; i++)

using namespace std;
using ll = long long int;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;

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

  string s;
  cin >> s;

  bool used[26];
  REP(i, 26) { used[i] = false; }
  REP(i, s.size()) { used[s[i] - 'a'] = true; }
  REP(i, 26) {
    char ness;
    if (!used[i]) {
      ness = 'a' + i;
      cout << s << ness;
      return 0;
    }
  }
  int flag = 0;
  for (int i = s.size() - 1; i > 0; i--) {
    if (s[i] > s[i - 1]) {
      flag = i;
      break;
    }
  }
  if (flag) {
    char nass;
    for (int i = s.size() - 1; i >= flag; i++) {
      if (s[i] > s[flag - 1]) {
        nass = s[i];
        break;
      }
    }
    cout << s.substr(0, flag - 1) << nass;
    return 0;
  } else {
    cout << -1;
    return 0;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, A) for (int i = 0; i < A; i++)

using namespace std;
using ll = long long int;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;

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

  string s;
  cin >> s;

  bool used[26];
  REP(i, 26) { used[i] = false; }
  REP(i, s.size()) { used[s[i] - 'a'] = true; }
  REP(i, 26) {
    char ness;
    if (!used[i]) {
      ness = 'a' + i;
      cout << s << ness;
      return 0;
    }
  }
  int flag = 0;
  for (int i = s.size() - 1; i > 0; i--) {
    if (s[i] > s[i - 1]) {
      flag = i;
      break;
    }
  }
  if (flag) {
    char nass;
    for (int i = s.size() - 1; i >= flag; i--) {
      if (s[i] > s[flag - 1]) {
        nass = s[i];
        break;
      }
    }
    cout << s.substr(0, flag - 1) << nass;
    return 0;
  } else {
    cout << -1;
    return 0;
  }
}
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.size();
  unordered_set<char> st;
  for (int i = 0; i < N; ++i)
    st.insert(S[i]);
  if (N < 26) {
    for (char i = 'a'; i != 'z'; i = char(i + 1)) {
      if (st.count(i) == 0) {
        cout << S + i << endl;
        return 0;
      }
    }
  }
  for (int i = 24; i >= 0; --i) {
    if (S[i] < S[i + 1]) {
      int j = 25;
      while (S[i] > S[j])
        ++j;
      swap(S[i], S[j]);
      cout << S.substr(0, i + 1) << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.size();
  unordered_set<char> st;
  for (int i = 0; i < N; ++i)
    st.insert(S[i]);
  if (N < 26) {
    for (char i = 'a'; i != 'z'; i = char(i + 1)) {
      if (st.count(i) == 0) {
        cout << S + i << endl;
        return 0;
      }
    }
  }
  for (int i = 24; i >= 0; --i) {
    if (S[i] < S[i + 1]) {
      int j = 25;
      while (S[i] > S[j])
        --j;
      swap(S[i], S[j]);
      cout << S.substr(0, i + 1) << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p03393 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  vector<bool> hoge(26);
  string ans;
  if (s.length() != 26) {
    for (int i = 0; i < s.length(); i++) {
      hoge[(char)s[i] - 'a'] = true;
    }
    int min;
    for (int i = 0; i < 26; i++) {
      if (hoge[i] == false) {
        min = i;
        break;
      }
    }
    ans = s;
    ans.push_back((char)'a' + min);
  } else {
    string hogefuga = "zyxwvutsrqponmlkjihgfedcba";
    if (s == hogefuga) {
      cout << "-1" << endl;
      return 0;
    }
    int min = s.back();
    int popcount;
    for (int i = 0; i < 26; i++) {
      if (s[24 - i] < s[25 - i]) {
        popcount = i + 1;
        break;
      }
    }
    int i = 0;
    while (true) {
      if (s[25 - popcount] < s[25 - i]) {
        char unti = s[25 - i];
        for (int j = 0; j < popcount; j++)
          s.pop_back();
        s.back() = unti;
        break;
      }
    }
    ans = s;
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  vector<bool> hoge(26);
  string ans;
  if (s.length() != 26) {
    for (int i = 0; i < s.length(); i++) {
      hoge[(char)s[i] - 'a'] = true;
    }
    int min;
    for (int i = 0; i < 26; i++) {
      if (hoge[i] == false) {
        min = i;
        break;
      }
    }
    ans = s;
    ans.push_back((char)'a' + min);
  } else {
    string hogefuga = "zyxwvutsrqponmlkjihgfedcba";
    if (s == hogefuga) {
      cout << "-1" << endl;
      return 0;
    }
    int min = s.back();
    int popcount;
    for (int i = 0; i < 26; i++) {
      if (s[24 - i] < s[25 - i]) {
        popcount = i + 1;
        break;
      }
    }
    int i = 0;
    while (true) {
      if (s[25 - popcount] < s[25 - i]) {
        char unti = s[25 - i];
        for (int j = 0; j < popcount; j++)
          s.pop_back();
        s.back() = unti;
        break;
      }
      i++;
    }
    ans = s;
  }
  cout << ans << endl;
}
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (i = 0; i < n; i++)
#define REPR(i, n) for (i = n; i >= 0; i--)
#define FOR(i, m, n) for (i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;

int main(void) {
  string s;
  bool ap[26] = {false};
  cin >> s;
  int i, j;
  int slen = (int)s.size();
  char p, q;
  for (i = 0; i < slen; i++) {
    ap[s[i] - 'a'] = true;
  }

  if (slen < 26) {
    for (p = 0; p < 26; p++) {
      if (ap[p] == false) {
        cout << s << (char)(p + 'a');
        return 0;
      }
    }
  }

  ap[s[slen - 1] - 'a'] = false;
  s[slen - 1] = '\0';

  for (p = slen - 2; p >= 0; p--) {
    for (q = s[p] - 'a' + 1; q < 26; q++) {
      if (ap[q] == false) {
        s[p] = q + 'a';
        for (j = 0; s[j] != '\n'; j++)
          cout << s[j];
        return 0;
      }
    }
    ap[s[p] - 'a'] = false;
    s[p] = '\0';
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (i = 0; i < n; i++)
#define REPR(i, n) for (i = n; i >= 0; i--)
#define FOR(i, m, n) for (i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;

int main(void) {
  string s;
  bool ap[26] = {false};
  cin >> s;
  int i, j;
  int slen = (int)s.size();
  char p, q;
  for (i = 0; i < slen; i++) {
    ap[s[i] - 'a'] = true;
  }

  if (slen < 26) {
    for (p = 0; p < 26; p++) {
      if (ap[p] == false) {
        cout << s << (char)(p + 'a');
        return 0;
      }
    }
  }

  ap[s[slen - 1] - 'a'] = false;
  s[slen - 1] = '\0';

  for (p = slen - 2; p >= 0; p--) {
    for (q = s[p] - 'a' + 1; q < 26; q++) {
      if (ap[q] == false) {
        s[p] = q + 'a';
        for (j = 0; s[j] != '\0'; j++)
          cout << s[j];
        return 0;
      }
    }
    ap[s[p] - 'a'] = false;
    s[p] = '\0';
  }
  cout << -1 << endl;
  return 0;
}
Problem p03393 (C++)

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

int main() {
  string s;
  cin >> s;
  vector<char> vec;
  for (int i = 0; i < 26; i++) {
    vec.push_back('a' + i);
  }
  bool MAX = false;
  if (s.size() == 26) {
    MAX = true;
    int i = 25;
    while (s[i] < s[i - 1]) {
      i--;
      if (s[i] == 'z') {
        break;
      }
    }
    s = s.substr(0, i);
  }
  for (int i = 0; i < s.size(); i++) {
    int left = 0, right = vec.size(), mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (s[i] == vec[mid]) {
        vec.erase(vec.begin() + mid);
      } else if (s[i] < vec[mid]) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }
  if (s.size() == 0) {
    cout << -1 << endl;
  } else if (MAX) {
    while (vec[0] <= s[s.size() - 1]) {
      vec.erase(vec.begin());
    }
    s[s.size() - 1] = vec[0];
    cout << s << endl;
  } else {
    cout << s << vec[0] << endl;
  }
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  vector<char> vec;
  for (int i = 0; i < 26; i++) {
    vec.push_back('a' + i);
  }
  bool MAX = false;
  if (s.size() == 26) {
    MAX = true;
    int i = 25;
    while (s[i] < s[i - 1]) {
      i--;
      if (s[i] == 'z') {
        break;
      }
    }
    s = s.substr(0, i);
  }
  for (int i = 0; i < s.size(); i++) {
    int left = 0, right = vec.size(), mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (s[i] == vec[mid]) {
        vec.erase(vec.begin() + mid);
        break;
      } else if (s[i] < vec[mid]) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }
  if (s.size() == 0) {
    cout << -1 << endl;
  } else if (MAX) {
    while (vec[0] <= s[s.size() - 1]) {
      vec.erase(vec.begin());
    }
    s[s.size() - 1] = vec[0];
    cout << s << endl;
  } else {
    cout << s << vec[0] << endl;
  }
  return 0;
}
Problem p03393 (C++)

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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  string S;
  cin >> S;
  if (S.size() < 26) {
    bool alf[26] = {};
    for (auto c : S)
      alf[c - 'a'] = true;
    for (int i = 0; i < 26; i++) {
      if (!alf[i]) {
        cout << S << (char)(i + 'a') << endl;
        return 0;
      }
    }
  } else {
    int idx = S.size() - 1;
    while (idx > 0 && S[idx - 1] > S[idx])
      idx--;
    if (idx == 0)
      cout << -1 << endl;
    else {
      for (int i = 0; i < idx - 1; i++)
        cout << S[i];
      for (int i = S.size() - 1; i >= 0; i++) {
        if (S[i] > S[idx - 1]) {
          cout << S[i] << endl;
          break;
        }
      }
    }
  }

  return 0;
}


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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  string S;
  cin >> S;
  if (S.size() < 26) {
    bool alf[26] = {};
    for (auto c : S)
      alf[c - 'a'] = true;
    for (int i = 0; i < 26; i++) {
      if (!alf[i]) {
        cout << S << (char)(i + 'a') << endl;
        return 0;
      }
    }
  } else {
    int idx = S.size() - 1;
    while (idx > 0 && S[idx - 1] > S[idx])
      idx--;
    if (idx == 0)
      cout << -1 << endl;
    else {
      for (int i = 0; i < idx - 1; i++)
        cout << S[i];
      for (int i = S.size() - 1; i >= 0; i--) {
        if (S[i] > S[idx - 1]) {
          cout << S[i] << endl;
          break;
        }
      }
    }
  }

  return 0;
}
Problem p03393 (Python)

Buggy submission (status: Runtime Error):
S = input()
if len(S) < 26:
    for i in range(26):
        c = chr(i + ord("a"))
        if c not in S:
            print(S + c)
            break
else:
    tmp = 26
    for i in range(len(S) - 1, 0, -1):
        if ord(S[i - 1]) < ord(S[i]):
            break
        tmp = i
    tmp -= 1
    if tmp == 0:
        print(-1)
    c = ord(S[:tmp][-1])
    suf = S[tmp:]
    print(S[: tmp - 1] + chr(min([ord(si) for si in suf if ord(si) > c])))


Fixed submission:
S = input()
if len(S) < 26:
    for i in range(26):
        c = chr(i + ord("a"))
        if c not in S:
            print(S + c)
            break
else:
    tmp = 26
    for i in range(len(S) - 1, 0, -1):
        if ord(S[i - 1]) < ord(S[i]):
            break
        tmp = i
    tmp -= 1
    if tmp == 0:
        print(-1)
    else:
        c = ord(S[:tmp][-1])
        suf = S[tmp:]
        print(S[: tmp - 1] + chr(min([ord(si) for si in suf if ord(si) > c])))
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
using namespace std;

string s;
int nyak[130];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> s;
  int p = s.size();
  if (s == "zyxwvutsrqponmlkjihgfedcba") {
    printf("-1\n");
    return 0;
  }
  for (int x = 0; x < p; x++)
    nyak[s[x] - 'a']++;

  if (p == 26)
    assert(false);
  for (int y = p - 1; y >= 0; y--) {

    int now;
    if (y == p - 1)
      now = 0;
    else
      now = s[y + 1] - 'a' + 1;
    for (int z = now; z < 26; z++) {
      if (nyak[z] == 0) {
        for (int x = 0; x <= y; x++) {
          printf("%c", s[x]);
        }
        printf("%c", z + 'a');
        printf("\n");
        return 0;
      }
    }
    nyak[s[y] - 'a']--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
using namespace std;

string s;
int nyak[130];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> s;
  int p = s.size();
  if (s == "zyxwvutsrqponmlkjihgfedcba") {
    printf("-1\n");
    return 0;
  }
  for (int x = 0; x < p; x++)
    nyak[s[x] - 'a']++;

  for (int y = p - 1; y >= -1; y--) {

    int now;
    if (y == p - 1)
      now = 0;
    else
      now = s[y + 1] - 'a' + 1;
    for (int z = now; z < 26; z++) {
      if (nyak[z] == 0) {
        for (int x = 0; x <= y; x++) {
          printf("%c", s[x]);
        }
        printf("%c", z + 'a');
        printf("\n");
        return 0;
      }
    }
    nyak[s[y] - 'a']--;
  }
}
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char s[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
              'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
set<char> t;
int main() {
  string a;
  while (cin >> a) {
    if (a == "zyxwvutsrqponmlkjihgfedcba")
      printf("-1\n");
    else {
      int len = (int)a.size();
      if (len < 26) {
        cout << a;
        t.clear();
        for (int i = 0; i < len; i++)
          t.insert(a[i]);
        for (int i = 0; i < 26; i++) {
          if (!t.count(s[i])) {
            printf("%c\n", s[i]);
            break;
          }
        }
      } else {
        if (a[len - 1] == 'z') {
          for (int i = 0; i < len - 2; i++)
            printf("%c", a[i]);
          printf("z\n");
        } else {
          int i;
          char k = 'z';
          char b[10];
          int j = 0;
          for (i = len - 1; i > 0; i--) {
            b[j++] = a[i];
            if (a[i] > a[i - 1])
              break;
          }
          int h = i;
          char p = a[i - 1];
          for (int i = 0; i < j; i++)
            if (b[i] > p) {
              if (b[i] < k)
                k = b[i];
            }
          a[h - 1] = k;
          for (int j = 0; j <= h - 1; j++)
            printf("%c", a[j]);
          printf("\n");
        }
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char s[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
              'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
set<char> t;
int main() {
  string a;
  while (cin >> a) {
    if (a == "zyxwvutsrqponmlkjihgfedcba")
      printf("-1\n");
    else {
      int len = (int)a.size();
      if (len < 26) {
        cout << a;
        t.clear();
        for (int i = 0; i < len; i++)
          t.insert(a[i]);
        for (int i = 0; i < 26; i++) {
          if (!t.count(s[i])) {
            printf("%c\n", s[i]);
            break;
          }
        }
      } else {
        if (a[len - 1] == 'z') {
          for (int i = 0; i < len - 2; i++)
            printf("%c", a[i]);
          printf("z\n");
        } else {
          int i;
          char k = 'z';
          char b[30];
          int j = 0;
          for (i = len - 1; i > 0; i--) {
            b[j++] = a[i];
            if (a[i] > a[i - 1])
              break;
          }
          int h = i;
          char p = a[i - 1];
          for (int i = 0; i < j; i++)
            if (b[i] > p) {
              if (b[i] < k)
                k = b[i];
            }
          a[h - 1] = k;
          for (int j = 0; j <= h - 1; j++)
            printf("%c", a[j]);
          printf("\n");
        }
      }
    }
  }
  return 0;
}
Problem p03393 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  string s = "";
  cin >> s;
  vector<bool> check(26, false);
  for (char c : s) {
    check.at(c - 'a') = true;
  }
  for (int i = 0; i < 26; i++) {
    if (!check.at(i)) {
      cout << s + (char)('a' + i) << endl;
      return 0;
    }
  }
  string ret = "";
  for (int i = 24; i >= 0; i--) {
    if (s.at(i) > s.at(i + 1)) {
      continue;
    } else {
      for (int j = 0; j < i; j++) {
        ret += s.at(j);
      }
      int now = 25;
      while (true) {
        if (s.at(now) > s.at(i)) {
          ret += s.at(now);
          cout << ret << endl;
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  string s = "";
  cin >> s;
  vector<bool> check(26, false);
  for (char c : s) {
    check.at(c - 'a') = true;
  }
  for (int i = 0; i < 26; i++) {
    if (!check.at(i)) {
      cout << s + (char)('a' + i) << endl;
      return 0;
    }
  }
  string ret = "";
  for (int i = 24; i >= 0; i--) {
    if (s.at(i) > s.at(i + 1)) {
      continue;
    } else {
      for (int j = 0; j < i; j++) {
        ret += s.at(j);
      }
      int now = 25;
      while (true) {
        if (s.at(now) > s.at(i)) {
          ret += s.at(now);
          cout << ret << endl;
          return 0;
        }
        now--;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
string a;
int v[30];
int main() {
  int i, j;
  cin >> a;
  for (i = 0; a[i]; i++)
    v[a[i] - 'a'] = 1;
  if (a.size() < 26) {
    for (i = 0; v[i]; i++)
      ;
    cout << a << (char)(i + 'a');
    return 0;
  }
  for (i = 24; i >= 0 && a[i] > a[i + 1]; i--)
    ;
  if (i == -1) {
    cout << -1;
    return 0;
  }
  cout << a.substr(0, i);
  for (j = 25; a[j] < a[i]; j++)
    ;
  cout << a[j];
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
string a;
int v[30];
int main() {
  int i, j;
  cin >> a;
  for (i = 0; a[i]; i++)
    v[a[i] - 'a'] = 1;
  if (a.size() < 26) {
    for (i = 0; v[i]; i++)
      ;
    cout << a << (char)(i + 'a');
    return 0;
  }
  for (i = 24; i >= 0 && a[i] > a[i + 1]; i--)
    ;
  if (i == -1) {
    cout << -1;
    return 0;
  }
  cout << a.substr(0, i);
  for (j = 25; a[j] < a[i]; j--)
    ;
  cout << a[j];
  return 0;
}
Problem p03393 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i) // a ≦ i < b
#define Rrep(i, a, b)                                                          \
  for (int64_t i = (a); i >= (b); --i) // reverse repeat. a から b まで減少．
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend() // 逆イテレータ
#define RANGE(a, b, c)                                                         \
  (a).begin() + b, (a).begin() + c // コンテナ a の 要素 b から c へのイテレータ
#define MOD 1000000007
#define INF 1000000000
typedef pair<int64_t, int64_t> PII;
typedef vector<int64_t> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// まずは全探索，次にDP
// 偶奇，逆から，ソート，出現回数，出現位置，DP
// 余事象，包除
// データ構造
// 問題の特徴量
// 存在判定：構成方法，入力の特徴
// gcd, lcm ,素因数分解．
// 例外を十分に含む一般化．想像力の限界
// 小さい系から例示
// 始めは過剰に例示・場合分けしてもいい．各場合を確実に対処．
// 小数のときは，精度の設定する．
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  vector<bool> cnt(26, false);

  if (S.size() == 26) {
    if (S == "zyxwvutsrqponmlkjihgfedcba") {
      cout << "-1\n";
      return -1;
    }
    string s = S;
    next_permutation(ALL(S));
    rep(i, 0, 26) {
      cout << S[i];
      if (s[i] != S[i])
        break;
    }
    cout << "\n";
  } else {
    for (char c : S)
      cnt[c - 'a'] = true;
    rep(i, 0, 26) {
      if (cnt[i] == false) {
        S += (char)(i + 'a');
        break;
      }
    }
    cout << S << "\n";
  }
}

// 境界，出力文字列 チェック


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i) // a ≦ i < b
#define Rrep(i, a, b)                                                          \
  for (int64_t i = (a); i >= (b); --i) // reverse repeat. a から b まで減少．
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend() // 逆イテレータ
#define RANGE(a, b, c)                                                         \
  (a).begin() + b, (a).begin() + c // コンテナ a の 要素 b から c へのイテレータ
#define MOD 1000000007
#define INF 1000000000
typedef pair<int64_t, int64_t> PII;
typedef vector<int64_t> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// まずは全探索，次にDP
// 偶奇，逆から，ソート，出現回数，出現位置，DP
// 余事象，包除
// データ構造
// 問題の特徴量
// 存在判定：構成方法，入力の特徴
// gcd, lcm ,素因数分解．
// 例外を十分に含む一般化．想像力の限界
// 小さい系から例示
// 始めは過剰に例示・場合分けしてもいい．各場合を確実に対処．
// 小数のときは，精度の設定する．
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  vector<bool> cnt(26, false);

  if (S.size() == 26) {
    if (S == "zyxwvutsrqponmlkjihgfedcba") {
      cout << "-1\n";
      return 0;
    }
    string s = S;
    next_permutation(ALL(S));
    rep(i, 0, 26) {
      cout << S[i];
      if (s[i] != S[i])
        break;
    }
    cout << "\n";
  } else {
    for (char c : S)
      cnt[c - 'a'] = true;
    rep(i, 0, 26) {
      if (cnt[i] == false) {
        S += (char)(i + 'a');
        break;
      }
    }
    cout << S << "\n";
  }
}

// 境界，出力文字列 チェック
Problem p03393 (C++)

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

int main() {
  string S;
  cin >> S;
  int N = S.length();
  bool used[26] = {};
  for (int i = 0; i < N; ++i)
    used[S[i] - 'a'] = true;
  if (S.length() == 26) {
    for (int i = N - 1; i > 0; --i) {
      used[S[i] - 'a'] = false;
      if (S[i - 1] < S[i]) {
        S.erase(begin(S) + i, end(S));
        char c = S.back();
        S.pop_back();
        for (int j = c - 'a' + 1; j < 26; ++i) {
          if (!used[j]) {
            S += 'a' + j;
            break;
          }
        }
        cout << S << endl;
        return 0;
      }
    }
    cout << -1 << endl;
  } else {
    for (int i = 0; i < 26; ++i) {
      if (!used[i]) {
        S += 'a' + i;
        break;
      }
    }
    cout << S << endl;
  }
  return 0;
}


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

int main() {
  string S;
  cin >> S;
  int N = S.length();
  bool used[26] = {};
  for (int i = 0; i < N; ++i)
    used[S[i] - 'a'] = true;
  if (S.length() == 26) {
    for (int i = N - 1; i > 0; --i) {
      used[S[i] - 'a'] = false;
      if (S[i - 1] < S[i]) {
        S.erase(begin(S) + i, end(S));
        char c = S.back();
        S.pop_back();
        for (int j = c - 'a' + 1; j < 26; ++j) {
          if (!used[j]) {
            S += 'a' + j;
            break;
          }
        }
        cout << S << endl;
        return 0;
      }
    }
    cout << -1 << endl;
  } else {
    for (int i = 0; i < 26; ++i) {
      if (!used[i]) {
        S += 'a' + i;
        break;
      }
    }
    cout << S << endl;
  }
  return 0;
}
Problem p03394 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD = 1000000007;
const ll INF = LLONG_MAX;
const ll MAX_N = 100000;

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

ll NUM = 2 * 3 * 5 * 7 * 11, prime[] = {2, 3, 5, 7, 11};

int main() {
  ll n, sum = 0, ans[20000];
  cin >> n;

  ll i = 2;
  for (ll cnt = 0; cnt < n - 2; i++) {
    bool flag = false;
    for (ll j = 0; j < 5; j++) {
      if (i % prime[j] == 0)
        flag = true;
    }
    if (flag) {
      sum += i;
      ans[cnt] = i;
      cnt++;
    }
  }
  ll all = (sum / NUM + 1) * NUM;
  while (all - sum < i * 2)
    all += NUM;
  while (1) {
    bool flag[2]{};
    for (ll j = 0; j < 5; j++) {
      if (i % prime[j] == 0)
        flag[0] = true;
    }
    for (ll j = 0; j < 5; j++) {
      if ((all - sum - i) % prime[j] == 0)
        flag[1] = true;
    }
    if (flag[0] && flag[1]) {
      ans[n - 2] = i;
      ans[n - 1] = all - sum - i;
      break;
    }
  }
  for (int j = 0; j < n; j++) {
    printf("%lld\n", ans[j]);
  }
  cout << flush;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD = 1000000007;
const ll INF = LLONG_MAX;
const ll MAX_N = 100000;

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

ll NUM = 2 * 3 * 5 * 7 * 11, prime[] = {2, 3, 5, 7, 11};

int main() {
  ll n, sum = 0, ans[20000];
  cin >> n;

  ll i = 2;
  for (ll cnt = 0; cnt < n - 2; i++) {
    bool flag = false;
    for (ll j = 0; j < 5; j++) {
      if (i % prime[j] == 0)
        flag = true;
    }
    if (flag) {
      sum += i;
      ans[cnt] = i;
      cnt++;
    }
  }
  ll all = (sum / NUM + 1) * NUM;
  while (all - sum < i * 2)
    all += NUM;
  while (1) {
    bool flag[2]{};
    for (ll j = 0; j < 5; j++) {
      if (i % prime[j] == 0)
        flag[0] = true;
    }
    for (ll j = 0; j < 5; j++) {
      if ((all - sum - i) % prime[j] == 0)
        flag[1] = true;
    }
    if (flag[0] && flag[1]) {
      ans[n - 2] = i;
      ans[n - 1] = all - sum - i;
      break;
    }
    i++;
  }
  for (int j = 0; j < n; j++) {
    printf("%lld\n", ans[j]);
  }
  cout << flush;
  return 0;
}
Problem p03394 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vint;
typedef vector<string> vstr;
typedef pair<ll, ll> pint;
unsigned long long inf = (1LL << 62);
ll mod = (ll)pow(10.0, 9.0) + 7;
pint dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

ll min(ll a, ll b) { return (a < b) ? a : b; }
string S;

ll N;
vector<ll> ans;
int main() {

  cin >> N;

  if (N == 3) {
    cout << 2 << " " << 5 << " " << 63;
    return 0;
  }

  for (int i = 1; i <= min(15000, 3 * (N / 3)); i++) {
    ans.push_back(2 * i);
  }

  if (N > 15000 && N % 2 == 0) {
    for (int i = 1; i <= N - 15000; i++) {
      ans.push_back(3 * (2 * i - 1));
    }
  } else if (N > 15000 && N % 2 == 1) {
    ans[N - 1] = 3;

    for (int i = 1; i <= N - 15000; i++) {
      ans.push_back(3 * (2 * i + 1));
    }
  }

  else {
    if (N % 3 == 0) {
      ans[ans.size() - 3] = 3;
      ans[ans.size() - 2] = 9;
    }

    else if (N % 3 == 1) {
      ans[ans.size() - 1] = 3;
      ans.push_back(9);

    }

    else if (N % 3 == 2) {
      ans.push_back(3);
      ans.push_back(9);
    }
  }

  for (auto &ct : ans) {

    cout << ct << " ";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vint;
typedef vector<string> vstr;
typedef pair<ll, ll> pint;
unsigned long long inf = (1LL << 62);
ll mod = (ll)pow(10.0, 9.0) + 7;
pint dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

ll min(ll a, ll b) { return (a < b) ? a : b; }
string S;

ll N;
vector<ll> ans;
int main() {

  cin >> N;

  if (N == 3) {
    cout << 2 << " " << 5 << " " << 63;
    return 0;
  }

  for (int i = 1; i <= min(15000, 3 * (N / 3)); i++) {
    ans.push_back(2 * i);
  }

  if (N > 15000 && N % 2 == 0) {
    for (int i = 1; i <= N - 15000; i++) {
      ans.push_back(3 * (2 * i - 1));
    }
  } else if (N > 15000 && N % 2 == 1) {
    ans[ans.size() - 1] = 3;

    for (int i = 1; i <= N - 15000; i++) {
      ans.push_back(3 * (2 * i + 1));
    }
  }

  else {
    if (N % 3 == 0) {
      ans[ans.size() - 3] = 3;
      ans[ans.size() - 2] = 9;
    }

    else if (N % 3 == 1) {
      ans[ans.size() - 1] = 3;
      ans.push_back(9);

    }

    else if (N % 3 == 2) {
      ans.push_back(3);
      ans.push_back(9);
    }
  }

  for (auto &ct : ans) {

    cout << ct << " ";
  }

  return 0;
}
Problem p03394 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1061109567
#define INFLL 4557430888798830399
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i : x)
using namespace std;
int n;
vector<int> v;
signed main() {
#ifdef BLU
  in("blu.inp");
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  /*
  Firstly, a few elements are essential: 2, 3, 4 and 9. Then start putting
  multiples of 6 and we are good.
  */
  if (n == 3) {
    cout << "2 5 63";
    return 0;
  }
  v.pb(2), v.pb(3), v.pb(4), v.pb(9);
  n -= 4;
  int cur = 6;
  while (n > 1 && cur <= 30000) {
    v.pb(cur);
    n--;
    cur += 6;
  }
  cur = 8;
  while (n > 1 && cur <= 30000) {
    v.pb(cur), v.pb(cur + 2);
    n -= 2;
    cur += 6;
  }
  cur = 15;
  while (n > 1 && cur <= 30000) {
    v.pb(cur), v.pb(cur + 6);
    n -= 2;
    cur += 12;
  }
  if (n == 1) {
    v.erase(v.begin() + 4);
    v.pb(cur), v.pb(cur + 6);
  }
  fa(i, v) cout << i << " ";
#ifdef BLU
  int sum = 0;
  fa(i, v) sum += i;
  fa(i, v) if (__gcd(i, sum - i) == 1) {
    cout << "NO";
    return 0;
  }
#endif
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1061109567
#define INFLL 4557430888798830399
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i : x)
using namespace std;
int n;
vector<int> v;
signed main() {
#ifdef BLU
  in("blu.inp");
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  /*
  Firstly, a few elements are essential: 2, 3, 4 and 9. Then start putting
  multiples of 6 and we are good.
  */
  if (n == 3) {
    cout << "2 5 63";
    return 0;
  }
  if (n == 5) {
    cout << "2 3 4 9 6";
    return 0;
  }
  v.pb(2), v.pb(3), v.pb(4), v.pb(9);
  n -= 4;
  int cur = 6;
  while (n > 1 && cur <= 30000) {
    v.pb(cur);
    n--;
    cur += 6;
  }
  cur = 8;
  while (n > 1 && cur <= 30000) {
    v.pb(cur), v.pb(cur + 2);
    n -= 2;
    cur += 6;
  }
  cur = 15;
  while (n > 1 && cur <= 30000) {
    v.pb(cur), v.pb(cur + 6);
    n -= 2;
    cur += 12;
  }
  if (n == 1) {
    v.erase(v.begin() + 4);
    v.pb(cur), v.pb(cur + 6);
  }
  fa(i, v) cout << i << " ";
#ifdef BLU
  int sum = 0;
  fa(i, v) sum += i;
  fa(i, v) if (__gcd(i, sum - i) == 1) {
    cout << "NO";
    return 0;
  }
#endif
  return 0;
}
Problem p03394 (C++)

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

using namespace std;

#define fo(i, n) for (int i = 1; i <= n; ++i)

#define pb push_back
#define eb emplace_back

typedef long long ll;
const int N = 200200;
const int mod = 1e9 + 7;
const int K = 30000;

int n;
int p[N];
int used[N];
int a[N];
inline bool dfs(int v, int last = 1) {
  if (v > n) {
    int s = 0, g = 0;
    for (int i = 1; i <= n; ++i)
      s += a[i], g = __gcd(g, a[i]);
    if (g != 1)
      return 0;
    for (int i = 1; i <= n; ++i)
      if (__gcd(a[i], s - a[i]) == 1)
        return 0;
    fo(i, n) cout << a[i] << ' ';
    exit(0);
    return 1;
  }
  for (int i = last + 1; i <= K; i++) {
    a[v] = i;
    if (dfs(v + 1, i))
      return 1;
  }
  return 0;
}

inline int lcm(int x, int y) { return x / __gcd(x, y) * y; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  memset(used, 0, sizeof used);
  int x = 0;
  for (int i = 2; i <= 11; ++i) {
    for (int j = i; j <= 30000; j += i)
      used[j] = 1;
  }
  int cnt = 0;
  int last = 0;
  for (int i = 1; cnt + 2 < n && i <= 30000; ++i)
    if (used[i]) {
      a[++cnt] = i;
      last = i;
    }
  dfs(n - 1, last);
  return 0;
}

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

using namespace std;

#define fo(i, n) for (int i = 1; i <= n; ++i)

#define pb push_back
#define eb emplace_back

typedef long long ll;
const int N = 200200;
const int mod = 1e9 + 7;
const int K = 30000;

int n;
int p[N];
int used[N];
int a[N];
inline bool dfs(int v, int last = 1) {
  if (v > n) {
    int s = 0, g = 0;
    for (int i = 1; i <= n; ++i)
      s += a[i], g = __gcd(g, a[i]);
    if (g != 1)
      return 0;
    for (int i = 1; i <= n; ++i)
      if (__gcd(a[i], s - a[i]) == 1)
        return 0;
    fo(i, n) cout << a[i] << ' ';
    exit(0);
    return 1;
  }
  for (int i = last + 1; i <= K; i++) {
    if (!used[i])
      continue;
    a[v] = i;
    if (dfs(v + 1, i))
      return 1;
  }
  return 0;
}

inline int lcm(int x, int y) { return x / __gcd(x, y) * y; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  memset(used, 0, sizeof used);
  int x = 0;
  for (int i = 2; i <= 11; ++i) {
    for (int j = i; j <= 30000; j += i)
      used[j] = 1;
  }
  int cnt = 0;
  int last = 0;
  for (int i = 1; cnt + 2 < n && i <= 30000; ++i)
    if (used[i]) {
      a[++cnt] = i;
      last = i;
    }
  dfs(n - 1, last);
  return 0;
}
Problem p03394 (C++)

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

int main() {
  int N;
  cin >> N;
  vi table;
  for (int i = 1; i <= 15000; ++i) {
    table.push_back(2 * i);
  }
  for (int i = 0; i < 5000; ++i) {
    table.push_back(3 + 6 * i);
  }

  if (N == 3) {
    cout << 2 << ' ' << 3 << ' ' << 25;
    return 0;
  }
  if (N == 4) {
    cout << 2 << ' ' << 3 << ' ' << 25 << ' ' << 30;
    return 0;
  }
  if (N == 6) {
    cout << 2 << ' ' << 3 << ' ' << 25 << ' ' << 30 << ' ' << 60 << ' ' << 90;
    return 0;
  }
  if (N <= 15000) {
    if (N % 3 == 1) {
      cout << 3 << ' ' << 9 << ' ' << 15 << ' ' << 21 << ' ';
      for (int i = 1; i <= N - 4; ++i) {
        cout << 2 * i << ' ';
      }
      return 0;
    }

    if (N % 3 == 2) {
      cout << 3 << ' ' << 9 << ' ';
      for (int i = 1; i <= N - 2; ++i) {
        cout << 2 * i << ' ';
      }
      return 0;
    }
    if (N % 3 == 0) {
      cout << 3 << ' ' << 9 << ' ' << 15 << ' ' << 21 << ' ' << 27 << ' ' << 33
           << ' ';
      for (int i = 1; i <= N - 6; ++i) {
        cout << 2 * i << ' ';
      }
      return 0;
    }
  }

  if (N % 2 == 0) {
    for (int i = 0; i < N; ++i) {
      cout << table[i] << ' ';
    }
    return 0;
  }
  if (N % 2 == 1) {
    for (int i = 0; i < 14999; ++i) {
      cout << table[i] << ' ';
    }
    for (int i = 0; N - 14999; ++i) {
      cout << table[15000 + i] << ' ';
    }
    return 0;
  }
}


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

int main() {
  int N;
  cin >> N;
  vi table;
  for (int i = 1; i <= 15000; ++i) {
    table.push_back(2 * i);
  }
  for (int i = 0; i < 5000; ++i) {
    table.push_back(3 + 6 * i);
  }

  if (N == 3) {
    cout << 2 << ' ' << 3 << ' ' << 25;
    return 0;
  }
  if (N == 4) {
    cout << 2 << ' ' << 3 << ' ' << 25 << ' ' << 30;
    return 0;
  }
  if (N == 6) {
    cout << 2 << ' ' << 3 << ' ' << 25 << ' ' << 30 << ' ' << 60 << ' ' << 90;
    return 0;
  }
  if (N <= 15000) {
    if (N % 3 == 1) {
      cout << 3 << ' ' << 9 << ' ' << 15 << ' ' << 21 << ' ';
      for (int i = 1; i <= N - 4; ++i) {
        cout << 2 * i << ' ';
      }
      return 0;
    }

    if (N % 3 == 2) {
      cout << 3 << ' ' << 9 << ' ';
      for (int i = 1; i <= N - 2; ++i) {
        cout << 2 * i << ' ';
      }
      return 0;
    }
    if (N % 3 == 0) {
      cout << 3 << ' ' << 9 << ' ' << 15 << ' ' << 21 << ' ' << 27 << ' ' << 33
           << ' ';
      for (int i = 1; i <= N - 6; ++i) {
        cout << 2 * i << ' ';
      }
      return 0;
    }
  }

  if (N % 2 == 0) {
    for (int i = 0; i < N; ++i) {
      cout << table[i] << ' ';
    }
    return 0;
  }
  if (N % 2 == 1) {
    for (int i = 0; i < 14999; ++i) {
      cout << table[i] << ' ';
    }
    for (int i = 0; i < N - 14999; ++i) {
      cout << table[15000 + i] << ' ';
    }
    return 0;
  }
}
Problem p03394 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define f(a, n) for (int a = 0; a < n; a++)
#define F(a, n) for (int a = 1; a <= n; a++)
using namespace std;
int n;
bool A[30005];
int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(0);
  set<int> S;
  cin >> n;
  if (n < 900) {
    cout << "22 3 5 ";
    F(i, n - 3) cout << 30 * i << " ";
    cout << endl;
  } else {
    int sum = 0;
    int cnt = 0;
    F(i, 30000) {
      if (cnt == n)
        break;
      if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
        sum += i;
        cnt++;
        A[i] = 1;
      }
    }
    while (sum % 30 != 0) {
      while (1) { // del
        int x = (rand() % 30000) + 1;
        if (A[x]) {
          sum -= x;
          A[x] = 0;
          break;
        }
      }
      while (1) {
        int x = (rand() % 30000) + 1;
        if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0)
          if (A[x] == 0) {
            sum += x;
            A[x] = 1;
          }
      }
    }
    f(i, 30000) if (A[i]) cout << i << " ";
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define f(a, n) for (int a = 0; a < n; a++)
#define F(a, n) for (int a = 1; a <= n; a++)
using namespace std;
int n;
bool A[30005];
int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(0);
  set<int> S;
  cin >> n;
  if (n < 900) {
    cout << "22 3 5 ";
    F(i, n - 3) cout << 30 * i << " ";
    cout << endl;
  } else {
    int sum = 0;
    int cnt = 0;
    F(i, 30000) {
      if (cnt == n)
        break;
      if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
        sum += i;
        cnt++;
        A[i] = 1;
      }
    }
    while (sum % 30 != 0) {
      while (1) { // del
        int x = (rand() % 30000) + 1;
        if (A[x]) {
          sum -= x;
          A[x] = 0;
          break;
        }
      }
      while (1) {
        int x = (rand() % 30000) + 1;
        if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0)
          if (A[x] == 0) {
            sum += x;
            A[x] = 1;
            break;
          }
      }
    }
    f(i, 30000) if (A[i]) cout << i << " ";
    cout << endl;
  }
}
Problem p03394 (C++)

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

signed main() {
  int N;
  cin >> N;
  int E[2][8] = {{2, 10, 3, 9, 4, 8, 6, 12}, {6, 2, 10, 3, 9, 4, 8, 12}};
  if (N == 3) {
    cout << "2 5 63 " << endl;
    return 0;
  }
  int out = 0;
  REP(i, 30001) {
    cout << E[N % 2][i] + (i / 12) * 12 << " ";
    out++;
    if (out == N)
      break;
  }
  cout << endl;
  return 0;
}


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

signed main() {
  int N;
  cin >> N;
  int E[2][8] = {{2, 10, 3, 9, 4, 8, 6, 12}, {6, 2, 10, 3, 9, 4, 8, 12}};
  if (N == 3) {
    cout << "2 5 63 " << endl;
    return 0;
  }
  REP(i, N) { cout << E[N % 2][(i % 8)] + (i / 8) * 12 << " "; }
  cout << endl;
  return 0;
}
Problem p03394 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

int n;

VI v;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  cin >> n;

  for (int i = 1; i <= 30000; ++i) {
    if (i % 2 == 0 || i % 3 == 0)
      v.pb(i);
  }

  srand(21);

  while (1) {
    random_shuffle(ALL(v));

    int curr = 0, g = v[0];

    for (int i = 0; i < n; ++i) {
      curr += v[i];
      curr %= 6;
      g = __gcd(v[i], g);
    }

    if (!curr && g == 1) {
      for (int i = 0; i < n; ++i) {
        cout << v[i] << " ";
      }

      exit(0);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

int n;

VI v;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  cin >> n;

  if (n == 3) {
    cout << 2 << " " << 5 << " " << 63 << endl;
    return 0;
  }

  for (int i = 1; i <= 30000; ++i) {
    if (i % 2 == 0 || i % 3 == 0)
      v.pb(i);
  }

  srand(21);

  while (1) {
    random_shuffle(ALL(v));

    int curr = 0, g = v[0];

    for (int i = 0; i < n; ++i) {
      curr += v[i];
      curr %= 6;
      g = __gcd(v[i], g);
    }

    if (!curr && g == 1) {
      for (int i = 0; i < n; ++i) {
        cout << v[i] << " ";
      }

      exit(0);
    }
  }
}