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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
#define fi first
#define se second
#define show(x) cout << #x << " is " << x << "\\n";
const ll inf = 9e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
}
ll power(ll x, ll y, ll M = inf) {
  if (y <= 0)
    return 1;
  ll ans = 1;
  x %= M;
  while (y) {
    if (y & 1)
      ans = (x * ans) % M;

    x = (x * x) % M;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2, mod); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c > mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

// const ll N = 1e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // cout<<fixed<<setprecision(10);

  ll n;
  cin >> n;
  ld a[n];
  ll gf = 1000000000000000000;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (a[0] == 0)
    cout << 0;
  else {
    ld ans = 0;
    ll ans1 = 1;
    for (int i = 0; i < n; i++) {
      ans += log10(a[i]);
      ans1 *= a[i];
    }
    // cout<<ans1;
    if (ans > 18 || ans1 > gf)
      cout << -1;
    else
      cout << ans1;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <utility>
using boost::multiprecision::cpp_int;
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
#define fi first
#define se second
#define show(x) cout << #x << " is " << x << "\\n";
const ll inf = 9e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
}
ll power(ll x, ll y, ll M = inf) {
  if (y <= 0)
    return 1;
  ll ans = 1;
  x %= M;
  while (y) {
    if (y & 1)
      ans = (x * ans) % M;

    x = (x * x) % M;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2, mod); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c > mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

// const ll N = 1e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  cpp_int a[n];
  cpp_int ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // 		(a[i]==0) flag=1;
    // 		//ans*=a[iif];
  }
  //	if(flag==1) {
  //		cout<<0;
  //		return 0;
  //	}
  //	sort(a, a+n, greater<ld>());
  for (int i = 0; i < n; i++) {
    ans = ans * a[i];
  }
  if (ans > 1000000000000000000) {
    cout << -1 << "\\n";
    return 0;
  }
  cout << ans << "\\n";
  return 0;
}