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

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ut i = (a); i < (ut)(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define cat // cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
const int SIZE = 5 + 3 * 1e5;
const int INF = 10 + 1e9;
const LL p = 7 + 1e9;
const LL c = 1 << 30;
LL A[SIZE];
int N;
LL gcd(LL a, LL b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
bool solve() {
  LL gcds = A[0];
  LL sum = 0;
  int odds = 0;
  bool isEnd = false;
  REP(i, N) {
    sum += A[i] - 1;
    if (A[i] % 2)
      odds++;
    if (A[i] == 1)
      isEnd = true;
    else
      gcds = gcd(gcds, A[i] / 2 * 2);
  }
  sum %= 2;
  bool isFirst = sum;
  if (N == 2 or isEnd or odds >= 2)
    return isFirst;
  if (isFirst)
    return isFirst;
  REP(i, N)
  A[i] /= gcds;
  return !solve();
}
int main() {
  cin >> N;
  REP(i, N)
  cin >> A[i];
  if (solve())
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ut i = (a); i < (ut)(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define cat // cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
const int SIZE = 5 + 3 * 1e5;
const int INF = 10 + 1e9;
const LL p = 7 + 1e9;
const LL c = 1 << 30;
LL A[SIZE];
int N;
LL gcd(LL a, LL b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
bool solve() {
  LL gcds = A[0] / 2 * 2;
  LL sum = 0;
  int odds = 0;
  bool isEnd = false;
  REP(i, N) {
    sum += A[i] - 1;
    if (A[i] % 2)
      odds++;
    if (A[i] == 1)
      isEnd = true;
    else
      gcds = gcd(gcds, A[i] / 2 * 2);
  }
  sum %= 2;
  bool isFirst = sum;
  if (N == 2 or isEnd or odds >= 2)
    return isFirst;
  if (isFirst)
    return isFirst;
  REP(i, N)
  A[i] /= gcds;
  return !solve();
}
int main() {
  cin >> N;
  REP(i, N)
  cin >> A[i];
  if (solve())
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}
Problem p03810 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1e5 + 5;

typedef long long LL;

int n, a[N];

bool ans;

int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i, p, s;;) {
    for (p = s = 0, i = 1; i <= n; i++)
      if (a[i] & 1)
        s++;
      else
        p ^= 1;
    if (p) {
      ans ^= 1;
      break;
    }
    if (s > 1)
      break;
    for (i = 1; i <= n; i++)
      if (a[i] & 1)
        break;
    if (a[i] == 1)
      break;
    s = a[1];
    for (i = 2; i <= n; i++)
      s = gcd(s, a[i]);
    for (i = 1; i <= n; i++)
      a[i] /= s;
    ans ^= 1;
  }
  if (ans)
    printf("First");
  else
    printf("Second");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1e5 + 5;

typedef long long LL;

int n, a[N];

bool ans;

int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i, p, s;;) {
    for (p = s = 0, i = 1; i <= n; i++)
      if (a[i] & 1)
        s++;
      else
        p ^= 1;
    if (p) {
      ans ^= 1;
      break;
    }
    if (s > 1)
      break;
    for (i = 1; i <= n; i++)
      if (a[i] & 1)
        break;
    if (a[i] == 1)
      break;
    a[i]--;
    s = a[1];
    for (i = 2; i <= n; i++)
      s = gcd(s, a[i]);
    for (i = 1; i <= n; i++)
      a[i] /= s;
    ans ^= 1;
  }
  if (ans)
    printf("First");
  else
    printf("Second");
  return 0;
}
Problem p03810 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, a[100007];
bool chk() {
  int c0 = 0;
  for (int i = 0; i < n; ++i)
    c0 += ~a[i] & 1;
  if (~n & 1)
    return c0 & 1;
  if (c0 & 1)
    return 1;
  if (c0 == n - 1) {
    int p, g = 0;
    for (int i = 0; i < n; ++i)
      if (a[i] & 1)
        p = i;
    --a[p];
    for (int i = 0; i < n; ++i)
      g = gcd(a[i], g);
    if (g == 1)
      return 0;
    for (int i = 0; i < n; ++i)
      a[i] /= g;
    return !chk();
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  puts(chk() ? "First" : "Second");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, a[100007];
bool chk() {
  int c0 = 0;
  for (int i = 0; i < n; ++i)
    c0 += ~a[i] & 1;
  if (~n & 1)
    return c0 & 1;
  if (c0 & 1)
    return 1;
  if (c0 == n - 1) {
    int p, g = 0;
    for (int i = 0; i < n; ++i)
      if (a[i] & 1)
        p = i;
    if (a[p] == 1)
      return 0;
    --a[p];
    for (int i = 0; i < n; ++i)
      g = gcd(a[i], g);
    if (g == 1)
      return 0;
    for (int i = 0; i < n; ++i)
      a[i] /= g;
    return !chk();
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  puts(chk() ? "First" : "Second");
  return 0;
}
Problem p03810 (C++)

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

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int, int>
#define cr const reg &
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) { return x > y ? x : y; }
I int _min(cr x, cr y) { return x < y ? x : y; }
I int read() {
  reg x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
I void ptt(cr x) {
  if (x >= 10)
    ptt(x / 10);
  putchar(x % 10 + '0');
}
I void put(cr x) { x < 0 ? putchar('-'), ptt(-x) : ptt(x); }
I void pr1(cr x) { put(x), putchar(' '); }
I void pr2(cr x) { put(x), puts(""); }

int n, a[N];

int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }

bool dfs(cr x) {
  bool bk = 0;
  int s = 0;
  for (reg i = 1; i <= n; i++) {
    if (a[i] == 1)
      bk = 1;
    if (a[i] & 1)
      s++;
  }
  if ((n - s) & 1)
    return x;
  if (bk && s > 1)
    return x;
  for (reg i = 1; i <= n; i++) {
    if (a[i] & 1) {
      a[i]--;
      break;
    }
  }
  int qq = a[1];
  for (reg i = 2; i <= n; i++)
    qq = gcd(qq, a[i]);
  for (reg i = 1; i <= n; i++)
    a[i] /= qq;
  return dfs(!x);
}

int main() {
  n = read();
  for (reg i = 1; i <= n; i++)
    a[i] = read();
  if (dfs(1))
    puts("First");
  else
    puts("Second");
  return 0;
}


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

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int, int>
#define cr const reg &
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) { return x > y ? x : y; }
I int _min(cr x, cr y) { return x < y ? x : y; }
I int read() {
  reg x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
I void ptt(cr x) {
  if (x >= 10)
    ptt(x / 10);
  putchar(x % 10 + '0');
}
I void put(cr x) { x < 0 ? putchar('-'), ptt(-x) : ptt(x); }
I void pr1(cr x) { put(x), putchar(' '); }
I void pr2(cr x) { put(x), puts(""); }

int n, a[N];

int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }

bool dfs(cr x) {
  bool bk = 0;
  int s = 0;
  for (reg i = 1; i <= n; i++) {
    if (a[i] == 1)
      bk = 1;
    if (a[i] & 1)
      s++;
  }
  if ((n - s) & 1)
    return x;
  if (bk || s > 1)
    return !x;
  for (reg i = 1; i <= n; i++) {
    if (a[i] & 1) {
      a[i]--;
      break;
    }
  }
  int qq = a[1];
  for (reg i = 2; i <= n; i++)
    qq = gcd(qq, a[i]);
  for (reg i = 1; i <= n; i++)
    a[i] /= qq;
  return dfs(!x);
}

int main() {
  n = read();
  for (reg i = 1; i <= n; i++)
    a[i] = read();
  if (dfs(1))
    puts("First");
  else
    puts("Second");
  return 0;
}
Problem p03810 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int> pa;
#define w1 first
#define w2 second
#define ls (x << 1)
#define rs (x << 1 | 1)
#define pb push_back
#define mp make_pair
#define mid ((l + r) >> 1)
#define SZ(x) ((x).size())
#define All(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
int power(int x, int k, int p) {
  int res = 1;
  for (; k; k >>= 1, x = 1ll * x * x % p)
    if (k & 1)
      res = 1ll * res * x % p;
  return res;
}
int getgcd(int x, int y) { return !x ? y : getgcd(y % x, x); }
const int maxn = 1e5 + 5;
int n, ans;
int a[maxn];
void check() {
  int cnt = 0;
  rep(i, 1, n) cnt += !(a[i] & 1);
  if (!(cnt & 1)) {
    ans ^= 1;
    if (cnt + 1 == n) {
      rep(i, 1, n) if (a[i] & 1) a[i]--;
      int d = 0;
      rep(i, 1, n) d = getgcd(d, a[i]);
      rep(i, 1, n) a[i] /= d;
      check();
    }
  }
}
int main() {
  read(n);
  rep(i, 1, n) read(a[i]);
  check();
  if (ans)
    puts("Second");
  else
    puts("First");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int> pa;
#define w1 first
#define w2 second
#define ls (x << 1)
#define rs (x << 1 | 1)
#define pb push_back
#define mp make_pair
#define mid ((l + r) >> 1)
#define SZ(x) ((x).size())
#define All(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
int power(int x, int k, int p) {
  int res = 1;
  for (; k; k >>= 1, x = 1ll * x * x % p)
    if (k & 1)
      res = 1ll * res * x % p;
  return res;
}
int getgcd(int x, int y) { return !x ? y : getgcd(y % x, x); }
const int maxn = 1e5 + 5;
int n, ans;
int a[maxn];
void check() {
  int cnt = 0;
  rep(i, 1, n) cnt += !(a[i] & 1);
  if (!(cnt & 1)) {
    ans ^= 1;
    if (cnt + 1 == n) {
      rep(i, 1, n) if (a[i] & 1) {
        if (a[i] == 1)
          return;
        a[i]--;
      }
      int d = 0;
      rep(i, 1, n) d = getgcd(d, a[i]);
      rep(i, 1, n) a[i] /= d;
      check();
    }
  }
}
int main() {
  read(n);
  rep(i, 1, n) read(a[i]);
  check();
  if (ans)
    puts("Second");
  else
    puts("First");
  return 0;
}
Problem p03810 (C++)

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

const int MAX_N = 100005;
int A[MAX_N], N;

int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); }

int work() {
  int even = 0, odd = 0;
  for (int i = 1; i <= N; ++i)
    if (A[i] & 1)
      odd++;
    else
      even++;
  if (even & 1)
    return 1;
  else if (!(even & 1) && odd > 1)
    return 0;
  else {
    for (int i = 1; i <= N; ++i)
      if (A[i] & 1) {
        if (A[i] == 1)
          return 0;
        A[i]--;
        break;
      }
    int g = A[1];
    for (int i = 2; i <= N; ++i)
      g = gcd(g, A[i]);
    for (int i = 1; i <= N; ++i)
      A[i] /= g;
    return work() ^ 1;
  }
}

int main() {
  freopen("game.in", "r", stdin);
  freopen("game.out", "w", stdout);

  int T = 1;
  // scanf("%d", &T);

  while (T--) {
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i)
      scanf("%d", &A[i]);
    bool flag = work();
    if (flag)
      puts("First");
    else
      puts("Second");
  }

  return 0;
}


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

const int MAX_N = 100005;
int A[MAX_N], N;

int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); }

int work() {
  int even = 0, odd = 0;
  for (int i = 1; i <= N; ++i)
    if (A[i] & 1)
      odd++;
    else
      even++;
  if (even & 1)
    return 1;
  else if (!(even & 1) && odd > 1)
    return 0;
  else {
    for (int i = 1; i <= N; ++i)
      if (A[i] & 1) {
        if (A[i] == 1)
          return 0;
        A[i]--;
        break;
      }
    int g = A[1];
    for (int i = 2; i <= N; ++i)
      g = gcd(g, A[i]);
    for (int i = 1; i <= N; ++i)
      A[i] /= g;
    return work() ^ 1;
  }
}

int main() {

  int T = 1;
  // scanf("%d", &T);

  while (T--) {
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i)
      scanf("%d", &A[i]);
    bool flag = work();
    if (flag)
      puts("First");
    else
      puts("Second");
  }

  return 0;
}
Problem p03810 (C++)

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

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

bool solve(vi os, vi es) {
  bool one = 0;
  for (int x : os) {
    if (x == -1)
      one = 1;
  }

  if (one) {
    int s = 0;
    for (int x : es) {
      s ^= 1;
    }
    return s;
  }

  if (es.size() % 2 == 1) {
    return 1;
  }

  if (os.size() >= 2) {
    return 0;
  }

  es.pb(os[0] - 1);
  int d = -1;
  for (int x : es) {
    if (d == -1) {
      d = x;
    } else {
      d = __gcd(d, x);
    }
  }

  vi _os, _es;
  for (int x : es) {
    int nx = x / d;
    if (nx & 1) {
      _os.pb(nx);
    } else {
      _es.pb(nx);
    }
  }
  return !solve(_os, _es);
}

int main() {
  int N;
  vi os, es;
  cin >> N;

  rep(i, N) {
    int x;
    cin >> x;
    if (x & 1) {
      os.pb(x);
    } else {
      es.pb(x);
    }
  }

  puts(solve(os, es) ? "First" : "Second");

  return 0;
}

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

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

bool solve(vi os, vi es) {
  bool one = 0;
  for (int x : os) {
    if (x == 1)
      one = 1;
  }

  if (one) {
    int s = 0;
    for (int x : es) {
      s ^= 1;
    }
    return s;
  }

  if (es.size() % 2 == 1) {
    return 1;
  }

  if (os.size() >= 2) {
    return 0;
  }

  es.pb(os[0] - 1);
  int d = -1;
  for (int x : es) {
    if (d == -1) {
      d = x;
    } else {
      d = __gcd(d, x);
    }
  }

  vi _os, _es;
  for (int x : es) {
    int nx = x / d;
    if (nx & 1) {
      _os.pb(nx);
    } else {
      _es.pb(nx);
    }
  }
  return !solve(_os, _es);
}

int main() {
  int N;
  vi os, es;
  cin >> N;

  rep(i, N) {
    int x;
    cin >> x;
    if (x & 1) {
      os.pb(x);
    } else {
      es.pb(x);
    }
  }

  puts(solve(os, es) ? "First" : "Second");

  return 0;
}
Problem p03811 (C++)

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

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back

const int N = 2e3 + 5;

typedef long long ll;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }

vi mer(vi a, vi b) {
  vi c;
  int i = 0, j = 0;
  while (i < sz(a) && j < sz(b)) {
    if (a[i] < b[j])
      c.pb(b[j++]);
    else
      c.pb(a[i++]);
  }
  while (i < sz(a))
    c.pb(a[i++]);
  while (j < sz(b))
    c.pb(b[j++]);
  return c;
}

vector<int> g[N], ar[N];

ll a[N];
int p[N], n;

void dfs(int u) {
  for (int v : g[u])
    if (v != p[u] && p[v] < 0) {
      p[v] = u;
      dfs(v);
    }
}

void sol(int u) {
  ar[u].push_back(u);
  vector<int> res;
  for (int v : g[u])
    if (p[v] == u) {
      sol(v);
      res = mer(res, ar[v]);
    }
  for (int i = 0; i < sz(res); ++i)
    ar[u].pb(res[i]);
}

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

  cin >> n;

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

  sort(a + 1, a + 1 + n);

  int j = 1;
  while (a[j] < 2)
    ++j;

  vector<int> res(j - 1, 1);

  n -= (j - 1);
  for (int i = 1; i <= n; ++i)
    a[i] = a[i + j - 1];

  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= n; ++j) {
      if (i == j || gcd(a[i], a[j]) < 2)
        continue;
      g[i].pb(j);
    }

  memset(p, -1, sizeof p);
  p[0] = 0;
  dfs(0);
  sol(0);

  for (int i = sz(ar[0]) - 1; i; --i)
    res.pb(a[ar[0][i]]);

  reverse(all(res));
  for (int i = 0; i < sz(res); ++i)
    cout << res[i] << ' ';
}

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

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back

const int N = 2e3 + 5;

typedef long long ll;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }

vi mer(vi a, vi b) {
  vi c;
  int i = 0, j = 0;
  while (i < sz(a) && j < sz(b)) {
    if (a[i] < b[j])
      c.pb(b[j++]);
    else
      c.pb(a[i++]);
  }
  while (i < sz(a))
    c.pb(a[i++]);
  while (j < sz(b))
    c.pb(b[j++]);
  return c;
}

vector<int> g[N], ar[N];

ll a[N];
int p[N], n;

void dfs(int u) {
  for (int v : g[u])
    if (v != p[u] && p[v] < 0) {
      p[v] = u;
      dfs(v);
    }
}

void sol(int u) {
  ar[u].push_back(u);
  vector<int> res;
  for (int v : g[u])
    if (p[v] == u) {
      sol(v);
      res = mer(res, ar[v]);
    }
  for (int i = 0; i < sz(res); ++i)
    ar[u].pb(res[i]);
}

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

  cin >> n;

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

  sort(a + 1, a + 1 + n);

  int j = 1;
  while (j <= n && a[j] < 2)
    ++j;

  vector<int> res(j - 1, 1);

  n -= (j - 1);
  for (int i = 1; i <= n; ++i)
    a[i] = a[i + j - 1];

  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= n; ++j) {
      if (i == j || gcd(a[i], a[j]) < 2)
        continue;
      g[i].pb(j);
    }

  memset(p, -1, sizeof p);
  p[0] = 0;
  dfs(0);
  sol(0);

  for (int i = sz(ar[0]) - 1; i; --i)
    res.pb(a[ar[0][i]]);

  reverse(all(res));
  for (int i = 0; i < sz(res); ++i)
    cout << res[i] << ' ';
}
Problem p03811 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < int(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()

vi combine(const vi &l, const vi &r) {
  if (l.empty() || r.empty())
    return l.empty() ? r : l;

  vi res(sz(l) + sz(r));
  int pl = 0, pr = 0, h = 0;
  while (pl < sz(l) && pr < sz(r))
    res[h++] = (l[pl] > r[pr] ? l[pl++] : r[pr++]);
  while (pl < sz(l))
    res[h++] = l[pl++];
  while (pr < sz(r))
    res[h++] = l[pl++];
  assert(h == sz(res));

  return res;
}

vi dfs(int x, const vvi &g, vector<char> &used) {
  used[x] = 1;
  vi res;

  for (int dest : g[x])
    if (!used[dest])
      res = combine(res, dfs(dest, g, used));

  res.insert(res.begin(), x);
  return res;
}

void solve(int n) {
  vi a(n);
  forn(i, n) cin >> a[i];
  sort(ALL(a));

  vvi g(n);
  forn(i, n) forn(j, i) if (__gcd(a[i], a[j]) != 1) {
    g[i].pb(j);
    g[j].pb(i);
  }

  forn(i, n) sort(ALL(g[i]));

  vi res;
  vector<char> used(n);
  forn(i, n) if (!used[i]) res = combine(res, dfs(i, g, used));

  forn(i, n) cout << a[res[i]] << " \n"[i + 1 == n];
}

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

  int n;
  while (cin >> n)
    solve(n);
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < int(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()

vi combine(const vi &l, const vi &r) {
  if (l.empty() || r.empty())
    return l.empty() ? r : l;

  vi res(sz(l) + sz(r));
  int pl = 0, pr = 0, h = 0;
  while (pl < sz(l) && pr < sz(r))
    res[h++] = (l[pl] > r[pr] ? l[pl++] : r[pr++]);
  while (pl < sz(l))
    res[h++] = l[pl++];
  while (pr < sz(r))
    res[h++] = r[pr++];
  assert(h == sz(res));

  return res;
}

vi dfs(int x, const vvi &g, vector<char> &used) {
  used[x] = 1;
  vi res;

  for (int dest : g[x])
    if (!used[dest])
      res = combine(res, dfs(dest, g, used));

  res.insert(res.begin(), x);
  return res;
}

void solve(int n) {
  vi a(n);
  forn(i, n) cin >> a[i];
  sort(ALL(a));

  vvi g(n);
  forn(i, n) forn(j, i) if (__gcd(a[i], a[j]) != 1) {
    g[i].pb(j);
    g[j].pb(i);
  }

  forn(i, n) sort(ALL(g[i]));

  vi res;
  vector<char> used(n);
  forn(i, n) if (!used[i]) res = combine(res, dfs(i, g, used));

  forn(i, n) cout << a[res[i]] << " \n"[i + 1 == n];
}

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

  int n;
  while (cin >> n)
    solve(n);
}
Problem p03812 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define LL long long
#define pb push_back
const int N = 3005;
int v[N * 2], nxt[N * 2], point[N], bs;
int ans[N];
LL a[N];
vector<int> vp;
void addedge(int x, int y) {
  bs++;
  v[bs] = y;
  nxt[bs] = point[x];
  point[x] = bs;
}
void dfs(int x) {
  int p, tp = 1;
  for (p = point[x]; p; p = nxt[p]) {
    if (ans[v[p]] == -1)
      dfs(v[p]);
    tp = min(tp, ans[v[p]]);
  }
  ans[x] = 1 - tp;
}
int main() {
  //  freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
  int i, n;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (a[x] < a[y])
      swap(x, y);
    if (a[x] > a[y])
      addedge(x, y);
  }
  for (i = 1; i <= n; i++)
    ans[i] = -1;
  for (i = 1; i <= n; i++)
    if (ans[i] == -1)
      dfs(i);
  for (i = 1; i <= n; i++)
    if (ans[i] == 1)
      vp.pb(i);
  int tot = vp.size();
  printf("%d", vp[0]);
  for (i = 1; i < tot; i++)
    printf(" %d", vp[i]);
  puts("");

  return 0;
}


Fixed submission:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define LL long long
#define pb push_back
const int N = 3005;
int v[N * 2], nxt[N * 2], point[N], bs;
int ans[N];
LL a[N];
vector<int> vp;
void addedge(int x, int y) {
  bs++;
  v[bs] = y;
  nxt[bs] = point[x];
  point[x] = bs;
}
void dfs(int x) {
  int p, tp = 1;
  for (p = point[x]; p; p = nxt[p]) {
    if (ans[v[p]] == -1)
      dfs(v[p]);
    tp = min(tp, ans[v[p]]);
  }
  ans[x] = 1 - tp;
}
int main() {
  //  freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
  int i, n;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (a[x] < a[y])
      swap(x, y);
    if (a[x] > a[y])
      addedge(x, y);
  }
  for (i = 1; i <= n; i++)
    ans[i] = -1;
  for (i = 1; i <= n; i++)
    if (ans[i] == -1)
      dfs(i);
  for (i = 1; i <= n; i++)
    if (ans[i] == 1)
      vp.pb(i);
  int tot = vp.size();
  if (!tot) {
    puts("");
    return 0;
  }
  printf("%d", vp[0]);
  for (i = 1; i < tot; i++)
    printf(" %d", vp[i]);
  puts("");

  return 0;
}
Problem p03812 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define N (3005)

using namespace std;

struct edge {
  int nt, to;
} g[N << 1];

int head[N], sg[N], a[N], n, num;

il int gi() {
  RG int x = 0, q = 1;
  RG char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    q = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q * x;
}

il void insert(RG int from, RG int to) {
  g[++num] = (edge){head[from], to}, head[from] = num;
  return;
}

il void dfs(RG int x, RG int p) {
  sg[x] = 0;
  for (RG int i = head[x], v; i; i = g[i].nt) {
    v = g[i].to;
    if (v == p)
      continue;
    dfs(v, x);
    if (a[x] > a[v] && !sg[v]) {
      sg[x] = 1;
      return;
    }
  }
  return;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("game.in", "r", stdin);
  freopen("game.out", "w", stdout);
#endif
  n = gi();
  for (RG int i = 1; i <= n; ++i)
    a[i] = gi();
  for (RG int i = 1, u, v; i < n; ++i)
    u = gi(), v = gi(), insert(u, v), insert(v, u);
  for (RG int i = 1; i <= n; ++i) {
    dfs(i, 0);
    if (sg[i])
      printf("%d ", i);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define N (3005)

using namespace std;

struct edge {
  int nt, to;
} g[N << 1];

int head[N], sg[N], a[N], n, num;

il int gi() {
  RG int x = 0, q = 1;
  RG char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    q = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q * x;
}

il void insert(RG int from, RG int to) {
  g[++num] = (edge){head[from], to}, head[from] = num;
  return;
}

il void dfs(RG int x, RG int p) {
  sg[x] = 0;
  for (RG int i = head[x], v; i; i = g[i].nt) {
    v = g[i].to;
    if (v == p)
      continue;
    dfs(v, x);
    if (a[x] > a[v] && !sg[v]) {
      sg[x] = 1;
      return;
    }
  }
  return;
}

int main() {
  n = gi();
  for (RG int i = 1; i <= n; ++i)
    a[i] = gi();
  for (RG int i = 1, u, v; i < n; ++i)
    u = gi(), v = gi(), insert(u, v), insert(v, u);
  for (RG int i = 1; i <= n; ++i) {
    dfs(i, 0);
    if (sg[i])
      printf("%d ", i);
  }
  return 0;
}
Problem p03812 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(aa, bb, cc) for (int aa = (bb); aa <= (int)(cc); ++aa)
#define Forr(aa, bb, cc) for (int aa = (bb); aa >= (int)(cc); --aa)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 10;
int n;
int stone[maxn];
int be[maxn], ne[maxn << 1], to[maxn << 1], e;
int ans[maxn];
bool vis[maxn];

template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}

inline void add_edge(int x, int y) {
  to[++e] = y, ne[e] = be[x], be[x] = e;
  to[++e] = x, ne[e] = be[y], be[y] = e;
}

int dfs(int node) {
  ans[node] = 0;
  vis[node] = 1;
  for (int i = be[node]; i; i = ne[i]) {
    int u = to[i];
    if (stone[u] >= stone[node])
      continue;
    ans[node] |= !dfs(u);
  }
  return ans[node];
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif
  int x, y;
  read(n);
  For(i, 1, n) read(stone[i]);
  For(i, 1, n - 1) read(x), read(y), add_edge(x, y);
  For(i, 1, n) if (!vis[i]) dfs(i);
  For(i, 1, n) {
    if (ans[i]) {
      printf("%d ", i);
    }
  }
  puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(aa, bb, cc) for (int aa = (bb); aa <= (int)(cc); ++aa)
#define Forr(aa, bb, cc) for (int aa = (bb); aa >= (int)(cc); --aa)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 10;
int n;
int stone[maxn];
int be[maxn], ne[maxn << 1], to[maxn << 1], e;
int ans[maxn];
bool vis[maxn];

template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}

inline void add_edge(int x, int y) {
  to[++e] = y, ne[e] = be[x], be[x] = e;
  to[++e] = x, ne[e] = be[y], be[y] = e;
}

int dfs(int node) {
  ans[node] = 0;
  vis[node] = 1;
  for (int i = be[node]; i; i = ne[i]) {
    int u = to[i];
    if (stone[u] >= stone[node])
      continue;
    ans[node] |= !dfs(u);
  }
  return ans[node];
}

int main() {
  int x, y;
  read(n);
  For(i, 1, n) read(stone[i]);
  For(i, 1, n - 1) read(x), read(y), add_edge(x, y);
  For(i, 1, n) if (!vis[i]) dfs(i);
  For(i, 1, n) {
    if (ans[i]) {
      printf("%d ", i);
    }
  }
  puts("");
  return 0;
}
Problem p03812 (C++)

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

struct Graph {
  int n;
  vector<vector<int>> g;

  Graph(int n) : n(n) { g.resize(n); }

  void add_edge(int from, int to) { g[from].push_back(to); }
};

bool dfs(int u, Graph &g, int *a) {
  for (int v : g.g[u]) {
    if (a[v] < a[u]) {
      if (!dfs(v, g, a))
        return true;
    }
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  int a[3005];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  Graph g(n);
  for (int i = 0; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g.add_edge(u, v);
    g.add_edge(v, u);
  }
  for (int i = 0; i < n; i++) {
    if (dfs(i, g, a))
      cout << i + 1 << " ";
  }
  cout << endl;
}


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

struct Graph {
  int n;
  vector<vector<int>> g;

  Graph(int n) : n(n) { g.resize(n); }

  void add_edge(int from, int to) { g[from].push_back(to); }
};

bool dfs(int u, Graph &g, int *a) {
  for (int v : g.g[u]) {
    if (a[v] < a[u]) {
      if (!dfs(v, g, a))
        return true;
    }
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  int a[3005];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  Graph g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g.add_edge(u, v);
    g.add_edge(v, u);
  }
  for (int i = 0; i < n; i++) {
    if (dfs(i, g, a))
      cout << i + 1 << " ";
  }
  cout << endl;
}
Problem p03812 (C++)

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

struct edge {
  int s, t, n;
} e[3010];
int n, h[3010], a[3010], d[3010], f[3010];
queue<int> Q;

void work() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1, u, v, tot = 0; i < n; i++) {
    scanf("%d %d", &u, &v);
    e[++tot] = (edge){u, v, h[u]}, h[u] = tot, d[u] += a[u] > a[v];
    e[++tot] = (edge){v, u, h[v]}, h[v] = tot, d[v] += a[v] > a[u];
  }
  for (int i = 1; i <= n; i++)
    if (!d[i])
      Q.push(i);
  for (int x, y, i; !Q.empty();)
    for (i = h[x = Q.front()], Q.pop(); y = e[i].t, i; i = e[i].n)
      if (a[x] > a[y])
        f[x] |= !f[y];
      else if ((a[x] < a[y]) && (!(--d[y])))
        Q.push(y);
  for (int i = 1; i <= n; i++)
    if (f[i])
      printf("%d ", i);
}

int main() {
  work();
  return 0;
}


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

struct edge {
  int s, t, n;
} e[6010];
int n, h[3010], a[3010], d[3010], f[3010];
queue<int> Q;

void work() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1, u, v, tot = 0; i < n; i++) {
    scanf("%d %d", &u, &v);
    e[++tot] = (edge){u, v, h[u]}, h[u] = tot, d[u] += a[u] > a[v];
    e[++tot] = (edge){v, u, h[v]}, h[v] = tot, d[v] += a[v] > a[u];
  }
  for (int i = 1; i <= n; i++)
    if (!d[i])
      Q.push(i);
  for (int x, y, i; !Q.empty();)
    for (i = h[x = Q.front()], Q.pop(); y = e[i].t, i; i = e[i].n)
      if (a[x] > a[y])
        f[x] |= !f[y];
      else if ((a[x] < a[y]) && (!(--d[y])))
        Q.push(y);
  for (int i = 1; i <= n; i++)
    if (f[i])
      printf("%d ", i);
}

int main() {
  work();
  return 0;
}
Problem p03812 (C++)

Buggy submission (status: Runtime Error):
// Written By NewbieChd
#include <cctype>
#include <cstdio>
#include <vector>
using namespace std;

const int BUF = 1000000;
char buf[BUF], *p1, *p2;
inline char getChar() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int f = 0;
  char c;
  while (!isdigit(c = getChar())) {
  }
  do
    f = f * 10 + (c ^ 48);
  while (isdigit(c = getChar()));
  return f;
}

const int maxN = 2003;
vector<int> g[maxN];
int w[maxN];
bool dfs(int x) {
  for (auto y : g[x])
    if (w[y] < w[x] && !dfs(y))
      return 1;
  return 0;
}

int main() {
  int n = read(), i, x, y;
  for (i = 1; i <= n; ++i)
    w[i] = read();
  for (i = 1; i < n; ++i)
    x = read(), y = read(), g[x].push_back(y), g[y].push_back(x);
  for (i = 1; i <= n; ++i)
    if (dfs(i))
      printf("%d ", i);
  return 0;
}


Fixed submission:
// Written By NewbieChd
#include <cctype>
#include <cstdio>
#include <vector>
using namespace std;

const int BUF = 1000000;
char buf[BUF], *p1, *p2;
inline char getChar() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int f = 0;
  char c;
  while (!isdigit(c = getChar())) {
  }
  do
    f = f * 10 + (c ^ 48);
  while (isdigit(c = getChar()));
  return f;
}

const int maxN = 3007;
vector<int> g[maxN];
int w[maxN];
bool dfs(int x) {
  for (auto y : g[x])
    if (w[y] < w[x] && !dfs(y))
      return 1;
  return 0;
}

int main() {
  int n = read(), i, x, y;
  for (i = 1; i <= n; ++i)
    w[i] = read();
  for (i = 1; i < n; ++i)
    x = read(), y = read(), g[x].push_back(y), g[y].push_back(x);
  for (i = 1; i <= n; ++i)
    if (dfs(i))
      printf("%d ", i);
  return 0;
}
Problem p03812 (C++)

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

using namespace std;

const int maxn = 2010;

int n, a[maxn], ind[maxn];
int dp[maxn], l[maxn], e;

struct Edge {
  int v, x;
} E[maxn << 1];

bool cmp(int x, int y) { return a[x] < a[y]; }

inline void addEdge(int u, int v) {
  E[e].v = v;
  E[e].x = l[u];
  l[u] = e++;
}

int main() {
  memset(l, -1, sizeof(l));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    ind[i] = i;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(ind + 1, ind + n + 1, cmp);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addEdge(u, v);
    addEdge(v, u);
  }
  for (int _ = 1; _ <= n; _++) {
    int i = ind[_];
    for (int p = l[i]; p >= 0; p = E[p].x) {
      int v = E[p].v;
      if (a[v] < a[i]) {
        if (!dp[v]) {
          dp[i] = 1;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (dp[i]) {
      printf("%d ", i);
    }
  }
  puts("");
  return 0;
}

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

using namespace std;

const int maxn = 3010;

int n, a[maxn], ind[maxn];
int dp[maxn], l[maxn], e;

struct Edge {
  int v, x;
} E[maxn << 1];

bool cmp(int x, int y) { return a[x] < a[y]; }

inline void addEdge(int u, int v) {
  E[e].v = v;
  E[e].x = l[u];
  l[u] = e++;
}

int main() {
  memset(l, -1, sizeof(l));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    ind[i] = i;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(ind + 1, ind + n + 1, cmp);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addEdge(u, v);
    addEdge(v, u);
  }
  for (int _ = 1; _ <= n; _++) {
    int i = ind[_];
    for (int p = l[i]; p >= 0; p = E[p].x) {
      int v = E[p].v;
      if (a[v] < a[i]) {
        if (!dp[v]) {
          dp[i] = 1;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (dp[i]) {
      printf("%d ", i);
    }
  }
  puts("");
  return 0;
}
Problem p03812 (C++)

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

const int N = 1010;
int n, a[N], g[N];
vector<int> e[N];

int dfs(int v) {
  if (g[v] >= 0)
    return g[v];
  g[v] = 0;
  for (int i = 0; i < (int)e[v].size(); i++) {
    int u = e[v][i];
    g[v] |= !dfs(u);
  }
  return g[v];
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    if (a[x] > a[y])
      e[x].push_back(y);
    else if (a[x] < a[y]) {
      e[y].push_back(x);
    }
  }
  fill_n(g, n, -1);
  for (int i = 0; i < n; i++)
    if (dfs(i))
      printf("%d ", i + 1);
  printf("\n");
}

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

const int N = 3010;
int n, a[N], g[N];
vector<int> e[N];

int dfs(int v) {
  if (g[v] >= 0)
    return g[v];
  g[v] = 0;
  for (int i = 0; i < (int)e[v].size(); i++) {
    int u = e[v][i];
    g[v] |= !dfs(u);
  }
  return g[v];
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    if (a[x] > a[y])
      e[x].push_back(y);
    else if (a[x] < a[y]) {
      e[y].push_back(x);
    }
  }
  fill_n(g, n, -1);
  for (int i = 0; i < n; i++)
    if (dfs(i))
      printf("%d ", i + 1);
  printf("\n");
}
Problem p03812 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define N 3050
int n, a[N], head[N], to[N << 1], nxt[N << 1], cnt;
int mn[N], se[N], is[N];
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
}
int dfs(int x) {
  int i;
  for (i = head[x]; i; i = nxt[i])
    if (a[to[i]] < a[x] || !dfs(to[i]))
      return 1;
  return 0;
}
int main() {
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int x, y;
  for (i = 1; i < n; i++)
    scanf("%d%d", &x, &y), add(x, y), add(y, x);
  for (i = 1; i <= n; i++) {
    if (dfs(i))
      printf("%d ", i);
  }
  puts("");
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define N 3050
int n, a[N], head[N], to[N << 1], nxt[N << 1], cnt;
int mn[N], se[N], is[N];
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
}
int dfs(int x) {
  int i;
  for (i = head[x]; i; i = nxt[i])
    if (a[to[i]] < a[x] && !dfs(to[i]))
      return 1;
  return 0;
}
int main() {
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int x, y;
  for (i = 1; i < n; i++)
    scanf("%d%d", &x, &y), add(x, y), add(y, x);
  for (i = 1; i <= n; i++) {
    if (dfs(i))
      printf("%d ", i);
  }
  puts("");
}
Problem p03813 (Python)

Buggy submission (status: Runtime Error):
print("ARC" if int(input) > 1200 else "ABC")


Fixed submission:
print("ARC" if int(input()) >= 1200 else "ABC")
Problem p03813 (Python)

Buggy submission (status: Runtime Error):
x = input()
if x < 1200:
    print("ABC")
else:
    print("ARC")


Fixed submission:
x = int(input())
if x < 1200:
    print("ABC")
else:
    print("ARC")
Problem p03813 (Python)

Buggy submission (status: Runtime Error):
x = input()


if x < 1200:
    print("ABC")

else:
    print("ARC")


Fixed submission:
x = int(input())


if x < 1200:
    print("ABC")

else:
    print("ARC")
Problem p03814 (Python)

Buggy submission (status: Time Limit Exceeded):
def getZ_index(input_list):
    for index, i in enumerate(reversed(input_list)):
        if i == "Z":
            return len(input_list) - index
    return 0


lis = list(input())
max_num = 0
for index, i in enumerate(lis):
    if i == "A":
        if "Z" in lis[index : len(lis)]:
            max_num = max(max_num, getZ_index(lis[index : len(lis)]))
print(max_num)


Fixed submission:
s = list(input())
print(len(s) - list(reversed(s)).index("Z") - s.index("A"))
Problem p03814 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
ans = 0
for i in range(len(s)):
    if s[i] != "A":
        continue
    for j in range(i + 1, len(s)):
        if s[j] == "Z":
            ans = max(ans, j + 1 - i)
print(ans)


Fixed submission:
s = input()
n = len(s)
a = float("INF")
z = 0
for i in range(n):
    if s[i] == "A":
        a = min(a, i)
    elif s[i] == "Z":
        z = max(z, i)
print(z + 1 - a)
Problem p03814 (C++)

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

int main() {
  string s;
  cin >> s;
  int max = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s.at(i) == 'A') {
      for (int j = i + 1; j < s.length(); j++) {
        if (s.at(j) == 'Z' && max < j - i + 1)
          max = j - i + 1;
      }
    }
  }
  cout << max << endl;
}


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

int main() {
  string s;
  cin >> s;
  int max = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s.at(i) == 'A') {
      for (int j = i + 1; j < s.length(); j++) {
        if (s.at(j) == 'Z' && max < j - i + 1)
          max = j - i + 1;
      }
      break;
    }
  }
  cout << max << endl;
}
Problem p03814 (C++)

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

using namespace std;

int main(void) {
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    for (int j = s.size() - 1; j >= 0; j--) {
      if (s[i] == 'A' && s[j] == 'Z') {
        cout << j - i + 1;
        return 0;
      }
    }
  }
  return 0;
}

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

using namespace std;

int main(void) {
  string s;
  cin >> s;
  int i;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == 'A') {
      break;
    }
  }
  for (int j = s.size() - 1; j >= 0; j--) {
    if (s[j] == 'Z') {
      cout << j - i + 1;
      return 0;
    }
  }
  return 0;
}
Problem p03814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, a, b = 0, i = 1, j, saidai = 0;
  string s;
  cin >> s;
  a = s.size();
  for (i = 0; i < a; i++) {
    for (j = 0; j < a - j - 1; j++) {
      if (s.at(i) == 'A' && s.at(a - j - 1) == 'Z') {
        cout << a - j - i << endl;
        return 0;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, a, b = 0, i = 1, j, saidai = 0;
  string s;
  cin >> s;
  a = s.size();
  for (i = 0; i < a; i++) {
    if (s.at(i) != 'A') {
      continue;
    }
    for (j = 0; saidai < a - j - 1; j++) {
      if (s.at(i) == 'A' && s.at(a - j - 1) == 'Z') {
        cout << a - j - i << endl;
        return 0;
      }
    }
  }
  return 0;
}
Problem p03814 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j;
  string s;
  cin >> s;
  i = 0, j = s.size();
  while (s.at(i) != 'A')
    i++;
  while (s.at(j) != 'Z')
    j--;
  cout << j - i + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j;
  string s;
  cin >> s;
  i = 0, j = s.size() - 1;
  while (s.at(i) != 'A')
    i++;
  while (s.at(j) != 'Z')
    j--;
  cout << j - i + 1 << endl;
}
Problem p03814 (C++)

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

int main() {
  int a, b;
  string s;
  cin >> s;
  for (int i = 0; i != s.size(); i++) {
    if (s[i] == 'A') {
      a = i;
      break;
    }
  }
  for (int i = s.size() - 1; i != 0; i++) {
    if (s[i] == 'Z') {
      b = i;
      break;
    }
  }
  cout << (b - a) + 1 << endl;
  return 0;
}

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

int main() {
  int a, b;
  string s;
  cin >> s;
  for (int i = 0; i != s.size(); i++) {
    if (s[i] == 'A') {
      a = i;
      break;
    }
  }
  for (int i = s.size() - 1; i != 0; i--) {
    if (s[i] == 'Z') {
      b = i;
      break;
    }
  }
  cout << (b - a) + 1 << endl;
  return 0;
}
Problem p03814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF (1ll << 60)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
using vl = vector<ll>;
using vvl = vector<vl>;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;
using ll = long long;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  REP(i, s.length()) {
    if (s[i] == 'A') {
      for (int j = i + 1; j < s.length(); j++) {
        if (s[j] == 'Z') {
          ans = max(ans, j - i + 1);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF (1ll << 60)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
using vl = vector<ll>;
using vvl = vector<vl>;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;
using ll = long long;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  REP(i, s.length() - ans) {
    if (s[i] == 'A') {
      for (int j = i + 1; j < s.length(); j++) {
        if (s[j] == 'Z') {
          ans = max(ans, j - i + 1);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03814 (C++)

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  string s;
  cin >> s;
  int m;
  int l;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'A') {
      m = i;
      i = s.length();
    }
  }
  for (int i = s.length() - 1; i >= m + 1; i--) {
    if (s[i] == 'Z') {
      l = i;
      i = s.length();
    }
  }
  cout << l - m + 1;
}

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  string s;
  cin >> s;
  int m;
  int l;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'A') {
      m = i;
      i = s.length();
    }
  }
  for (int i = s.length() - 1; i >= m + 1; i--) {
    if (s[i] == 'Z') {
      l = i;
      i = -1;
    }
  }
  cout << l - m + 1;
}
Problem p03814 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
ans = 0
for i in range(len(s) - 1):
    for j in range(i + 1, len(s)):
        if s[i] == "A" and s[j] == "Z":
            ans = max(ans, j - i + 1)
print(ans)


Fixed submission:
s = input()
ansi = 0
ansj = 0
for i in range(len(s)):
    if s[i] == "A":
        ansi = i
        break
for j in range(len(s) - 1, -1, -1):
    if s[j] == "Z":
        ansj = j
        break
print(j - i + 1)