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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define FOR(i, n) REP(i, 0, n)

string string_add(string a, string b) {
  string new_str = "";
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());

  string long_s;
  string short_s;
  if (a.size() >= b.size()) {
    long_s = a;
    short_s = b;
  } else {
    long_s = b;
    short_s = a;
  }
  int carry = 0;
  int dif = long_s.size() - short_s.size();
  FOR(i, dif)
  short_s += "0";
  FOR(i, long_s.size()) {
    int hab = stoi(long_s.substr(i, 1)) + stoi(short_s.substr(i, 1)) + carry;
    if (hab > 9) {
      carry = 1;
      hab -= 10;
    } else
      carry = 0;

    new_str += to_string(hab);
  }
  if (carry != 0)
    new_str += to_string(carry);
  reverse(new_str.begin(), new_str.end());
  return new_str;
}

string string_multi(string a, string b) {
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());

  string acc = "0";
  FOR(i, a.size()) {
    int carry = 0;
    string gob = "";
    FOR(j, b.size()) {
      int gob_1 = stoi(a.substr(i, 1)) * stoi(b.substr(j, 1)) + carry;
      gob += to_string(gob_1 % 10);
      carry = gob_1 / 10;
    }
    if (carry != 0)
      gob += to_string(carry);
    FOR(j, i)
    gob = "0" + gob;
    reverse(gob.begin(), gob.end());
    acc = string_add(acc, gob);
  }
  return acc;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  string now = "1", maxN = "1";
  for (int i = 0; i < 18; i++)
    maxN += "0";
  cin >> N;
  vector<string> v(N);
  for (auto &i : v) {
    cin >> i;
    if (i == "0") {
      cout << 0;
      return 0;
    }
  }

  for (auto tmp : v) {
    if (tmp == "0") {
      cout << 0;
      return 0;
    }
    now = string_multi(now, tmp);
    if (now.size() >= 19 && now != maxN) {
      cout << -1;
      return 0;
    }
  }

  cout << now;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define FOR(i, n) REP(i, 0, n)

string string_add(string a, string b) {
  string new_str = "";
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());

  string long_s;
  string short_s;
  if (a.size() >= b.size()) {
    long_s = a;
    short_s = b;
  } else {
    long_s = b;
    short_s = a;
  }
  int carry = 0;
  int dif = long_s.size() - short_s.size();
  FOR(i, dif)
  short_s += "0";
  FOR(i, long_s.size()) {
    int hab = stoi(long_s.substr(i, 1)) + stoi(short_s.substr(i, 1)) + carry;
    if (hab > 9) {
      carry = 1;
      hab -= 10;
    } else
      carry = 0;

    new_str += to_string(hab);
  }
  if (carry != 0)
    new_str += to_string(carry);
  reverse(new_str.begin(), new_str.end());
  return new_str;
}

string string_multi(string a, string b) {
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());

  string acc = "0";
  FOR(i, a.size()) {
    int carry = 0;
    string gob = "";
    FOR(j, b.size()) {
      int gob_1 = stoi(a.substr(i, 1)) * stoi(b.substr(j, 1)) + carry;
      gob += to_string(gob_1 % 10);
      carry = gob_1 / 10;
    }
    if (carry != 0)
      gob += to_string(carry);
    FOR(j, i)
    gob = "0" + gob;
    reverse(gob.begin(), gob.end());
    acc = string_add(acc, gob);
  }
  return acc;
}

int main() {
  int N;
  string now = "1", maxN = "1";
  for (int i = 0; i < 18; i++)
    maxN += "0";
  cin >> N;
  while (N--) {
    string tmp;
    cin >> tmp;
    if (tmp == "0") {
      cout << 0;
      return 0;
    }
    now = string_multi(now, tmp);
  }

  if (now.size() >= 19 && now != maxN)
    cout << -1;
  else
    cout << now;
}