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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

//--boost--
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部長が32の浮動小数点数型
using Real32 = mp::number<mp::cpp_dec_float<32>>;
// 仮数部長が1024の浮動小数点数型
using Real1024 = mp::number<mp::cpp_dec_float<1024>>;
// ついでに有理数型
using Rat = boost::rational<Bint>;
//!--boost--

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = unsigned long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N - 1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N - 1, 0)
const ll INF = 1LL << 60;
const int INF_INT = 1 << 30;

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;
}

struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
  }
} fast; // cin,cout高速化のおまじない＋桁数指定

// void solve()
//{
//	ll N;
//	cin >> N;
//	vector<ll> A(N);
//	rep(i, N) {
//		cin >> A[i];
//		if (A[i] == 0) {
//			cout << 0 << endl;
//			return;
//		}
//	}
//
//	unsigned long long result = 1;
//	rep(i, N) {
//		unsigned long long temp = (A[i] + (unsigned long
//long)1000000000000000000 - 1); 		temp /= A[i]; 		if (result > temp) { 			cout << -1
//<< endl; 			return;
//		}
//		else {
//			result *= A[i];
//			if (result > (unsigned long long)1000000000000000000) {
//				cout << -1 << endl;
//				return;
//			}
//		}
//	}
//
//	cout << result << endl;
// }

void solve() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    if (A[i] == 0) {
      cout << 0 << endl;
      return;
    }
  }

  Bint result = 1;

  rep(i, N) {
    result *= A[i];
    if (result > (Bint)1e18) {
      cout << -1 << endl;
      return;
    }
  }

  cout << result << endl;
}

int main() {
#if TEST && LOCAL
  ll t;
  cin >> t;
  rep(i, t) { solve(); }
#else
  solve();
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

//--boost--
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部長が32の浮動小数点数型
using Real32 = mp::number<mp::cpp_dec_float<32>>;
// 仮数部長が1024の浮動小数点数型
using Real1024 = mp::number<mp::cpp_dec_float<1024>>;
// ついでに有理数型
using Rat = boost::rational<Bint>;
//!--boost--

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = unsigned long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N - 1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N - 1, 0)
const ll INF = 1LL << 60;
const int INF_INT = 1 << 30;

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;
}

struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
  }
} fast; // cin,cout高速化のおまじない＋桁数指定

// void solve()
//{
//	ll N;
//	cin >> N;
//	vector<ll> A(N);
//	rep(i, N) {
//		cin >> A[i];
//		if (A[i] == 0) {
//			cout << 0 << endl;
//			return;
//		}
//	}
//
//	unsigned long long result = 1;
//	rep(i, N) {
//		unsigned long long temp = (A[i] + (unsigned long
//long)1000000000000000000 - 1); 		temp /= A[i]; 		if (result > temp) { 			cout << -1
//<< endl; 			return;
//		}
//		else {
//			result *= A[i];
//			if (result > (unsigned long long)1000000000000000000) {
//				cout << -1 << endl;
//				return;
//			}
//		}
//	}
//
//	cout << result << endl;
// }

void solve() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    if (A[i] == 0) {
      cout << 0 << endl;
      return;
    }
  }

  std::sort(A.begin(), A.end());

  Bint result = 1;

  rep(i, N) { result *= A[i]; }

  if (result > (Bint)1000000000000000000) {
    cout << -1 << endl;
    return;
  }

  cout << result << endl;
}

int main() {
#if TEST && LOCAL
  ll t;
  cin >> t;
  rep(i, t) { solve(); }
#else
  solve();
#endif
  return 0;
}