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

/*
 ▓█████▄  ▄████▄  ▓██   ██▓ ██▓███   ██░ ██ ▓█████  ██▀███
▒██▀ ██▌▒██▀ ▀█   ▒██  ██▒▓██░  ██▒▓██░ ██▒▓█   ▀ ▓██ ▒ ██▒
 ░██   █▌▒▓█    ▄   ▒██ ██░▓██░ ██▓▒▒██▀▀██░▒███   ▓██ ░▄█ ▒
 ░▓█▄   ▌▒▓▓▄ ▄██▒  ░ ▐██▓░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█  ▄ ▒██▀▀█▄
 ░▒████▓ ▒ ▓███▀ ░  ░ ██▒▓░▒██▒ ░  ░░▓█▒░██▓░▒████▒░██▓ ▒██▒
  ▒▒▓  ▒ ░ ░▒ ▒  ░   ██▒▒▒ ▒▓▒░ ░  ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░
  ░ ▒  ▒   ░  ▒    ▓██ ░▒░ ░▒ ░      ▒ ░▒░ ░ ░ ░  ░  ░▒ ░ ▒░
  ░ ░  ░ ░         ▒ ▒ ░░  ░░        ░  ░░ ░   ░     ░░   ░
    ░    ░ ░       ░ ░               ░  ░  ░   ░  ░   ░
  ░      ░         ░ ░
*/
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using boost::multiprecision::cpp_int;
using namespace __gnu_pbds;
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define sedes set<ll, greater<ll>>
#define seasc set<ll>
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map<ll, ll>
#define map map<ll, ll>
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define mems(a, i) memset(a, i, sizeof(a))
#define endl '\\n'
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define printx(x) printf("%lld\\n", x);
#define printxy(x, y) cout << x << y << endl;
#define printxyz(x, y, z) cout << x << y << z << endl;
#define debuga(a) cout << "debug " << a << endl;
#define debugab(a, b) cout << "debug1 " << a << " debug2 " << b << endl;
/*void product(ll ar[], ll n)
{
  cpp_int result = 1;
  for (ll i = 0; i < n; i++)
    result = result * ar[i];
  if (result > 1000000000000000000)
    cout << -1 << endl;
  else
    cout << result << endl;
}*/
int32_t main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("/home/kushagra/Desktop/Kushagra/input.txt", "r", stdin);
  freopen("/home/kushagra/Desktop/Kushagra/output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  cpp_int a[n];
  cpp_int res = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    res = res * a[i];
    if (res > 1000000000000000000) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

/*
 ▓█████▄  ▄████▄  ▓██   ██▓ ██▓███   ██░ ██ ▓█████  ██▀███
▒██▀ ██▌▒██▀ ▀█   ▒██  ██▒▓██░  ██▒▓██░ ██▒▓█   ▀ ▓██ ▒ ██▒
 ░██   █▌▒▓█    ▄   ▒██ ██░▓██░ ██▓▒▒██▀▀██░▒███   ▓██ ░▄█ ▒
 ░▓█▄   ▌▒▓▓▄ ▄██▒  ░ ▐██▓░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█  ▄ ▒██▀▀█▄
 ░▒████▓ ▒ ▓███▀ ░  ░ ██▒▓░▒██▒ ░  ░░▓█▒░██▓░▒████▒░██▓ ▒██▒
  ▒▒▓  ▒ ░ ░▒ ▒  ░   ██▒▒▒ ▒▓▒░ ░  ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░
  ░ ▒  ▒   ░  ▒    ▓██ ░▒░ ░▒ ░      ▒ ░▒░ ░ ░ ░  ░  ░▒ ░ ▒░
  ░ ░  ░ ░         ▒ ▒ ░░  ░░        ░  ░░ ░   ░     ░░   ░
    ░    ░ ░       ░ ░               ░  ░  ░   ░  ░   ░
  ░      ░         ░ ░
*/
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using boost::multiprecision::cpp_int;
using namespace __gnu_pbds;
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define sedes set<ll, greater<ll>>
#define seasc set<ll>
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map<ll, ll>
#define map map<ll, ll>
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define mems(a, i) memset(a, i, sizeof(a))
#define endl '\\n'
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define printx(x) printf("%lld\\n", x);
#define printxy(x, y) cout << x << y << endl;
#define printxyz(x, y, z) cout << x << y << z << endl;
#define debuga(a) cout << "debug " << a << endl;
#define debugab(a, b) cout << "debug1 " << a << " debug2 " << b << endl;
void product(ll ar[], ll n) {
  cpp_int result = 1;
  for (ll i = 0; i < n; i++)
    result = result * ar[i];
  if (result > 1000000000000000000)
    cout << -1 << endl;
  else
    cout << result << endl;
}
int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("/home/kushagra/Desktop/Kushagra/input.txt", "r", stdin);
  freopen("/home/kushagra/Desktop/Kushagra/output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll a[n + 1];
  rep(i, 0, n) cin >> a[i];
  product(a, n);

  return 0;
}