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

/****************************
 *  Developer : joshi08
 *  Date  :  16-06-2020
 *  Year  :  2020
 ****************************/

#include <bits/stdc++.h>

#define lli long long int
#define mod 1000000007
#define fi first
#define se second
#define ii pair<lli, lli>
#define mp make_pair
#define endl '\\n'
#define maxN 1000000000000000000
const double PI = 3.141592653589793238460;
lli gcd(lli a, lli b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
lli max(lli a, lli b) {
  if (a > b)
    return a;
  else
    return b;
}
lli min(lli a, lli b) {
  if (a < b)
    return a;
  else
    return b;
}
lli power(lli a, lli b) {
  lli i, p = 1;
  for (i = 0; i < b; i++) {
    p *= a;
  }
  return p;
}

using namespace std;

int main() {
  ios::sync_with_stdio(false);

  lli n, i, check = 0, flag = 0, k, m = 1;
  cin >> n;
  vector<lli> a;
  // string s1,s2="1";
  for (i = 0; i < n; i++) {
    cin >> k;

    if (k == 0)
      flag = 1;

    a.push_back(k);
  }

  if (flag) {
    cout << 0;
    return 0;
  }

  for (i = 0; i < n; i++) {
    if (m > maxN / a[i]) {
      check = 1;
      break;
    }
    m = m * a[i];
  }

  if (check) {
    cout << -1;
    return 0;
  }
  cout << m;

  return 0;
}


Error: TLE

Fixed submission:

/****************************
 *  Developer : joshi08
 *  Date  :  16-06-2020
 *  Year  :  2020
 ****************************/

#include <bits/stdc++.h>

#define lli long long int
#define mod 1000000007
#define fi first
#define se second
#define ii pair<lli, lli>
#define mp make_pair
#define endl '\\n'
const double PI = 3.141592653589793238460;
lli gcd(lli a, lli b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
lli max(lli a, lli b) {
  if (a > b)
    return a;
  else
    return b;
}
lli min(lli a, lli b) {
  if (a < b)
    return a;
  else
    return b;
}
lli power(lli a, lli b) {
  lli i, p = 1;
  for (i = 0; i < b; i++) {
    p *= a;
  }
  return p;
}

using namespace std;

int toi(string s) {
  int r = s[0] - '0';
  for (int i = 1; i < s.length(); i++)
    r *= 10, r += s[i] - '0';
  return r;
}
string tos(int n) {
  string r;
  while (n > 9) {
    r.push_back(n % 10 + '0');
    n /= 10;
  }
  r.push_back(n % 10 + '0');
  reverse(r.begin(), r.end());
  return r;
}
string sum(string s, string p, int m = 10, char z = '0') {
  string r;
  int b = 0;
  reverse(s.begin(), s.end());
  reverse(p.begin(), p.end());
  if (s.size() < p.size())
    s.swap(p);
  for (int i = 0; i < s.size(); i++) {
    if (i >= p.size()) {
      r.push_back((s[i] - z + b) % m + z);
      if (s[i] - z + b >= m)
        b = 1;
      else
        b = 0;
    } else {
      r.push_back((p[i] - z + s[i] - z + b) % m + z);
      if (p[i] - z + s[i] - z + b >= m)
        b = 1;
      else
        b = 0;
    }
  }
  if (b > 0)
    r.push_back(b + z);
  reverse(r.begin(), r.end());
  return r;
}

string multiplyint(string s, int n) {
  string r = tos((s[0] - '0') * n);
  for (int i = 1; i < s.length(); i++) {
    r.push_back('0');
    r = sum(r, tos((s[i] - '0') * n));
  }
  return r;
}

string multiply(string s, string p) {
  string r = multiplyint(p, s[0] - '0');
  for (int i = 1; i < s.length(); i++) {
    r.push_back('0');
    r = sum(r, multiplyint(p, s[i] - '0'));
  }

  // cout<<r<<endl;
  int check = 0;
  string t;
  for (int i = 0; i < r.size(); i++) {
    if (r[i] == '0' && check == 0) {
      continue;
    } else {
      check = 1;
      t.push_back(r[i]);
    }
  }
  if (t.empty())
    t = "0";
  return t;
}

int main() {
  ios::sync_with_stdio(false);

  lli n, i, check = 0;
  cin >> n;
  string s1, s2 = "1";
  for (i = 0; i < n; i++) {
    cin >> s1;

    s2 = multiply(s1, s2);
  }

  // cout<<s2<<endl;
  if (s2.size() == 19 && s2[0] == '1') {
    check = 1;
    for (i = 1; i < s2.size(); i++) {
      if (s2[i] == '0' && check == 1) {
        continue;
      } else {
        check = 0;
        break;
      }
    }
  }

  // cout<<s2.size()<<" "<<check<<endl;
  if (s2.size() < 19 || check == 1) {
    cout << s2;
  } else {
    cout << -1;
  }
  return 0;
}