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

#include <bits/stdc++.h>
#define ll high_precision
using namespace std;
class high_precision {
public:
  int a[50], lenth;
  void read() {
    memset(a, 0, sizeof(a));
    string s;
    cin >> s;
    lenth = s.size();
    for (int i = 0; i < lenth; i++)
      a[i] = s[lenth - i - 1] - '0';
    while (lenth >= 1 && a[lenth - 1] == 0)
      --lenth;
  }
  void write() {
    for (int i = lenth - 1; i >= 0; --i)
      printf("%d", a[i]);
  }
  high_precision operator*(high_precision &num) {
    high_precision ans;
    ans.lenth = lenth + num.lenth;
    for (int i = 0; i <= ans.lenth; ++i)
      ans.a[i] = 0;
    ans.lenth = 0;
    for (int i = 0; i < lenth; i++) {
      int mo = 0;
      for (int j = 0; j < num.lenth; j++) {
        ans.a[i + j] = mo + a[i] * num.a[j] + ans.a[i + j];
        mo = ans.a[i + j] / 10;
        ans.a[i + j] %= 10;
      }
      ans.a[i + num.lenth] = mo;
    }
    int k = lenth + num.lenth;
    while (k > 0 && ans.a[k] == 0)
      --k;
    ans.lenth = k + 1;
    return ans;
  }
  void operator=(long long num) {
    int l = 0;
    while (num) {
      a[l++] = num % 10;
      num = num / 10;
    }
    lenth = l;
  }
};
bool compare(ll a, ll b) {
  if (a.lenth != b.lenth)
    return a.lenth > b.lenth;
  for (int i = a.lenth - 1; i >= 0; --i)
    if (a.a[i] != b.a[i])
      return a.a[i] > b.a[i];
  return false;
}
ll ANS, mx;
ll A[100010];
int n;
bool b = false, bb = false;
int main() {
  cin >> n;
  ANS = 1;
  mx = 1000000000000000000;
  for (int i = 1; i <= n; ++i) {
    A[i].read();
    if (A[i].lenth == 0)
      bb = true;
    if (compare(A[i], mx))
      b = true;
  }
  if (bb) {
    printf("0");
    return 0;
  } else if (b) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    ANS = ANS * A[i];
    if (compare(ANS, mx)) {
      printf("-1");
      return 0;
    }
  }
  ANS.write();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll high_precision
using namespace std;
class high_precision {
public:
  int a[180010], lenth;
  void read() {
    memset(a, 0, sizeof(a));
    string s;
    scanf(" ");
    cin >> s;
    lenth = s.size();
    for (int i = 0; i < lenth; i++)
      a[i] = s[lenth - i - 1] - '0';
  }
  void write() {
    for (int i = lenth - 1; i >= 0; --i)
      printf("%d", a[i]);
  }
  high_precision operator*(high_precision &num) {
    high_precision ans;
    ans.lenth = lenth + num.lenth;
    for (int i = 0; i < ans.lenth; ++i)
      ans.a[i] = 0;
    ans.lenth = 0;
    for (int i = 0; i < lenth; i++) {
      int mo = 0;
      for (int j = 0; j < num.lenth; j++) {
        ans.a[i + j] = mo + a[i] * num.a[j] + ans.a[i + j];
        mo = ans.a[i + j] / 10;
        ans.a[i + j] %= 10;
      }
      ans.a[i + num.lenth] = mo;
    }
    int k = lenth + num.lenth;
    while (k > 0 && ans.a[k] == 0)
      --k;
    ans.lenth = k + 1;
    return ans;
  }
  void operator=(long long num) {
    int l = 0;
    while (num) {
      a[l++] = num % 10;
      num = num / 10;
    }
    lenth = l;
  }
};
bool compare(ll a, ll b) {
  if (a.lenth != b.lenth)
    return a.lenth > b.lenth;
  for (int i = a.lenth - 1; i >= 0; --i)
    if (a.a[i] != b.a[i])
      return a.a[i] > b.a[i];
  return false;
}
ll A, ANS, mx;
int n;
bool b = true;
int main() {
  scanf("%d", &n);
  ANS = 1;
  mx = 1000000000000000000;
  for (int i = 1; i <= n; ++i) {
    A.read();
    ANS = A * ANS;
  }
  if (compare(ANS, mx))
    printf("-1");
  else
    ANS.write();
  return 0;
}