Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):
// INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
// DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
//-----------------------------------------
#define dcml(n) fixed << setprecision(n)
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
ll Factorial(ll n) {
  ll m = 1;
  while (n >= 1)
    m *= n--;
  return m;
}
ll modPow(ll x, ll n, ll mod = MOD) {
  return (!n       ? 1
          : !n % 2 ? modPow(x * x, n / 2, mod) % mod
                   : x % mod * modPow(x, n - 1, mod) % mod);
}
int main(void) {
  ll N;
  cin >> N;
  ll ans = INF;
  ll A;
  vi v;
  REP(i, N) {
    cin >> A;
    for (auto it = v.begin(); 1;) {
      if (it == v.end()) {
        v.push_back(A);
        break;
      }
      if (*it % A == 0) {
        it = v.erase(it);
      } else {
        ans = min(ans, GCD(*it, A));
        ++it;
      }
      ans = min(ans, A);
    }
  }
  cout << ans << "\n";
}

Fixed submission:
// INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
// DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
//-----------------------------------------
#define dcml(n) fixed << setprecision(n)
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
ll Factorial(ll n) {
  ll m = 1;
  while (n >= 1)
    m *= n--;
  return m;
}
ll modPow(ll x, ll n, ll mod = MOD) {
  return (!n       ? 1
          : !n % 2 ? modPow(x * x, n / 2, mod) % mod
                   : x % mod * modPow(x, n - 1, mod) % mod);
}
int main(void) {
  ll N;
  cin >> N;
  vi v(N);
  REP(i, N) cin >> v[i];
  REPS(i, N - 1) { v[i] = GCD(v[i], v[i - 1]); }
  cout << v[N - 1] << "\n";
}