Problem p03464 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long
#define ll long long
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;

using pi = pair<int, int>;
using vi = vc<int>;

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  reverse(A, A + N);
  if (A[0] != 2) {
    cout << -1 << endl;
    return 0;
  }
  int ma = 3, mi = 2;
  rep(i, N - 1) {
    bool can = false;
    rng(j, mi, ma + 1) {
      if (j % A[i + 1] == 0)
        can = true;
    }
    if (!can) {
      cout << -1 << endl;
      return 0;
    } else {
      ma = ma / A[i + 1] * A[i + 1] + A[i + 1] - 1;
      mi = (mi + A[i + 1] - 1) / A[i + 1] * A[i + 1];
    }
  }
  cout << mi << " " << ma << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long
#define ll long long
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;

using pi = pair<int, int>;
using vi = vc<int>;

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  reverse(A, A + N);
  if (A[0] != 2) {
    cout << -1 << endl;
    return 0;
  }
  int ma = 3, mi = 2;
  rep(i, N - 1) {
    if (ma / A[i + 1] - mi / A[i + 1] <= 0 && mi % A[i + 1] != 0) {
      cout << -1 << endl;
      return 0;
    } else {
      ma = ma / A[i + 1] * A[i + 1] + A[i + 1] - 1;
      mi = (mi + A[i + 1] - 1) / A[i + 1] * A[i + 1];
    }
  }
  cout << mi << " " << ma << endl;
}