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

#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <math.h>
#include <string>
#define pi 3.1415926535897932384626
#include <cmath>
#define test(t) while (t--)

#define ll long long int

#define in cin >>
#define out cout <<
#define en "\\n"
#define sp " "
#define pb push_back
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define double long double

using namespace std;
ll form(ll n) { return (n * (n + 1)) / 2; }
ll exponent(ll base, ll power) {
  ll res = 1;
  ll m = 1000000007;
  while (power) {
    if (power & 1)
      res = (res * base) % m;
    base = (base * base) % m;
    power = power >> 1;
  }
  return res;
}
bool count_one(ll n) {
  ll cnt = 0;
  while (n > 0) {
    cnt += 1;
    n = n & (n - 1);
  }
  if (cnt > 1)
    return true;
  return false;
}
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++)
    in a[i];
  ll k = a[0];
  sort(a, a + n);
  bool flag = true;
  if (a[0] != 0) {
    for (ll i = 1; i < n; i++) {
      if ((double)a[i] >= (double)1000000000000000001 / k) {
        flag = false;
        break;
      } else
        k = k * a[i];
    }
    if (flag == true)
      out k << en;
    else
      out - 1 << en;
  } else
    out 0 << en;
}

int main() {
  fast()
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      ll t = 1;
  // cin>>t;
  test(t) { solve(); }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// Multiplies str1 and str2, and prints result.
string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  // will keep the result number in vector
  // in reverse order
  vector<int> result(len1 + len2, 0);

  // Below two indexes are used to find positions
  // in result.
  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    // To shift position to left after every
    // multiplication of a digit in num2
    i_n2 = 0;

    // Go from right to left in num2
    for (int j = len2 - 1; j >= 0; j--) {
      // Take current digit of second number
      int n2 = num2[j] - '0';

      // Multiply with current digit of first number
      // and add result to previously stored result
      // at current position.
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    // To shift position to left after every
    // multiplication of a digit in num1.
    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}

// Driver code
int main() {
  long long int no;
  string str1, str2;
  cin >> no;
  cin >> str1 >> str2;
  str2 = multiply(str1, str2);
  for (long long int i = 0; i < no - 2; i++) {
    cin >> str1;
    str2 = multiply(str1, str2);
  }
  // cout<<str2.size()<<endl;
  // cout<<str2<<"\\n";
  if (str2.size() > 19)
    cout << -1 << "\\n";
  else if (str2.size() == 19) {
    if (str2[18] == '0')
      cout << str2 << "\\n";
    else
      cout << -1 << "\\n";
  } else if (str2.size() < 19)
    cout << str2 << "\\n";
  return 0;
}