Problem p02658 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
#include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll n, m, k;
// ld d;
string s /*1, s2, s3*/;
char c /*1, c2, c3*/;
bool flag = false;

/*--------------------template--------------------*/

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

  cin >> n;
  vi a(n);
  veccin(a);
  cpp_int ans = 1;
  sort(ALL(a));
  if (a[0] == 0) {
    cout << 0;
    return 0;
  }
  REP(i, n) {
    ans *= a[i];
    if (ans > cpp_int(1e+18)) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
#include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll n, m, k;
// ld d;
string s /*1, s2, s3*/;
char c /*1, c2, c3*/;
bool flag = false;

/*--------------------template--------------------*/

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

  cin >> n;
  cpp_int ans = 1;
  REP(i, n) {
    cin >> m;
    ans *= m;
  }
  if (ans > cpp_int(1e+18)) {
    ans = -1;
  }
  cout << ans << endl;
}