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

Buggy submission (status: Runtime Error):
//
// Created by yuu on 2019-06-30.
//

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

  return gcd(b, a % b);
}

// int lcm(int a, int b) {
//     return a * b / gcd(a, b);
// }
//
//
// long long c_lcm(long a, long b){
//     long long x = a *b;
//     long long tmp, r;
//     if(a<b){
//         tmp = a;
//         a = b;
//         b = tmp;
//     }
//
//     r = a % b;
//     while(r!=0){
//         a = b;
//         b = r;
//         r = a % b;
//     }
//
//     return x/b;
// }

int A[10010];
int main(void) {
  int N;
  cin >> N;

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  //    sort(A,A+N);

  //    int min = A[0];
  //    if(min>lcm(A[0], A[1])){
  //        min = lcm(A[0], A[1]);
  //    }
  int temp = A[0];
  for (int i = 1; i < N; ++i) {
    temp = gcd(temp, A[i]);
    //        cout<<A[i]<<endl;
    //        if(min>temp && temp > 0){
    //            min =  temp;
    //        }
  }

  cout << temp << endl;
  return 0;
}

Fixed submission:
//
// Created by yuu on 2019-06-30.
//

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

  return gcd(b, a % b);
}

// int lcm(int a, int b) {
//     return a * b / gcd(a, b);
// }
//
//
// long long c_lcm(long a, long b){
//     long long x = a *b;
//     long long tmp, r;
//     if(a<b){
//         tmp = a;
//         a = b;
//         b = tmp;
//     }
//
//     r = a % b;
//     while(r!=0){
//         a = b;
//         b = r;
//         r = a % b;
//     }
//
//     return x/b;
// }

int A[100010];
int main(void) {
  int N;
  cin >> N;

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  //    sort(A,A+N);

  //    int min = A[0];
  //    if(min>lcm(A[0], A[1])){
  //        min = lcm(A[0], A[1]);
  //    }
  int temp = A[0];
  for (int i = 1; i < N; ++i) {
    temp = gcd(temp, A[i]);
    //        cout<<A[i]<<endl;
    //        if(min>temp && temp > 0){
    //            min =  temp;
    //        }
  }

  cout << temp << endl;
  return 0;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> monsters(n);
  for (int i = 0; i < monsters.size(); i++) {
    cin >> monsters[i];
  }
  int min = monsters[0];

  for (int pow : monsters) {
    min = __gcd(min, pow);
  }
  cout << min << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> monsters(n);
  for (int i = 0; i < monsters.size(); i++) {
    cin >> monsters[i];
  }
  int min = monsters[0];

  for (int i = 0; i < monsters.size(); i++) {
    min = __gcd(min, monsters[i]);
  }
  cout << min << endl;
}
Problem p03127 (C++)

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

#define REP(i, num) for (int i = 0; i < (num); ++i)
#define ALL(c) c.begin(), c.end()
#define PRINTALL(c)                                                            \
  for (auto &x : c) {                                                          \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;

using namespace std;
using ll = long long;

template <typename T = int> T in() {
  T x;
  cin >> x;
  return (x);
}

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

  int N = in();
  vector<int> s(N);
  int mp = 1e9 + 7;
  REP(i, N) { s[i] = in(); }
  do {
    auto res = min_element(
        ALL(s), [](int l, int r) { return (l < r && l != 0 && r != 0); });
    mp = *res;
    int index = distance(s.begin(), res);
    REP(i, N) {
      if (i != index)
        s[i] %= mp;
    }
  } while (count(ALL(s), 0) < N - 1);

  cout << mp << endl;
  return 0;
}


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

#define REP(i, num) for (int i = 0; i < (num); ++i)
#define ALL(c) c.begin(), c.end()
#define PRINTALL(c)                                                            \
  for (auto &x : c) {                                                          \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;

using namespace std;
using ll = long long;

template <typename T = int> T in() {
  T x;
  cin >> x;
  return (x);
}

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

  int N = in();
  vector<int> s(N);
  int mp = 1e9 + 7;
  REP(i, N) { s[i] = in(); }
  do {
    int index;
    REP(i, N) {
      if (s[i] > 0 && mp > s[i]) {
        mp = s[i];
        index = i;
      }
    }
    REP(i, N) {
      if (i != index)
        s[i] %= mp;
    }
  } while (count(ALL(s), 0) < N - 1);

  cout << mp << endl;
  return 0;
}
Problem p03127 (C++)

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

int gcd(int a, int b) {
  while (a != b) {
    if (a > b)
      a = a - b;
    if (a < b)
      b = b - a;
  }
  return a;
}

int main() {

  int N;
  cin >> N;
  vector<int> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
  }
  for (int i = 1; i < N; i++) {
    S.at(i) = gcd(S.at(i - 1), S.at(i));
  }
  cout << S.at(N - 1) << endl;
}

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

int gcd(int a, int b) {
  while (a != b) {
    if (a > b) {
      if (a % b == 0)
        return b;
      else
        a = a % b;
    }
    if (a < b) {
      if (b % a == 0)
        return a;
      else
        b = b % a;
    }
  }
  return a;
}

int main() {

  int N;
  cin >> N;
  vector<int> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
  }
  for (int i = 1; i < N; i++) {
    S.at(i) = gcd(S.at(i - 1), S.at(i));
  }
  cout << S.at(N - 1) << endl;
}
Problem p03127 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long

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

int main() {
  int n;
  cin >> n;
  vector<ll> a;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = a[0];
  for (int i = 0; i < n; i++) {
    ans = gcd(ans, a[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = a[0];
  for (int i = 0; i < n; i++) {
    ans = gcd(ans, a[i]);
  }
  cout << ans << endl;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SORT(i) sort((i).begin(), (i).end())
#define INF 2000000000
constexpr ll mod = 1000000007;

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

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  SORT(A);
  ll ans = A[0];
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      ans = min(ans, gcd(A[i], A[j]));
    }
  }
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SORT(i) sort((i).begin(), (i).end())
#define INF 2000000000
constexpr ll mod = 1000000007;

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

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  ll ans = INF;
  for (int i = 0; i < N - 1; ++i) {
    A[i + 1] = gcd(A[i], A[i + 1]);
    ans = min(ans, A[i + 1]);
  }
  cout << ans << "\n";

  return 0;
}
Problem p03127 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
  if (x = 0 || y == 0)
    return max(x, y);
  else {
    if (x < y)
      return gcd(x, y % x);
    else if (y < x)
      return gcd(y, x % y);
    else
      return x;
  }
}
int main() {
  int N;
  cin >> N;
  int Min;
  cin >> Min;
  for (int i = 0; i < N - 1; i++) {
    int a;
    cin >> a;
    Min = gcd(a, Min);
  }
  cout << Min << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
  if (x == 0 || y == 0)
    return max(x, y);
  else {
    if (x < y)
      return gcd(x, y % x);
    else if (y < x)
      return gcd(y, x % y);
    else
      return x;
  }
}
int main() {
  int N;
  cin >> N;
  int Min;
  cin >> Min;
  for (int i = 0; i < N - 1; i++) {
    int a;
    cin >> a;
    Min = gcd(a, Min);
  }
  cout << Min << endl;
  return 0;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main(void) {

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

  long gcdmin = 10000000000;

  for (int i = 0; i < n - 1; i++) {
    for (int t = i + 1; t < n; t++) {
      long newvalue = euclidean_gcd(a[i], a[t]);
      gcdmin = min(gcdmin, newvalue);
    }
  }

  cout << gcdmin << endl;

  return 0;
}


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

using namespace std;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main(void) {

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

  long gcdmin = 10000000000;

  gcdmin = euclidean_gcd(a[0], a[1]);

  for (int i = 2; i < n; i++) {
    gcdmin = euclidean_gcd(gcdmin, a[i]);
  }

  cout << gcdmin << endl;

  return 0;
}
Problem p03127 (C++)

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

using namespace std;

using ll = long long;

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

  int res = INT_MAX;
  priority_queue<int> que;
  for (int i = 0, a; i < n; ++i) {
    cin >> a;
    que.push(a);
    res = min(res, a);
  }

  while (1 < que.size()) {
    int cur = que.top();
    que.pop();
    res = min(res, cur);
    if (1 <= cur - que.top())
      que.push(cur - que.top());
  }

  return min(res, que.top());
}

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

  cout << Solve() << endl;

  return 0;
}


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

using namespace std;

using ll = long long;

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

  int res = INT_MAX;
  priority_queue<int> que;
  for (int i = 0, a; i < n; ++i) {
    cin >> a;
    que.push(a);
    res = min(res, a);
  }

  while (1 < que.size()) {
    int cur = que.top();
    que.pop();
    res = min(res, cur);
    int a = cur - (int)((cur - 1) / que.top()) * que.top();
    if (1 <= cur - que.top() && a < cur)
      que.push(a);
  }

  return min(res, que.top());
}

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

  cout << Solve() << endl;

  return 0;
}
Problem p03127 (C++)

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

using namespace std;

typedef long long ll;

ll gcd(ll m, ll n) {
  if (m == 0 || n == 0)
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N);
  vector<bool> used(N, false);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll ans = gcd(A[0], A[1]);
  used[0] = used[1] = true;
  for (int i = 2; i < N; i++) {
    if (A[i] % A[0] == 0 || A[i] % A[1] == 0)
      used[i] = true;
  }

  // remove Ai s.t. Ai % A[0] == 0 or Ai % A[1] == 0
  for (int i = 2; i < N; i++) {
    if (!used[i]) {
      ans = gcd(ans, A[i]);
      used[i] = true;
      for (int k = i + 1; k < N; k++) {
        if (used[k])
          continue;
        if (A[k] % A[i] == 0) {
          used[k] = true;
        }
      }
    }
  }
  cout << ans << endl;
}


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

using namespace std;

typedef long long ll;

ll gcd(ll m, ll n) {
  if (m == 0 || n == 0)
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N);
  vector<bool> used(N, false);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll ans = gcd(A[0], A[1]);
  used[0] = used[1] = true;
  for (int i = 2; i < N; i++) {
    if (A[i] % A[0] == 0 || A[i] % A[1] == 0)
      used[i] = true;
  }

  // remove Ai s.t. Ai % A[0] == 0 or Ai % A[1] == 0
  for (int i = 2; i < N; i++) {
    if (!used[i]) {
      ans = gcd(ans, A[i]);
      used[i] = true;
      for (int k = i + 1; k < N; k++) {
        if (A[k] % ans == 0) {
          used[k] = true;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long int gcd(long int m, long int n) {
  if ((0 == m) || (0 == n))
    return 0;

  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main() {
  int n;
  cin >> n;
  long int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int g = 1;
  if (a[0] == 1 || a[1] == 1) {

    cout << 1;
    return 0;
  }
  g = gcd(a[0], a[1]);

  if (n > 2) {
    for (int i = 2; i < n; ++i) {
      g = gcd(g, a[i]);
      if (g == 1) {
        break;
      }
    }
  }
  cout << g;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long int gcd(long int m, long int n) {
  if ((0 == m) || (0 == n))
    return 0;

  while (m != n) {
    if (m > n) {
      if (m % n == 0) {
        m = n;
      } else {
        m = m % n;
      }
    } else {
      if (n % m == 0) {
        n = m;
      } else {
        n = n % m;
      }
    }
  }
  return m;
}

int main() {
  int n;
  cin >> n;
  long int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int g = 1;
  if (a[0] == 1 || a[1] == 1) {

    cout << 1;
    return 0;
  }
  g = gcd(a[0], a[1]);

  if (n > 2) {
    for (int i = 2; i < n; ++i) {
      g = gcd(g, a[i]);
      if (g == 1) {
        break;
      }
    }
  }
  cout << g;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long GCD(long long A, long long B) {
  long long R = A % B;
  while (R != 0) {
    A = B;
    B = R;
    R = A % B;
  }
  return B;
}
int main(void) {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  long long MaxG = 1000000000;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++)
      MaxG = min(MaxG, GCD(A[j], A[i]));
  }
  cout << MaxG << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long GCD(long long A, long long B) {
  long long R = A % B;
  while (R != 0) {
    A = B;
    B = R;
    R = A % B;
  }
  return B;
}
int main(void) {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  long long MaxG = 1000000000;
  for (int i = 0; i < N - 1; i++)
    MaxG = min(MaxG, GCD(A[i + 1], A[i]));
  cout << MaxG << endl;
}
Problem p03127 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>
long long int GCD(long long int a, long long int b) {
  long long int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
using namespace std;

int main() {
  int n;
  long long int a[n + 1];
  for (int i = 0; i != n; i++) {
    cin >> a[i];
  }
  long long int x;
  x = a[0];
  for (int i = 1; i != n; i++) {
    x = GCD(x, a[i]);
  }
  cout << x << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
long long int GCD(long long int a, long long int b) {
  long long int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
using namespace std;

int main() {
  int n;
  cin >> n;
  long long int a[n + 1];
  for (int i = 0; i != n; i++) {
    cin >> a[i];
  }
  long long int x;
  x = a[0];
  for (int i = 1; i != n; i++) {
    x = GCD(x, a[i]);
  }
  cout << x << endl;
}
Problem p03127 (C++)

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

int main() {
  int N;
  int A[200];
  int Min = 1000000000;
  cin >> N;
  int beforemin = 12345;
  int aftermin = 56789;
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  while (true) {
    for (int i = 0; i < N; ++i)
      if ((A[i] != 0) && (A[i] < Min))
        Min = A[i];
    if (beforemin == Min)
      break;
    for (int i = 0; i < N; ++i)
      if (A[i] != Min)
        A[i] = A[i] % Min;
    beforemin = Min;
  }
  cout << Min << endl;
}

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

int main() {
  int N;
  int A[1000002];
  int Min = 1000000000;
  cin >> N;
  int beforemin = 12345;
  int aftermin = 56789;
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  while (true) {
    for (int i = 0; i < N; ++i)
      if ((A[i] != 0) && (A[i] < Min))
        Min = A[i];
    if (beforemin == Min)
      break;
    for (int i = 0; i < N; ++i)
      if (A[i] != Min)
        A[i] = A[i] % Min;
    beforemin = Min;
  }
  cout << Min << endl;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int yg(int a, int b) {
  int tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
  }
  return b;
}
int v[110000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    v[i + 1] = yg(v[i], v[i + 1]);
  }
  cout << v[n - 1] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int yg(int a, int b) {
  int tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int v[110000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    v[i + 1] = yg(v[i], v[i + 1]);
  }
  cout << v[n - 1] << endl;
}
Problem p03127 (C++)

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

using namespace std;

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

  vector<long long> a(n);

  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }

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

  vector<long long> r(105, 1);

  int count = 0;
  for (long long i = 1; i * i <= a.at(0); i++) {
    if (a.at(0) % i == 0) {
      r.at(2 * count) = i;
      r.at(2 * count + 1) = a.at(0) / i;
      count++;
    }
  }

  sort(r.begin(), r.end(), greater<long long>());

  int test = 1;
  long long cv;

  for (int i = 0; i < r.size(); i++) {
    if (test == 1) {
      for (int j = 1; j < n; j++) {
        if (a.at(j) % r.at(i) != 0) {
          test = 1;
          break;
        }
        if (j == (n - 1) && a.at(j) % r.at(i) == 0) {
          test = 0;
          cv = r.at(i);
        }
      }
    } else {
      break;
    }
  }

  cout << cv << endl;
}


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

using namespace std;

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

  vector<long long> a(n);

  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }

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

  vector<long long> r(10000, 1);

  int count = 0;
  for (long long i = 1; i * i <= a.at(0); i++) {
    if (a.at(0) % i == 0) {
      r.at(2 * count) = i;
      r.at(2 * count + 1) = a.at(0) / i;
      count++;
    }
  }

  sort(r.begin(), r.end(), greater<long long>());

  int test = 1;
  long long cv;

  for (int i = 0; i < r.size(); i++) {
    if (test == 1) {
      for (int j = 1; j < n; j++) {
        if (a.at(j) % r.at(i) != 0) {
          test = 1;
          break;
        }
        if (j == (n - 1) && a.at(j) % r.at(i) == 0) {
          test = 0;
          cv = r.at(i);
        }
      }
    } else {
      break;
    }
  }

  cout << cv << endl;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define NIL -1
using namespace std;
typedef long long ll;
typedef long long int lli;
static const int MOD = 10000007;
static const int MAX = 100005;

string s;
int N;

int gcd(int a, int b) {
  if (b > a)
    swap(a, b);
  int r = 1;
  while (r > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  cin >> N;
  vector<int> A(N, 0);
  rep(i, N) { cin >> A[i]; }
  int minv = A[0];
  rep(i, N) {
    for (int j = i + 1; j < N; j++) {
      int r = gcd(A[i], A[j]);
      if (minv > r)
        minv = r;
    }
  }
  cout << minv << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define NIL -1
using namespace std;
typedef long long ll;
typedef long long int lli;
static const int MOD = 10000007;
static const int MAX = 100005;

string s;
int N;

int gcd(int a, int b) {
  if (b > a)
    swap(a, b);
  int r = 1;
  while (r > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  cin >> N;
  vector<int> A(N, 0);
  rep(i, N) { cin >> A[i]; }
  int minv = A[0];
  rep(i, N - 1) { minv = min(gcd(minv, A[i + 1]), minv); }
  cout << minv << endl;
}
Problem p03127 (C++)

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

using namespace std;

typedef long long int lld;
typedef unsigned long long int llu;

#define rep(i, n) for (lld i = 0; i < n; i++)

lld N;
lld hp[10010];

int main() {
  cin >> N;
  rep(i, N) {
    scanf("%lld", &hp[i]);
    if (hp[i] == 1) {
      cout << 1 << endl;
      return 0;
    }
  }
  rep(i, N - 1) {
    lld big = hp[i];
    lld little = hp[i + 1];
    lld tmp;
    if (big < little) {
      tmp = big;
      big = little;
      little = tmp;
    }

    while (big % little != 0) {
      big %= little;
      tmp = big;
      big = little;
      little = tmp;
    }
    hp[i + 1] = little;
    if (hp[i + 1] == 1) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << hp[N - 1] << endl;
  return 0;
}


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

using namespace std;

typedef long long int lld;
typedef unsigned long long int llu;

#define rep(i, n) for (lld i = 0; i < n; i++)

lld N;
lld hp[100010];

int main() {
  cin >> N;
  rep(i, N) {
    scanf("%lld", &hp[i]);
    if (hp[i] == 1) {
      cout << 1 << endl;
      return 0;
    }
  }
  rep(i, N - 1) {
    lld big = hp[i];
    lld little = hp[i + 1];
    lld tmp;
    if (big < little) {
      tmp = big;
      big = little;
      little = tmp;
    }

    while (big % little != 0) {
      big %= little;
      tmp = big;
      big = little;
      little = tmp;
    }
    hp[i + 1] = little;
    if (hp[i + 1] == 1) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << hp[N - 1] << endl;
  return 0;
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  int N, ans = 100000100;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  while (A.size() > 1) {
    sort(A.begin(), A.end());
    if (A[A.size() - 1] % A[A.size() - 2] == 0)
      A.pop_back();
    else
      A[A.size() - 1] %= A[A.size() - 2];
  }

  cout << A[0] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  int N, ans = 100000100;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sort(A.begin(), A.end());
  for (int i = 0; i < A.size() - 1; i++) {
    if (A[i + 1] % A[i] == 0) {
      A.erase(A.begin() + i + 1);
      i--;
    } else
      A[i + 1] %= A[i];
  }
  sort(A.begin(), A.end());
  int s = 1;
  while (s != A.size()) {
    if (A[s - 1] == A[s])
      A.erase(A.begin() + s);
    else
      s++;
  }
  while (A.size() > 1) {
    sort(A.begin(), A.end());
    if (A[A.size() - 1] % A[A.size() - 2] == 0)
      A.pop_back();
    else
      A[A.size() - 1] %= A[A.size() - 2];
  }

  cout << A[0] << endl;

  return 0;
}
Problem p03127 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define reprl(i, n) for (ll i = n; i >= 0; i--)
#define fori(i, m, n) for (int i = m; i < (int)n; i++)
#define forl(i, m, n) for (ll i = m; i < (ll)n; i++)
#define all(v) v.begin(), v.end()
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
bool isLowerCase(char c) { return (c >= 'a' && c <= 'z'); }
const string yesno(bool ans) { return (ans ? "Yes" : "No"); }
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(all(a));
  int idx = 0;
  while (idx < n - 1) {
    fori(i, 1, n) { a[i] %= a[idx]; }
    sort(all(a));
    auto firstNonZero = upper_bound(all(a), 0);
    idx = firstNonZero - a.begin();
  }
  std::cout << a[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define reprl(i, n) for (ll i = n; i >= 0; i--)
#define fori(i, m, n) for (int i = m; i < (int)n; i++)
#define forl(i, m, n) for (ll i = m; i < (ll)n; i++)
#define all(v) v.begin(), v.end()
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
bool isLowerCase(char c) { return (c >= 'a' && c <= 'z'); }
const string yesno(bool ans) { return (ans ? "Yes" : "No"); }
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(all(a));
  int idx = 0;
  while (idx < n - 1) {
    fori(i, idx + 1, n) { a[i] %= a[idx]; }
    sort(all(a));
    auto firstNonZero = upper_bound(all(a), 0);
    idx = firstNonZero - a.begin();
  }
  std::cout << a[n - 1] << endl;
  return 0;
}
Problem p03127 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 3005
#define REP(i, n) for (int i = 0; i < n; i++)
long n;
long a[10000];
long ans, en;
long cou;
long gcd(long i, long j) {
  if (i < j) {
    int tmp = i;
    i = j;
    j = tmp;
  }

  /* ユークリッドの互除法 */
  long r = i % j;
  while (r != 0) {
    i = j;
    j = r;
    r = i % j;
  }
  return j;
}
int main() {
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  sort(a, a + n);
  ans = gcd(a[0], a[1]);
  for (int i = 2; i < n; i++) {
    ans = gcd(a[0], a[i]);
    if (ans == 1)
      break;
  }
  printf("%ld", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 3005
#define REP(i, n) for (int i = 0; i < n; i++)
long n;
long a[100000];
long ans, en;
long cou;
long gcd(long i, long j) {
  if (i < j) {
    int tmp = i;
    i = j;
    j = tmp;
  }

  /* ユークリッドの互除法 */
  long r = i % j;
  while (r != 0) {
    i = j;
    j = r;
    r = i % j;
  }
  return j;
}
int main() {
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  sort(a, a + n);
  ans = gcd(a[0], a[1]);
  for (int i = 2; i < n; i++) {
    ans = gcd(a[0], a[i]);
    if (ans == 1)
      break;
  }
  printf("%ld", ans);
}
Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <iostream>

using namespace std;

int GCD(int x, int y) {
  int gcd;
  for (int i = 1; i <= x && i <= y; i++) {
    if (x % i == 0 && y % i == 0)
      gcd = i;
  }
  return gcd;
}

int main() {
  int N;

  int ans;

  cin >> N;

  N -= 2;

  int x, y;

  cin >> x >> y;

  ans = GCD(x, y);

  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    ans = GCD(ans, A);
  }

  cout << ans;

  return 0;
}


Fixed submission:

#include <iostream>

using namespace std;

int GCD(int x, int y) {
  if (y == 0)
    return x;
  return GCD(y, x % y);
}

int main() {
  int N;

  int ans;

  cin >> N;

  N -= 2;

  int x, y;

  cin >> x >> y;

  ans = GCD(x, y);

  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    ans = GCD(ans, A);
  }

  cout << ans;

  return 0;
}
Problem p03127 (C++)

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

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int main() {
  while (1) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    vector<int> tairyoku(N);
    for (int i = 0; i < N; i++)
      cin >> tairyoku[N];
    int gcds = 0;
    for (int i = 0; i < N; i++)
      gcds = gcd(gcds, tairyoku[i]);
    cout << gcds << endl;
  }
}


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

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int main() {
  int N;
  cin >> N;
  vector<int> tairyoku(N);
  for (int i = 0; i < N; i++)
    cin >> tairyoku[i];
  int gcds = 0;
  for (int i = 0; i < N; i++)
    gcds = gcd(gcds, tairyoku[i]);
  cout << gcds << endl;
}
Problem p03127 (C++)

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

int N, temp;
std::vector<int> A;

int main() {
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> temp;
    A.push_back(temp);
  }
  std::sort(A.begin(), A.end());

  auto begin = A.begin();
  auto next = std::next(begin);

  while (next != A.end() && *begin != 1) {
    *next = *next % *begin;

    if (*next < *begin) {
      temp = *next;
      *next = *begin;
      *begin = temp;
    } else if (*next == 0) {
      next++;
    }
  }
  std::cout << *begin << std::endl;
}


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

int N, temp;
std::vector<int> A;

int main() {
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> temp;
    A.push_back(temp);
  }
  std::sort(A.begin(), A.end());

  auto begin = A.begin();
  auto next = std::next(begin);

  while (next != A.end() && *begin != 1) {
    *next = *next % *begin;

    if (*next < *begin && *next != 0) {
      temp = *next;
      *next = *begin;
      *begin = temp;
    } else if (*next == 0) {
      next++;
    }
  }
  std::cout << *begin << std::endl;
}
Problem p03127 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int c;
  c = a % b;
  if (c == 0)
    return b;
  a = b;
  b = c;
}
int main() {
  int n, a[111111], i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  int ans = a[0];
  for (i = 1; i < n; i++)
    ans = gcd(a[i], ans);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int c;
  while (1) {
    c = a % b;
    if (c == 0)
      return b;
    a = b;
    b = c;
  }
}
int main() {
  int n, a[111111], i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  int ans = a[0];
  for (i = 1; i < n; i++)
    ans = gcd(a[i], ans);
  cout << ans << endl;
}
Problem p03127 (C++)

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

int main() {
  int N, A[100000];
  int min;
  bool same_min = false;
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  while (!same_min) {
    min = *min_element(A, &A[N]);
    for (int i = 0; i < N; i++) {
      if (A[i] != min)
        break;
      else if (i == N - 1 && A[i] == min)
        same_min = true;
    }
    for (int i = 0; i < N; i++) {
      while (A[i] > min)
        A[i] -= min;
    }
  }
  printf("%d\n", min);
  return 0;
}

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

int main() {
  int N, A[100000];
  int min;
  bool same_min = false;
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  while (!same_min) {
    min = *min_element(A, &A[N]);
    for (int i = 0; i < N; i++) {
      if (A[i] != min)
        break;
      else if (i == N - 1 && A[i] == min)
        same_min = true;
    }
    for (int i = 0; i < N; i++) {
      if (A[i] > min) {
        A[i] = A[i] % min;
        if (A[i] == 0)
          A[i] = min;
      }
    }
  }
  printf("%d\n", min);
  return 0;
}
Problem p03127 (C++)

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

uint64_t gcd(uint64_t u, uint64_t v) {
  while (v != 0) {
    uint64_t r = u % v;
    u = v;
    v = r;
  }
  return u;
}

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int g = gcd(a[i], a[j]);
      a[i] = gcd(a[i], g);
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < n; ++i) {
    ans = min(ans, a[i]);
  }
  cout << ans << "\n";
}


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

uint64_t gcd(uint64_t u, uint64_t v) {
  while (v != 0) {
    uint64_t r = u % v;
    u = v;
    v = r;
  }
  return u;
}

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int ans = gcd(a[0], a[1]);
  for (int i = 1; i < n; ++i) {
    ans = gcd(ans, a[i]);
  }
  cout << ans << "\n";
}
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from math import gcd
from functools import reduce


def solve(string):
    n, *a = map(int, string.split())
    return str(reduce(gcd, a))


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))


Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from fractions import gcd
from functools import reduce


def solve(string):
    n, *a = map(int, string.split())
    return str(reduce(gcd, a))


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))
Problem p03127 (C++)

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

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

  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N);

  int enemy_min = A[0];
  int enemy_next = A[1];
  int i = 0;

  while (1) {
    A[i + 1] = A[i + 1] - A[i];

    if (A[i + 1] < A[i]) {
      int tmp;
      tmp = A[i];
      A[i] = A[i + 1];
      A[i + 1] = tmp;
    }

    if (A[i] < 1) {
      i++;
    }

    if (A[N - 2] == 0) {
      break;
    }
  }

  cout << A[N - 1] << endl;

  return 0;
}


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

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

  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N);

  int enemy_min = A[0];
  int enemy_next = A[1];
  int i = 0;

  while (1) {
    A[i + 1] = A[i + 1] % A[i];

    if (A[i + 1] < A[i]) {
      int tmp;
      tmp = A[i];
      A[i] = A[i + 1];
      A[i + 1] = tmp;
    }

    if (A[i] < 1) {
      i++;
    }

    if (A[N - 2] == 0) {
      break;
    }
  }

  cout << A[N - 1] << endl;

  return 0;
}
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

n = int(input())
A = list(map(int, input().split()))
ans = A[0]
for a in A[1:]:
    ans = gcd(ans, a)
print(ans)


Fixed submission:
from fractions import gcd

n = int(input())
A = list(map(int, input().split()))
ans = A[0]
for a in A[1:]:
    ans = gcd(ans, a)
print(ans)
Problem p03127 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2:
    k = a[1] % a[0]
    if k == 0:
        a.pop(1)
    else:
        a[1] = k
    a.sort()
    if a[0] == 1:
        break
print(a[0])


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2:
    k = a[1] % a[0]
    if k == 0:
        a.pop(1)
    else:
        a[1] = k
        a[0], a[1] = a[1], a[0]
print(a[0])
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

N = int(input())
A = sorted([int(n) for n in input().split()], reverse=True)

x = A[0]
for i in range(1, N):
    x = gcd(x, A[i])
print(x)


Fixed submission:
from fractions import gcd

N = int(input())
A = sorted([int(n) for n in input().split()], reverse=True)

x = A[0]
for i in range(1, N):
    x = gcd(x, A[i])
print(x)
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
import sys

readline = sys.stdin.buffer.readline


def sss(A, N):
    A.sort()
    if A[0] == 0:
        A.remove(0)
    for i in range(1, len(A)):
        A[i] = A[i] % A[0]
    if A[0] == sum(A):
        print(sum(A))
    else:
        return sss(A, N)


def main():
    N = int(readline())
    A = list(map(int, readline().split()))

    sss(A, N)


main()


Fixed submission:
import sys

readline = sys.stdin.buffer.readline


def sss(A, N):
    A.sort()
    while A[0] == 0:
        A.remove(0)
    for i in range(1, len(A)):
        A[i] = A[i] % A[0]
    if A[0] == sum(A):
        print(sum(A))
    else:
        return sss(A, N)


def main():
    N = int(readline())
    A = list(map(int, readline().split()))

    sss(A, N)


main()
Problem p03127 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-
N = map(int, input())
A = list(map(int, input().split()))

while len(A) >= 2:
    x = A.pop(A.index(max(A)))
    y = min(A)
    n = x % y
    if n == 0:
        pass
    else:
        A.append(n)

print(A[0])


Fixed submission:
# -*- coding: utf-8 -*-
N = map(int, input())
A = list(map(int, input().split()))

while len(A) >= 2:
    idx = A.index(min(A))
    x = A.pop(idx)
    B = list()
    for i in range(len(A)):
        if A[i] % x != 0:
            B.append(A[i] % x)
    B.append(x)
    A = B[:]

print(A[0])
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
from functools import reduce
from math import gcd

if __name__ == "__main__":
    n = int(input())
    a = [int(s) for s in input().split()]
    print(reduce(gcd, a))


Fixed submission:
from functools import reduce


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


if __name__ == "__main__":
    n = int(input())
    a = [int(s) for s in input().split()]
    print(reduce(gcd, a))
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

N = int(input())
A = [int(x) for x in input().split()]

ans = A[0]

for i in range(1, N):
    ans = gcd(ans, A[i])

print(ans)


Fixed submission:
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


N = int(input())
A = [int(x) for x in input().split()]

ans = A[0]

for i in range(1, N):
    ans = gcd(ans, A[i])

print(ans)
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()

ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()
A.sort()
ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)
Problem p03127 (Python)

Buggy submission (status: Runtime Error):
from functools import reduce
from functools import gcd

N = int(input())
A = [int(i) for i in input().split()]

print(reduce(gcd, A))


Fixed submission:
from functools import reduce
from fractions import gcd

N = int(input())
A = [int(i) for i in input().split()]

print(reduce(gcd, A))
Problem p03127 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

while True:
    if len(A) == 1:
        break
    max_i = A.index(max(A))
    min_i = A.index(min(A))
    life = A[max_i] % A[min_i]
    if life == 0:
        A.pop(max_i)
    else:
        A[max_i] = life

print(A[0])


Fixed submission:
N = int(input())
A = list(map(int, input().split()))

while True:
    if len(A) == 1:
        break
    pop_i = []
    A.sort()
    for i in range(1, len(A)):
        life = A[i] % A[0]
        if life == 1:
            print(life)
            exit()
        else:
            A[i] = life
    A = [n for n in A if n > 0]

print(A[0])
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
const ll N = 1e9 + 7;
const int n_max = 1e5 + 10;

const vi num = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(m);
  rep(i, m) {
    cin >> a.at(i);
    a.at(i)--;
  }
  vi dp(n + 1, 0);
  rep(i, n + 1) {
    rep(j, m) {
      if (i < num.at(a.at(j)))
        continue;
      dp.at(i) = max(dp.at(i), dp.at(i - num.at(a.at(j))) + 1);
    }
  }
  // rep(i,dp.size())cout << dp.at(i) << " ";
  // cout << endl;

  vi ans;
  sort(a.begin(), a.end(), greater<int>());
  int count = n;
  while (0 < count) {
    rep(j, m) {
      if (count < num.at(a.at(j)))
        continue;
      if (dp.at(count) == 1) {
        if (count != num.at(a.at(j)))
          continue;
      }
      if (dp.at(count) == dp.at(count - num.at(a.at(j))) + 1) {
        ans.pb(a.at(j) + 1);
        // cout << count << " " << a.at(j) + 1 << endl;
        count -= num.at(a.at(j));
        break;
      }
    }
  }

  rep(i, ans.size()) cout << ans.at(i);
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
const ll N = 1e9 + 7;
const int n_max = 1e5 + 10;

const vi num = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(m);
  rep(i, m) {
    cin >> a.at(i);
    a.at(i)--;
  }
  vi dp(n + 1, -INT_MAX);
  dp.at(0) = 0;
  rep(i, n + 1) {
    rep(j, m) {
      if (i < num.at(a.at(j)))
        continue;
      dp.at(i) = max(dp.at(i), dp.at(i - num.at(a.at(j))) + 1);
    }
  }
  // rep(i,dp.size())cout << dp.at(i) << " ";
  // cout << endl;

  vi ans;
  sort(a.begin(), a.end(), greater<int>());
  int count = n;
  while (0 < count) {
    rep(j, m) {
      if (count < num.at(a.at(j)))
        continue;
      if (dp.at(count) == 1) {
        if (count != num.at(a.at(j)))
          continue;
      }
      if (dp.at(count) == dp.at(count - num.at(a.at(j))) + 1) {
        ans.pb(a.at(j) + 1);
        // cout << count << " " << a.at(j) + 1 << endl;
        count -= num.at(a.at(j));
        break;
      }
    }
  }

  rep(i, ans.size()) cout << ans.at(i);
  cout << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e18;

int main() {
  ll n, m;
  ll d[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  cin >> n >> m;
  vector<pll> a(m);
  rep(i, m) {
    cin >> a[i].fi;
    a[i].se = d[a[i].fi - 1];
  }
  sort(a.rbegin(), a.rend());
  vector<ll> dp(n + 1, -1);
  dp[0] = 0;
  rep(i, n + 1) {
    if (dp[i] != -1) {
      rep(j, m) {
        if (i + a[i].se <= n) {
          dp[i + a[j].se] = max(dp[i + a[j].se], dp[i] + 1);
        }
      }
    }
  }
  ll match = n, remain = dp[n];
  string ans = "";
  while (match > 0) {
    rep(i, m) {
      if (match >= a[i].se && dp[match - a[i].se] == remain - 1) {
        match -= a[i].se;
        remain--;
        string c = to_string(a[i].fi);
        ans += c;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e18;

int main() {
  ll n, m;
  ll d[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  cin >> n >> m;
  vector<pll> a(m);
  rep(i, m) {
    cin >> a[i].fi;
    a[i].se = d[a[i].fi - 1];
  }
  sort(a.rbegin(), a.rend());
  vector<ll> dp(n + 1, -1);
  dp[0] = 0;
  rep(i, n + 1) {
    if (dp[i] != -1) {
      rep(j, m) {
        if (i + a[j].se <= n) {
          dp[i + a[j].se] = max(dp[i + a[j].se], dp[i] + 1);
        }
      }
    }
  }
  ll match = n, remain = dp[n];
  string ans = "";
  while (match > 0) {
    rep(i, m) {
      if (match >= a[i].se && dp[match - a[i].se] == remain - 1) {
        match -= a[i].se;
        remain--;
        string c = to_string(a[i].fi);
        ans += c;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}