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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a, b;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i])
      ans += a[i] - b[i];
  }
  cout << ans;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i])
      ans += a[i] - b[i];
  }
  cout << ans;
}
Problem p03060 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    N = int(input())
    V = list(map(int, input().split()))
    C = list(map(int, input().split()))
    maxi = 0
    for i in range(2**N):
        binary = list(map(int, format(i, "b").zfill(N)))
        ans = 0
        for j, k in enumerate(binary):
            if k == 1:
                ans += V[j] - C[j]
        if ans > maxi:
            maxi = ans

    print(maxi)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    V = list(map(int, input().split()))
    C = list(map(int, input().split()))
    K = [V[i] - C[i] for i in range(N)]
    ans = 0
    for k in K:
        if k > 0:
            ans += k
    print(ans)


if __name__ == "__main__":
    main()
Problem p03060 (C++)

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

int main() {

  int N;
  cin >> N;
  vector<int> V, C, S;
  rep(i, N) cin >> V.at(i);
  rep(i, N) cin >> C.at(i);
  int answer = 0;
  rep(i, N) S.at(i) = V.at(i) - C.at(i);
  rep(i, N) {
    if (S.at(i) > 0)
      answer += S.at(i);
  }
  cout << answer << endl;
}

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

int main() {

  int N;
  cin >> N;
  vector<int> V(30), C(30), S(30);
  rep(i, N) cin >> V.at(i);
  rep(i, N) cin >> C.at(i);
  int answer = 0;
  rep(i, N) S.at(i) = V.at(i) - C.at(i);
  rep(i, N) {
    if (S.at(i) > 0)
      answer += S.at(i);
  }
  cout << answer << endl;
}
Problem p03060 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
vs = list(map(int, input().split()))
cs = list(map(int, input().split()))

ret = -float("inf")
for i in range((2**n)):
    total_cost = 0
    total_value = 0
    j = 0
    while i > 0:
        if i & 1:
            total_cost += cs[j]
            total_value += vs[j]
        i >>= 1
        j += 1
    ret = max(ret, total_value - total_cost)
print(ret)


Fixed submission:
n = int(input())
vs = list(map(int, input().split()))
cs = list(map(int, input().split()))

# ret = - float('inf')
# for i in range((2 ** n)):
#     total_cost = 0
#     total_value = 0
#     j = 0
#     while i > 0:
#         if i & 1:
#             total_cost += cs[j]
#             total_value += vs[j]
#         i >>= 1
#         j += 1
#     ret = max(ret, total_value - total_cost)
# print(ret)

x = 0
y = 0
for i in range(n):
    if vs[i] > cs[i]:
        x += vs[i]
        y += cs[i]
print(x - y)
Problem p03060 (Python)

Buggy submission (status: Time Limit Exceeded):
def resolve():
    N = int(input())
    V = [int(i) for i in input().split()]
    C = [int(i) for i in input().split()]
    maxA = 0
    for i in range(2**N):
        X = 0
        Y = 0
        for j in range(N):
            if (i >> j) & 1:
                X += V[j]
                Y += C[j]
        maxA = max(maxA, X - Y)
    print(maxA)


resolve()


Fixed submission:
def resolve():
    N = int(input())
    V = [int(i) for i in input().split()]
    C = [int(i) for i in input().split()]
    ans = 0
    for i in range(N):
        if V[i] > C[i]:
            ans += V[i] - C[i]
    print(ans)


resolve()
Problem p03060 (Python)

Buggy submission (status: Runtime Error):
def sub():
    N, Vs, Cs = [e for e in input().split(" ")]
    N = int(N)
    Vs = [int(e) for e in Vs.split(" ")]
    Cs = [int(e) for e in Cs.split(" ")]
    ds = [v - c for v, c in zip(Vs, Cs)]
    ds = [d for d in ds if d > 0]
    print(sum(ds))


sub()


Fixed submission:
def sub():
    N, Vs, Cs = [input() for _ in range(3)]
    N = int(N)
    Vs = [int(e) for e in Vs.split(" ")]
    Cs = [int(e) for e in Cs.split(" ")]
    ds = [v - c for v, c in zip(Vs, Cs)]
    ds = [d for d in ds if d > 0]
    print(sum(ds))


sub()
Problem p03060 (Python)

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

ans = -100000000000
for num in range(2**N):
    value = cost = 0
    for i in range(N):
        if (num >> i) & 1 == 1:
            value += vlist[i]
            cost += clist[i]
    if value - cost > ans:
        ans = value - cost
print(ans)


Fixed submission:
N = int(input())
vlist = list(map(int, input().split()))
clist = list(map(int, input().split()))
ans = 0
for i in range(N):
    if vlist[i] - clist[i] > 0:
        ans += vlist[i] - clist[i]
print(ans)
Problem p03060 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
v = [int(item) for item in input().split()]
c = [int(item) for item in input().split()]

d = [[v[i], c[i]] for i in range(n)]
d.sort(key=lambda x: x[0] - x[1], reverse=True)

ans = 0
i = 0
while d[i][0] - d[i][1] > 0:
    ans += d[i][0] - d[i][1]
    i += 1

print(ans)


Fixed submission:
n = int(input())
v = [int(item) for item in input().split()]
c = [int(item) for item in input().split()]

d = [[v[i], c[i]] for i in range(n)]
d.sort(key=lambda x: x[0] - x[1], reverse=True)

ans = 0
i = 0
while i < n and d[i][0] - d[i][1] > 0:
    ans += d[i][0] - d[i][1]
    i += 1

print(ans)
Problem p03060 (Python)

Buggy submission (status: Time Limit Exceeded):
# coding: utf-8
import itertools

n = int(input())
v = list(map(int, input().split()))
c = list(map(int, input().split()))
d = sum(v) - sum(c)
maxd = d if d > 0 else 0
for m in range(1, n):
    a0 = [i for i in list(itertools.combinations(list(range(n)), m))]
    for a1 in a0:
        d = sum([v[j] - c[j] for j in a1])
        if maxd < d:
            maxd = d
print(maxd)


Fixed submission:
# coding: utf-8
import itertools

n = int(input())
v = list(map(int, input().split()))
c = list(map(int, input().split()))
d = sum([v[i] - c[i] for i in range(n) if v[i] - c[i] > 0])
print(d)
Problem p03060 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
v = list(map(int, input().split(" ")))
c = list(map(int, input().split(" ")))

ans = 0
for bit in range(1 << n):
    tmp = 0
    for i in range(n):
        if bit >> i & 1:
            tmp += v[i] - c[i]
    ans = max(ans, tmp)
print(ans)


Fixed submission:
n = int(input())
v = list(map(int, input().split(" ")))
c = list(map(int, input().split(" ")))

ans = 0
for i in range(n):
    ans += max(0, v[i] - c[i])
print(ans)
Problem p03061 (C++)

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

long long gcd(long long n1, long long n2) {
  if (n1 < n2)
    std::swap(n1, n2);

  long long m;
  do {
    m = n1 % n2;
    n1 = n2;
    n2 = m;
  } while (m != 0);

  return n1;
}

int main() {
  long long N;
  std::cin >> N;

  std::vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  std::vector<long long> L(N);
  L[0] = 0;
  L[1] = A[0];
  for (long long i = 2; i < N; i++) {
    L[i] = gcd(L[i - 1], A[i - 1]);
  }

  std::vector<long long> R(N);
  R[N - 1] = 0;
  R[N - 2] = A[N - 1];
  for (long long i = N - 3; i >= 0; i--) {
    R[i] = gcd(R[i + 1], A[i + 1]);
  }

  long long max_g = 0;
  for (long long i = 0; i < N; i++) {
    max_g = std::max(max_g, gcd(L[i], R[i]));
  }

  std::cout << max_g << std::endl;

  return 0;
}

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

long long gcd(long long n1, long long n2) {
  if (n1 < n2)
    std::swap(n1, n2);
  if (n2 == 0)
    return n1;

  long long m;
  do {
    m = n1 % n2;
    n1 = n2;
    n2 = m;
  } while (m != 0);

  return n1;
}

int main() {
  long long N;
  std::cin >> N;

  std::vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  std::vector<long long> L(N);
  L[0] = 0;
  L[1] = A[0];
  for (long long i = 2; i < N; i++) {
    L[i] = gcd(L[i - 1], A[i - 1]);
  }

  std::vector<long long> R(N);
  R[N - 1] = 0;
  R[N - 2] = A[N - 1];
  for (long long i = N - 3; i >= 0; i--) {
    R[i] = gcd(R[i + 1], A[i + 1]);
  }

  long long max_g = 0;
  for (long long i = 0; i < N; i++) {
    max_g = std::max(max_g, gcd(L[i], R[i]));
  }

  std::cout << max_g << std::endl;

  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
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;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  rep1(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  rep1(i, n - 1) l[i + 1] = __gcd(l[i], a[i]);
  for (int i = n; i >= 2; --i)
    r[i - 1] = __gcd(r[i], a[i]);
  int ans = 1;
  rep1(i, n) ans = max(ans, __gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
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;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  rep1(i, n) cin >> a[i];
  vector<int> l(n + 1), r(n + 1);
  rep1(i, n - 1) l[i + 1] = __gcd(l[i], a[i]);
  for (int i = n; i >= 2; --i)
    r[i - 1] = __gcd(r[i], a[i]);
  int ans = 1;
  rep1(i, n) ans = max(ans, __gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
ll gcd(ll a, ll b) {
  if (a * b == 0)
    return a + b;
  if (a < b)
    gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n + 2, 0);
  vector<int> L(n + 2, 0);
  vector<int> R(n + 2, 0);
  FOR(i, 1, n + 1) { cin >> A[i]; }
  L[0] = 0;
  R[n + 1] = 0;
  REP(i, n + 2) { L[i + 1] = gcd(L[i], A[i]); }
  for (int i = n + 1; i >= 0; i--) {
    R[i] = gcd(R[i + 1], A[i]);
  }
  int mm = -1;
  FOR(i, 1, n + 1) {
    if (mm < gcd(L[i], R[i + 1]))
      mm = gcd(L[i], R[i + 1]);
  }
  cout << mm << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
ll gcd(ll a, ll b) {
  if (a * b == 0)
    return a + b;
  if (a < b)
    gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n + 2, 0);
  vector<int> L(n + 2, 0);
  vector<int> R(n + 2, 0);
  FOR(i, 1, n + 1) { cin >> A[i]; }
  L[0] = 0;
  R[n + 1] = 0;
  REP(i, n + 1) { L[i + 1] = gcd(L[i], A[i]); }
  for (int i = n; i >= 0; i--) {
    R[i] = gcd(R[i + 1], A[i]);
  }
  int mm = -1;
  FOR(i, 1, n + 1) {
    if (mm < gcd(L[i], R[i + 1]))
      mm = gcd(L[i], R[i + 1]);
  }
  cout << mm << endl;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using ll = unsigned long long;
using namespace std;

// http://drken1215.hatenablog.com/entry/2019/04/27/224100_1

int n;
ll *A;

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

int main(void) {
  cin >> n;

  A = new ll[n];

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

  // あるA[i]が無い状態での最大公約数を求める
  // gcd(left[i], right[i])のうちで，もっとも大きい値が解

  ll *left, *right;
  // nが抜けてたときのために，+1
  left = new ll[n + 1];
  right = new ll[n + 1];

  // forward 前向きに入れていく
  left[0] = 0;
  for (int i = 0; i < n; ++i) {
    left[i + 1] = gcd(left[i], A[i]);
  }

  // backward 後ろ向きにいれていく
  right[n] = 0;
  for (int i = n - 1; i >= 0; ++i) {
    right[i] = gcd(right[i + 1], A[i]);
  }

  // 集計する
  int maxv = 0;

  for (int i = 0; i < n; ++i) {

    // index問題に弱すぎ...ちゃんと確認すること
    int l = left[i];
    int r = right[i + 1];
    int cand = gcd(l, r);

    if (maxv < cand) {
      maxv = cand;
    }
  }

  cout << maxv << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using ll = unsigned long long;
using namespace std;

// http://drken1215.hatenablog.com/entry/2019/04/27/224100_1

int n;
ll *A;

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

int main(void) {
  cin >> n;

  A = new ll[n];

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

  // あるA[i]が無い状態での最大公約数を求める
  // gcd(left[i], right[i])のうちで，もっとも大きい値が解

  ll *left, *right;
  // nが抜けてたときのために，+1
  left = new ll[n + 1];
  right = new ll[n + 1];

  // forward 前向きに入れていく
  left[0] = 0;
  for (int i = 0; i < n; ++i) {
    left[i + 1] = gcd(left[i], A[i]);
  }

  // backward 後ろ向きにいれていく
  right[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    right[i] = gcd(right[i + 1], A[i]);
  }

  // 集計する
  int maxv = 0;

  for (int i = 0; i < n; ++i) {

    // index問題に弱すぎ...ちゃんと確認すること
    int l = left[i];
    int r = right[i + 1];
    int cand = gcd(l, r);

    if (maxv < cand) {
      maxv = cand;
    }
  }

  cout << maxv << endl;

  return 0;
}
Problem p03061 (C++)

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

using namespace std;
typedef long long ll;

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

int main() {
  ll N;
  vector<ll> A(N);
  cin >> N;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 入力完了
  vector<ll> L(N), R(N);
  L[0] = A[0];
  R[N - 1] = A[N - 1]; // LとRのベース入力

  for (ll i = 0; i < N - 1; i++) {
    L[i + 1] = gcd(L[i], A[i + 1]);
  }
  for (ll j = N - 2; j >= 0; j++) {
    R[j] = gcd(R[j + 1], A[j]);
  } // 累積gcd定義完了

  ll max_gcd = 0;

  for (ll i = 1; i <= N - 2; i++) {
    ll g = gcd(L[i - 1], R[i + 1]);
    max_gcd = max(max_gcd, g);
  }

  cout << max(max_gcd, max(R[1], L[N - 2])) << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;

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

int main() {
  ll N;
  vector<ll> A(N);
  cin >> N;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 入力完了
  vector<ll> L(N), R(N);
  L[0] = A[0];
  R[N - 1] = A[N - 1]; // LとRのベース入力

  for (ll i = 0; i < N - 1; i++) {
    L[i + 1] = gcd(L[i], A[i + 1]);
  }
  for (ll j = N - 2; j >= 0; j--) {
    R[j] = gcd(R[j + 1], A[j]);
  } // 累積gcd定義完了

  ll max_gcd = 0;

  for (ll i = 1; i <= N - 2; i++) {
    ll g = gcd(L[i - 1], R[i + 1]);
    max_gcd = max(max_gcd, g);
  }

  cout << max(max_gcd, max(R[1], L[N - 2])) << endl;
  return 0;
}
Problem p03061 (Python)

Buggy submission (status: Time Limit Exceeded):
import fractions

N = int(input())
A = list(map(int, input().split()))
gcd_max = 0
for i in range(N):
    a_temp = A[:i]
    a_temp.extend(A[i + 1 :])
    gcd_temp = a_temp[0]
    for a in a_temp:
        gcd_temp = fractions.gcd(gcd_temp, a)
        if gcd_temp <= gcd_max:
            break
    else:
        gcd_max = gcd_temp
print(gcd_max)


Fixed submission:
import fractions

N = int(input())
A = list(map(int, input().split()))
L = [0] * N
R = [0] * N
for i in range(0, N - 1):
    L[i + 1] = fractions.gcd(L[i], A[i])
for i in range(N - 1, 0, -1):
    R[i - 1] = fractions.gcd(R[i], A[i])
print(max([fractions.gcd(L[i], R[i]) for i in range(N)]))
Problem p03061 (Python)

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

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

cum_left = [A[0]]
cum_right = [A[N - 1]]
for i in range(1, N - 1):
    cum_left.append(gcd(cum_left[-1], A[i]))
    cum_right.append(gcd(cum_right[-1], A[N - 1 - i]))

ans = max(cum_left[N - 2], cum_right[N - 2])
for i in range(1, N - 1):
    ans = max(ans, gcd(cum_left[i - 1], cum_right[N - 2 - i]))
print(ans)


Fixed submission:
from fractions import gcd

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

cum_left = [A[0]]
cum_right = [A[N - 1]]
for i in range(1, N - 1):
    cum_left.append(gcd(cum_left[-1], A[i]))
    cum_right.append(gcd(cum_right[-1], A[N - 1 - i]))

ans = max(cum_left[N - 2], cum_right[N - 2])
for i in range(1, N - 1):
    ans = max(ans, gcd(cum_left[i - 1], cum_right[N - 2 - i]))
print(ans)
Problem p03061 (C++)

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

int req(vector<int> A, int N) {
  int wrongB = 0;
  int wrongC = 0;
  vector<int> B(N), C(N);
  B[0] = A[0];
  C[1] = A[1];
  for (int i = 1; i < N; i++) {
    B[i] = A[i] % A[0];
    if (B[i] == 0) {
      B[i] = A[0];
      continue;
    }
    wrongB++;
  }
  for (int i = 0; i < N; i++) {
    if (i == 1) {
      continue;
    }
    C[i] = A[i] % A[1];
    if (C[i] == 0) {
      B[i] = A[1];
      continue;
    }
    wrongC++;
  }
  if (wrongC < 2) {
    return A[1];
  }
  if (wrongB < 2) {
    return A[0];
  }
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  return max(req(B, N), req(C, N));
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());

  cout << req(A, N) << endl;
}

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

int req(vector<int> A, int N) {
  int wrongB = 0;
  int wrongC = 0;
  vector<int> B(N), C(N);
  B[0] = A[0];
  C[1] = A[1];
  for (int i = 1; i < N; i++) {
    B[i] = A[i] % A[0];
    if (B[i] == 0) {
      B[i] = A[0];
      continue;
    }
    wrongB++;
  }
  for (int i = 0; i < N; i++) {
    if (i == 1) {
      continue;
    }
    C[i] = A[i] % A[1];
    if (C[i] == 0) {
      C[i] = A[1];
      continue;
    }
    wrongC++;
  }
  if (wrongC < 2) {
    return A[1];
  }
  if (wrongB < 2) {
    return A[0];
  }
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  return max(req(B, N), req(C, N));
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());

  cout << req(A, N) << endl;
}
Problem p03061 (Python)

Buggy submission (status: Time Limit Exceeded):
import fractions

N = int(input())
aLst = list(map(int, input().split()))

ansLst = []
for i in range(len(aLst)):
    ans = 0
    for j in range(len(aLst)):
        if i != j:
            ans = fractions.gcd(ans, aLst[j])
    ansLst.append(ans)
ansLst.sort(reverse=True)
print(ansLst[0])


Fixed submission:
import fractions

N = int(input())
aLst = list(map(int, input().split()))

lans = 0
llst = [0] * (N + 1)
rans = 0
rlst = [0] * (N + 1)

for i in range(N):
    lans = fractions.gcd(lans, aLst[i])
    llst[i + 1] = lans

for i in range(N - 1, -1, -1):
    rans = fractions.gcd(rans, aLst[i])
    rlst[i] = rans

ansMax = 0
for i in range(N):
    ansMax = max(ansMax, fractions.gcd(llst[i], rlst[i + 1]))

print(ansMax)
Problem p03061 (Python)

Buggy submission (status: Time Limit Exceeded):
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
if n == 2:
    print(max(a))
    exit()
x = [a[0], gcd(a[0], a[1])]
y = [gcd(a[-2], a[-1]), a[-1]]
for i in range(1, n - 2):
    x.append(gcd(x[i], a[i + 1]))
    y.insert(0, gcd(y[0], a[-i - 2]))
ans = max(x[-1], y[0])
for i in range(n - 2):
    ans = max(ans, gcd(x[i], y[i + 1]))
print(ans)


Fixed submission:
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
if n == 2:
    print(max(a))
    exit()
x = [a[0], gcd(a[0], a[1])]
for i in range(n - 3):
    x.append(gcd(x[i + 1], a[i + 2]))
ans = x[-1]
y = a[-1]
for i in range(1, n - 1):
    z = gcd(y, a[-i])
    ans = max(ans, gcd(x[-i - 1], z))
    y = z
print(max(ans, gcd(y, a[1])))
Problem p03061 (C++)

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

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

int main() {
  int ans = 0, N;
  cin >> N;
  vector<int> A(N), M(N), L(N), R(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    if (i == 0)
      L[i] = 0;
    L[i + 1] = gcd(L[i], A[i]);
  }
  for (int i = N - 1; i >= 0; i--) {
    R[N] = 0;
    R[i] = gcd(R[i + 1], A[i]);
    M[i] = gcd(L[i], R[i + 1]);
  }
  sort(M.begin(), M.end());
  cout << M[N - 1] << endl;
}

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

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

int main() {
  int ans = 0, N;
  cin >> N;
  vector<int> A(N), M(N), L(N + 1), R(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    if (i == 0)
      L[i] = 0;
    L[i + 1] = gcd(L[i], A[i]);
  }
  for (int i = N - 1; i >= 0; i--) {
    R[N] = 0;
    R[i] = gcd(R[i + 1], A[i]);
    M[i] = gcd(L[i], R[i + 1]);
  }
  sort(M.begin(), M.end());
  cout << M[N - 1] << endl;
}
Problem p03061 (C++)

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

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  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];
  }

  vector<ll> left(N, 0);
  vector<ll> right(N, 0);

  left[0] = A[0];
  right[N - 1] = A[N - 1];

  for (int i = 1; i <= N; i++) {
    left[i] = gcd(A[i], left[i - 1]);
  }

  for (int j = N - 2; j >= 0; j--) {
    right[j] = gcd(A[j], right[j + 1]);
  }

  ll max_cnt = 0;

  for (int i = 1; i < N - 1; i++) {
    max_cnt = max(max_cnt, gcd(left[i - 1], right[i + 1]));
  }

  cout << max(max_cnt, max(right[1], left[N - 2])) << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  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];
  }

  vector<ll> left(N, 0);
  vector<ll> right(N, 0);

  left[0] = A[0];
  right[N - 1] = A[N - 1];

  for (int i = 1; i < N; i++) {
    left[i] = gcd(A[i], left[i - 1]);
  }

  for (int j = N - 2; j >= 0; j--) {
    right[j] = gcd(A[j], right[j + 1]);
  }

  ll max_cnt = 0;

  for (int i = 1; i < N - 1; i++) {
    max_cnt = max(max_cnt, gcd(left[i - 1], right[i + 1]));
  }

  cout << max(max_cnt, max(right[1], left[N - 2])) << endl;

  return 0;
}
Problem p03061 (C++)

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

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

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

  vector<int> l(n), r(n);
  for (int i = 0; i < n; i++)
    l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 1; i >= 1; i--)
    r[i - 1] = gcd(r[i], a[i]);
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, gcd(l[i], r[i]));
  cout << ans << endl;
}

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

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

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

  vector<int> l(n), r(n);
  for (int i = 0; i < n - 1; i++)
    l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 1; i >= 1; i--)
    r[i - 1] = gcd(r[i], a[i]);
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, gcd(l[i], r[i]));
  cout << ans << endl;
}
Problem p03061 (C++)

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

using namespace std;

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

  vector<int> A(no_of_elements);
  for (int i = 1; i <= no_of_elements; i++)
    cin >> A[i];

  vector<int> prefix_gcd(no_of_elements + 1, 0);
  for (int i = 1; i <= no_of_elements; i++)
    prefix_gcd[i] = __gcd(prefix_gcd[i - 1], A[i]);

  vector<int> suffix_gcd(no_of_elements + 2, 0);
  for (int i = no_of_elements; i >= 1; i--)
    suffix_gcd[i] = __gcd(suffix_gcd[i + 1], A[i]);

  int best_gcd = 0;
  for (int i = 1; i <= no_of_elements; i++) {
    int gcd_without_this = __gcd(prefix_gcd[i - 1], suffix_gcd[i + 1]);

    best_gcd = max(best_gcd, gcd_without_this);
  }

  cout << best_gcd;

  return 0;
}


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

using namespace std;

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

  vector<int> A(no_of_elements + 1);
  for (int i = 1; i <= no_of_elements; i++)
    cin >> A[i];

  vector<int> prefix_gcd(no_of_elements + 1, 0);
  for (int i = 1; i <= no_of_elements; i++)
    prefix_gcd[i] = __gcd(prefix_gcd[i - 1], A[i]);

  vector<int> suffix_gcd(no_of_elements + 2, 0);
  for (int i = no_of_elements; i >= 1; i--)
    suffix_gcd[i] = __gcd(suffix_gcd[i + 1], A[i]);

  int best_gcd = 0;
  for (int i = 1; i <= no_of_elements; i++) {
    int gcd_without_this = __gcd(prefix_gcd[i - 1], suffix_gcd[i + 1]);

    best_gcd = max(best_gcd, gcd_without_this);
  }

  cout << best_gcd;

  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Grafo vector<vector<pair<int, int>>>
#define Container vector<vector<int>>
#define MAX 40321
#define INFINITO INT_MAX

using namespace std;

int A[MAX];
int WA[MAX];
int n, maior;

int mdc(int x, int y) {
  if (x == -1) {
    return y;
  }
  if (y == -1) {
    return x;
  }
  return __gcd(x, y);
}

void conquer(int i1, int j1, int i2, int j2) {
  int left = mdc(A[i1], WA[i1]);
  int right = mdc(A[i2], WA[i2]);
  for (int i = i1; i <= j1; i++) {
    WA[i] = mdc(WA[i], right);
  }
  for (int i = i2; i <= j2; i++) {
    WA[i] = mdc(WA[i], left);
  }
}

void divide(int l, int r) {
  if (l != r) {
    int m = (l + r) / 2;
    divide(l, m);
    divide(m + 1, r);
    conquer(l, m, m + 1, r);
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    WA[i] = -1;
  }

  divide(0, n - 1);

  maior = 1;
  for (int i = 0; i < n; i++) {
    maior = max(maior, WA[i]);
  }

  cout << maior << "\n";

  return 0;
}

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

using namespace std;

int A[MAX];
int WA[MAX];
int n, maior;

int mdc(int x, int y) {
  if (x == -1) {
    return y;
  }
  if (y == -1) {
    return x;
  }
  return __gcd(x, y);
}

void conquer(int i1, int j1, int i2, int j2) {
  int left = mdc(A[i1], WA[i1]);
  int right = mdc(A[i2], WA[i2]);
  for (int i = i1; i <= j1; i++) {
    WA[i] = mdc(WA[i], right);
  }
  for (int i = i2; i <= j2; i++) {
    WA[i] = mdc(WA[i], left);
  }
}

void divide(int l, int r) {
  if (l != r) {
    int m = (l + r) / 2;
    divide(l, m);
    divide(m + 1, r);
    conquer(l, m, m + 1, r);
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    WA[i] = -1;
  }

  divide(0, n - 1);

  maior = 1;
  for (int i = 0; i < n; i++) {
    maior = max(maior, WA[i]);
  }

  cout << maior << "\n";

  return 0;
}
Problem p03061 (Python)

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

n = int(input())
a = list(map(int, input().split()))

pre = [0]
for i in range(n - 1):
    pre.append(gcd(pre[-1], a[i]))

post = [0]
for i in range(n - 1):
    post.append(gcd(post[-1], a[n - 1 - i]))

res = 0
for i in range(n):
    res = max(gcd(pre[i], post[n - 1 - i]), res)

print(res)


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


n = int(input())
a = list(map(int, input().split()))

pre = [0]
for i in range(n - 1):
    pre.append(gcd(pre[-1], a[i]))

post = [0]
for i in range(n - 1):
    post.append(gcd(post[-1], a[n - 1 - i]))

res = 0
for i in range(n):
    res = max(gcd(pre[i], post[n - 1 - i]), res)

print(res)
Problem p03061 (C++)

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

ll gcd(ll a, ll b) {
  ll c;
  while (a > 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return c;
}
int main() {
  ll b, c, d, e, k, n, o = 1, p, q, x, y, z, max1 = 0, ans = 1;
  string s, t, u;
  cin >> n;
  ll a[n], r[n], l[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  l[0] = a[0];
  r[n - 1] = a[n - 1];
  for (ll i = 1; i < n; i++) {
    l[i] = gcd(l[i - 1], a[i]);
  }
  for (ll i = n - 2; i >= 0; i++) {
    r[i] = gcd(r[i + 1], a[i]);
  }
  max1 = r[1];
  for (ll i = 1; i < n - 1; i++) {
    max1 = max(gcd(l[i - 1], r[i + 1]), max1);
  }
  max1 = max(max1, l[n - 2]);
  cout << max1 << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
using ll = long long;

ll gcd(ll a, ll b) {
  ll c;
  while (a > 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return c;
}
int main() {
  ll b, c, d, e, k, n, o = 1, p, q, x, y, z, max1 = 0, ans = 1;
  string s, t, u;
  cin >> n;
  ll a[n], r[n], l[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  l[0] = a[0];
  r[n - 1] = a[n - 1];
  for (ll i = 1; i < n; i++) {
    l[i] = gcd(l[i - 1], a[i]);
  }
  for (ll i = n - 2; i >= 0; i--) {
    r[i] = gcd(r[i + 1], a[i]);
  }
  max1 = r[1];
  for (ll i = 1; i < n - 1; i++) {
    max1 = max(gcd(l[i - 1], r[i + 1]), max1);
  }
  max1 = max(max1, l[n - 2]);
  cout << max1 << endl;
}
Problem p03061 (C++)

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

ll gcd(ll x, ll y) {
  if (y == 0) {
    return x;
  }
  return gcd(y, x % y);
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }

  vector<ll> G(N);
  G[0] = A[0];
  vector<ll> R(N);
  vector<ll> L(N);

  L[0] = 0;
  R[N] = 0;

  rep(i, N) { L[i + 1] = gcd(L[i], A[i]); }

  for (int i = N - 1; i >= 0; --i) {
    R[i] = gcd(R[i + 1], A[i]);
  }

  ll ans = 0;

  rep(i, N) { ans = max(ans, gcd(L[i], R[i + 1])); }

  cout << ans << endl;

  return 0;
}

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

ll gcd(ll x, ll y) {
  if (y == 0) {
    return x;
  }
  return gcd(y, x % y);
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }

  vector<ll> R(N + 1);
  vector<ll> L(N + 1);

  L[0] = 0;
  R[N] = 0;

  rep(i, N) { L[i + 1] = gcd(L[i], A[i]); }

  for (int i = N - 1; i >= 0; --i) {
    R[i] = gcd(R[i + 1], A[i]);
  }

  ll ans = 0;

  rep(i, N) { ans = max(ans, gcd(L[i], R[i + 1])); }

  cout << ans << endl;

  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
int N;
using namespace std;
vector<int> A;
vector<int> B;
vector<int> C;

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

int main() {
  cin >> N;
  A.resize(N);
  B.resize(N);
  C.resize(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  B[0] = A[0];
  for (int i = 1; i < N; i++) {
    B[i] = gcd(B[i - 1], A[i]);
  }
  C[N - 1] = A[N - 1];
  int ans = max(C[1], B[N - 1]);
  for (int i = 1; i < N - 1; i++) {
    ans = max(ans, gcd(B[i - 1], C[i + 1]));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
int N;
using namespace std;
vector<int> A;
vector<int> B;
vector<int> C;

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

int main() {
  cin >> N;
  A.resize(N);
  B.resize(N);
  C.resize(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  B[0] = A[0];
  for (int i = 1; i < N; i++) {
    B[i] = gcd(B[i - 1], A[i]);
  }
  C[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    C[i] = gcd(A[i], C[i + 1]);
  }

  int ans = max(C[1], B[N - 2]);
  for (int i = 1; i < N - 1; i++) {
    ans = max(ans, gcd(B[i - 1], C[i + 1]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

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

using namespace std;

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), right_gcd(n), left_gcd(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  // gcd with binary search
  left_gcd[1] = 0;
  right_gcd[n] = 0;
  for (int i = 2; i <= n; i++) {
    right_gcd[n + 1 - i] = gcd(max(right_gcd[n + 2 - i], a[n + 1 - i]),
                               min(right_gcd[n + 2 - i], a[n + 1 - i]));
    left_gcd[i] =
        gcd(max(left_gcd[i - 1], a[i - 2]), min(left_gcd[i - 1], a[i - 2]));
  }

  // merge gcd
  int answer = 0;
  for (int i = 1; i <= n; i++) {
    int i_gcd =
        gcd(max(left_gcd[i], right_gcd[i]), min(left_gcd[i], right_gcd[i]));
    if (answer < i_gcd)
      answer = i_gcd;
  }
  cout << answer << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#define MAX_N 100100

using namespace std;

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1), right_gcd(n + 1), left_gcd(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  // gcd with binary search
  left_gcd[1] = 0;
  right_gcd[n] = 0;
  for (int i = 2; i <= n; i++) {
    right_gcd[n + 1 - i] = gcd(max(right_gcd[n + 2 - i], a[n + 1 - i]),
                               min(right_gcd[n + 2 - i], a[n + 1 - i]));
    left_gcd[i] =
        gcd(max(left_gcd[i - 1], a[i - 2]), min(left_gcd[i - 1], a[i - 2]));
  }

  // merge gcd
  int answer = 0;
  for (int i = 1; i <= n; i++) {
    int i_gcd =
        gcd(max(left_gcd[i], right_gcd[i]), min(left_gcd[i], right_gcd[i]));
    if (answer < i_gcd)
      answer = i_gcd;
  }
  cout << answer << endl;
}
Problem p03061 (C++)

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

long long gcd(long long a, long long b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  } else if (a == 1 || b == 1) {
    return 1;
  }

  long long q;
  bool pro = true;
  while (pro) {
    q = a % b;
    if (q = 0) {
      return min(a, b);
    } else {
      a = b;
      b = q;
      if (a % b == 0) {
        return q;
        pro = false;
      }
    }
  }
}

int main() {
  int N;
  cin >> N;
  if (N == 2) {
    int a, b;
    cin >> a >> b;
    cout << max(a, b) << endl;
  } else {
    vector<long long> an(N);
    for (int i = 0; i < N; i++) {
      cin >> an.at(i);
    }
    vector<long long> li(N);
    vector<long long> ri(N);
    li.at(0) = an.at(0);
    ri.at(N - 1) = an.at(N - 1);

    for (int i = 1; i < N; i++) {
      li.at(i) = gcd(li.at(i - 1), an.at(i));
    }
    for (int i = N - 2; i >= 0; i = i - 1) {
      ri.at(i) = gcd(ri.at(i + 1), an.at(i));
    }

    long long m = ri.at(1);
    for (int i = 1; i < N - 1; i++) {
      long long mi = gcd(ri.at(i + 1), li.at(i - 1));
      if (mi > m) {
        m = mi;
      }
    }
    if (li.at(N - 2) > m) {
      m = li.at(N - 2);
    }

    cout << m << endl;
  }
}


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

long long gcd(long long a, long long b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  } else if (a == 1 || b == 1) {
    return 1;
  }

  long long q;
  bool pro = true;
  while (pro) {
    q = a % b;
    if (q == 0) {
      return min(a, b);
    } else {
      a = b;
      b = q;
      if (a % b == 0) {
        return q;
        pro = false;
      }
    }
  }
}

int main() {
  int N;
  cin >> N;
  if (N == 2) {
    int a, b;
    cin >> a >> b;
    cout << max(a, b) << endl;
  } else {
    vector<long long> an(N);
    for (int i = 0; i < N; i++) {
      cin >> an.at(i);
    }
    vector<long long> li(N);
    vector<long long> ri(N);
    li.at(0) = an.at(0);
    ri.at(N - 1) = an.at(N - 1);

    for (int i = 1; i < N; i++) {
      li.at(i) = gcd(li.at(i - 1), an.at(i));
    }
    for (int i = N - 2; i >= 0; i = i - 1) {
      ri.at(i) = gcd(ri.at(i + 1), an.at(i));
    }

    long long m = ri.at(1);
    for (int i = 1; i < N - 1; i++) {
      long long mi = gcd(ri.at(i + 1), li.at(i - 1));
      if (mi > m) {
        m = mi;
      }
    }
    if (li.at(N - 2) > m) {
      m = li.at(N - 2);
    }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
// #include <bits/stdc++.h>
using namespace std;
/*inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}*/
typedef long long ll;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int a[1004], b[1004], k[10001];
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; i++)
      cin >> k[i];
    a[1] = 0, b[n] = 0;
    for (int i = 2; i <= n; i++)
      a[i] = gcd(a[i - 1], k[i - 1]);
    for (int i = n - 1; i >= 1; i--)
      b[i] = gcd(b[i + 1], k[i + 1]);
    int ans = -999;
    for (int i = 1; i <= n; i++)
      ans = max(ans, gcd(a[i], b[i]));
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
// #include <bits/stdc++.h>
using namespace std;
/*inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}*/
typedef long long ll;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int a[100004], b[100004], k[1000001];
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; i++)
      cin >> k[i];
    a[1] = 0, b[n] = 0;
    for (int i = 2; i <= n; i++)
      a[i] = gcd(a[i - 1], k[i - 1]);
    for (int i = n - 1; i >= 1; i--)
      b[i] = gcd(b[i + 1], k[i + 1]);
    int ans = -999;
    for (int i = 1; i <= n; i++)
      ans = max(ans, gcd(a[i], b[i]));
    cout << ans << endl;
  }
  return 0;
}
Problem p03061 (C++)

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

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

using namespace std;
using ll = long long;

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

ll gcd(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  if (b < 1)
    return a;

  while (ll r = (a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

  int N = in();
  vector<ll> A(N);
  REP(i, N) A[i] = in();
  ll m = 0;
  REP(i, N) {
    ll lm = 0, rm = 0;
    for (int k = 0; k < i; k++) {
      lm = gcd(A[k], lm);
    }
    for (int k = i + 1; k < N; k++) {
      rm = gcd(A[k], rm);
    }
    m = max(m, gcd(lm, rm));
  }

  cout << m << endl;
  return 0;
}

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

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

using namespace std;
using ll = long long;

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

ll gcd(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  if (b < 1)
    return a;

  while (ll r = (a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

  int N = in();
  vector<ll> A(N);
  REP(i, N) A[i] = in();
  ll m = 0;
  vector<ll> lm(N + 1), rm(N + 1);
  lm[0] = 0, rm[N] = 0;
  for (int i = 0; i < N; i++) {
    lm[i + 1] = gcd(A[i], lm[i]);
  }
  for (int i = N - 1; i >= 0; i--) {
    rm[i] = gcd(A[i], rm[i + 1]);
  }
  for (int i = 0; i < N; i++) {
    m = max(m, gcd(lm[i], rm[i + 1]));
  }

  cout << m << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <stack>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define print(s) cout << s << endl
using namespace std;
const int INF = 1e9;
typedef long long lint;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
// const lint MOD = 998244353;
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
char upper(char c) { return c - 0x20; }
char lower(char c) { return c + 0x20; }

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  rep(i, n - 1) l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 1; i >= 0; i--)
    r[i - 1] = gcd(r[i], a[i]);
  int ans = 1;
  rep(i, n) ans = max(ans, gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <stack>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define print(s) cout << s << endl
using namespace std;
const int INF = 1e9;
typedef long long lint;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
// const lint MOD = 998244353;
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
char upper(char c) { return c - 0x20; }
char lower(char c) { return c + 0x20; }

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  rep(i, n - 1) l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 1; i >= 1; --i)
    r[i - 1] = gcd(r[i], a[i]);
  int ans = 1;
  rep(i, n) ans = max(ans, gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

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

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

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

  vector<int> pre_gcd(n + 1), suf_gcd(n + 1);

  for (int i = 1; i <= n + 1; i++) {
    pre_gcd[i] = __gcd(pre_gcd[i - 1], a[i - 1]);
  }

  //    for(auto it : pre_gcd) {
  //        cout << it << " ";
  //    }
  //    cout << endl;

  for (int i = n; i >= 1; i--) {
    suf_gcd[i - 1] = __gcd(suf_gcd[i], a[i - 1]);
  }

  //    for(auto it : suf_gcd) {
  //        cout << it << " ";
  //    }
  //    cout << endl;

  int ans = 0;
  for (int i = 0; i + 1 < pre_gcd.size(); i++) {
    ans = max(ans, __gcd(pre_gcd[i], suf_gcd[i + 1]));
  }

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

/**
5
12 6 3 4 10
*/


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

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

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

  vector<int> pre_gcd(n + 1), suf_gcd(n + 1);

  for (int i = 1; i <= n; i++) {
    pre_gcd[i] = __gcd(pre_gcd[i - 1], a[i - 1]);
  }

  //    for(auto it : pre_gcd) {
  //        cout << it << " ";
  //    }
  //    cout << endl;

  for (int i = n; i >= 1; i--) {
    suf_gcd[i - 1] = __gcd(suf_gcd[i], a[i - 1]);
  }

  //    for(auto it : suf_gcd) {
  //        cout << it << " ";
  //    }
  //    cout << endl;

  int ans = 0;
  for (int i = 0; i + 1 < pre_gcd.size(); i++) {
    ans = max(ans, __gcd(pre_gcd[i], suf_gcd[i + 1]));
  }

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

/**
5
12 6 3 4 10
*/
Problem p03061 (C++)

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

using namespace std;

long long gcd(long long x, long long y) {
  long long a = min(x, y), b = max(x, y);
  if (b % a == 0)
    return a;

  return (gcd(a, b % a));
}

int main() {
  long long n, *a;
  cin >> n;
  a = new long long[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  long long *r = new long long[n], *l = new long long[n];

  l[0] = a[0];
  r[n - 1] = a[n - 1];

  long long ans = r[1];

  for (long long i = 1; i < n - 1; i++) {
    long long x = gcd(l[i - 1], r[i + 1]);
    ans = max(ans, x);
  }

  ans = max(l[n - 2], ans);

  cout << ans << endl;

  return 0;
}

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

using namespace std;

long long gcd(long long x, long long y) {
  long long a = min(x, y), b = max(x, y);
  if (b % a == 0)
    return a;

  return (gcd(a, b % a));
}

int main() {
  long long n, *a;
  cin >> n;
  a = new long long[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  long long *r = new long long[n], *l = new long long[n];

  l[0] = a[0];
  r[n - 1] = a[n - 1];

  for (int i = 1; i < n; i++) {
    l[i] = gcd(l[i - 1], a[i]);
    r[n - i - 1] = gcd(r[n - i], a[n - i - 1]);
  }

  long long ans = r[1];

  for (long long i = 1; i < n - 1; i++) {
    long long x = gcd(l[i - 1], r[i + 1]);
    ans = max(ans, x);
  }

  ans = max(l[n - 2], ans);

  cout << ans << endl;

  return 0;
}
Problem p03061 (C++)

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

using namespace std;
struct CinInit {
  CinInit() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} CinInit;

int gcd(const int a, const int b) {
  if (a < b)
    return gcd(b, a);
  else if (b == 0)
    return a;
  else if (b == 1)
    return 1;
  else
    return gcd(b, a - b);
}

int main() {
  int n, cnt = 0;
  cin >> n;
  vector<int> a(n), l(n), r(n);
  l[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i < n - 1)
      l[i + 1] = gcd(l[i], a[i]);
  }
  r[n - 1] = 0;
  for (int i = n - 1; i >= 1; i--) {
    r[i - 1] = gcd(a[i], r[i]);
  }

  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans = max(gcd(l[i], r[i]), ans);
  }

  cout << ans << "\n";

  return 0;
}


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

using namespace std;
struct CinInit {
  CinInit() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} CinInit;

int gcd(const int a, const int b) {
  if (a < b)
    return gcd(b, a);
  else if (b == 0)
    return a;
  else if (b == 1)
    return 1;
  else
    return gcd(b, a % b);
}

int main() {
  int n, cnt = 0;
  cin >> n;
  vector<int> a(n), l(n), r(n);
  l[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i < n - 1)
      l[i + 1] = gcd(l[i], a[i]);
  }
  r[n - 1] = 0;
  for (int i = n - 1; i >= 1; i--) {
    r[i - 1] = gcd(a[i], r[i]);
  }

  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans = max(gcd(l[i], r[i]), ans);
  }

  cout << ans << "\n";

  return 0;
}