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

#include <iostream>
#include <string>
using namespace std;
const int N = 2e5 + 10;
string q[N];
string ADD(string, string);
string MULTIPLE(string, string);
string ADD(string a, string b) {
  string res = "";
  int carryitem = 0;
  int nowitem = 0;
  int a_len = a.length(), b_len = b.length();
  if (a_len < b_len) {
    swap(a, b);
    a_len = a.length();
    b_len = b.length();
  }
  for (int i = a_len - b_len; i > 0; --i) {
    b = '0' + b;
  }
  for (int i = a_len - 1; i >= 0; --i) {
    nowitem = a[i] + b[i] - 2 * '0' + carryitem;
    carryitem = nowitem / 10;
    nowitem %= 10;
    res = static_cast<char>(nowitem + '0') + res;
  }
  if (carryitem) {
    res = static_cast<char>(carryitem + '0') + res;
  }
  return res;
}
string MULTIPLE(string a, string b) {
  string res = "";
  int a_len = a.length(), b_len = b.length();
  if (a_len < b_len) {
    swap(a, b);
    a_len = a.length();
    b_len = b.length();
  }

  for (int i = b_len - 1; i >= 0; --i) {
    string tmp = "";
    for (int j = 0; j < b[i] - '0'; j++) {
      tmp = ADD(tmp, a);
    }
    for (int k = 0; k < b_len - i - 1; k++) {
      tmp += '0';
    }
    res = ADD(res, tmp);
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string res = "1";
  int flag = 0;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
    if (q[i] == "0") {
      flag = 1;
      break;
    }
  }
  if (flag != 1) {
    for (int i = 0; i < n; i++) {
      res = MULTIPLE(res, q[i]);
      if (res != "1000000000000000000" && res.length() >= 19) {
        flag = 2;
        break;
      }
    }
  }
  if (flag == 1)
    cout << 0;
  else if (flag == 2) {
    cout << -1;
  } else
    cout << res;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string ADD(string, string);
string MULTIPLE(string, string);
string ADD(string a, string b) {
  string res = "";
  int carryitem = 0;
  int nowitem = 0;
  int a_len = a.length(), b_len = b.length();
  if (a_len < b_len) {
    swap(a, b);
    a_len = a.length();
    b_len = b.length();
  }
  for (int i = a_len - b_len; i > 0; --i) {
    b = '0' + b;
  }
  for (int i = a_len - 1; i >= 0; --i) {
    nowitem = a[i] + b[i] - 2 * '0' + carryitem;
    carryitem = nowitem / 10;
    nowitem %= 10;
    res = static_cast<char>(nowitem + '0') + res;
  }
  if (carryitem) {
    res = static_cast<char>(carryitem + '0') + res;
  }
  return res;
}
string MULTIPLE(string a, string b) {
  string res = "";
  int a_len = a.length(), b_len = b.length();
  if (a_len < b_len) {
    swap(a, b);
    a_len = a.length();
    b_len = b.length();
  }

  for (int i = b_len - 1; i >= 0; --i) {
    string tmp = "";
    for (int j = 0; j < b[i] - '0'; j++) {
      tmp = ADD(tmp, a);
    }
    for (int k = 0; k < b_len - i - 1; k++) {
      tmp += '0';
    }
    res = ADD(res, tmp);
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string res = "1";
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    res = MULTIPLE(res, t);
  }
  if (res != "1000000000000000000" && res.length() >= 19)
    cout << "-1";

  else

      if (res.length() == 0)
    cout << 0;

  else
    cout << res;
  return 0;
}