Problem p02658 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;

  std::vector<std::string> a(n);
  bool zero = false;
  unsigned int lensum = 0;
  for (int i = 0; i < n; ++i) {
    std::cin >> a.at(i);
    if (a.at(i) == "0") {
      zero = true;
    }
    lensum += a.at(i).size() - 1;
  }

  if (zero) {
    std::cout << "0" << std::endl;
    return 0;
  }

  if (lensum > 19) {
    std::cout << "-1" << std::endl;
  }

  std::string final_result = a.at(0);
  for (int i = 1; i < n; ++i) {
    std::string u = a.at(i);
    std::vector<std::string> temps(u.size(), "");

    for (unsigned int j = 0; j < u.size(); ++j) {
      int p = std::stoi(u.substr(u.size() - 1 - j, 1));
      int kuriagari = 0;

      for (unsigned int k = 0; k < final_result.size(); ++k) {
        int q = std::stoi(final_result.substr(final_result.size() - 1 - k, 1));
        int r = p * q;
        temps.at(j) += std::to_string((kuriagari + r) % 10);
        kuriagari = (kuriagari + r) / 10;
        if (k == final_result.size() - 1 && kuriagari != 0) {
          temps.at(j) += std::to_string(kuriagari);
        }
      }
      std::reverse(temps.at(j).begin(), temps.at(j).end());

      for (unsigned int l = 0; l < j; ++l) {
        temps.at(j) += "0";
      }
      // std::cout << temps.at(j) << std::endl;
    }

    unsigned int maxlen = 0;
    for (unsigned int j = 0; j < u.size(); ++j) {
      if (temps.at(j).size() > maxlen) {
        maxlen = temps.at(j).size();
      }
    }

    for (unsigned int j = 0; j < u.size(); ++j) {
      while (temps.at(j).size() != maxlen) {
        temps.at(j).insert(0, "0");
      }
      // std::cout << temps.at(j) << std::endl;
    }

    final_result = "";
    int kuriagari = 0;
    for (unsigned int l = 0; l < maxlen; ++l) {
      int sum = 0;

      for (unsigned int j = 0; j < u.size(); ++j) {
        sum += std::stoi(temps.at(j).substr(maxlen - 1 - l, 1));
      }

      final_result += std::to_string((kuriagari + sum) % 10);
      kuriagari = (kuriagari + sum) / 10;
      if (l == maxlen - 1 && kuriagari != 0) {
        final_result += std::to_string(kuriagari);
      }
    }
    std::reverse(final_result.begin(), final_result.end());
    if (final_result.substr(0, 1) == "0") {
      final_result = "0";
    }
    // std::cout << final_result << std::endl;
  }

  // std::cout << final_result << std::endl;
  if (final_result.size() >= 19 && final_result > "1000000000000000000") {
    final_result = "-1";
  }

  std::cout << final_result << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;

  std::vector<std::string> a(n);
  bool zero = false;
  unsigned int lensum = 0;
  for (int i = 0; i < n; ++i) {
    std::cin >> a.at(i);
    if (a.at(i) == "0") {
      zero = true;
    }
    lensum += a.at(i).size() - 1;
  }

  if (zero) {
    std::cout << "0" << std::endl;
    return 0;
  }

  if (lensum > 19) {
    std::cout << "-1" << std::endl;
    return 0;
  }

  std::string final_result = a.at(0);
  for (int i = 1; i < n; ++i) {
    std::string u = a.at(i);
    std::vector<std::string> temps(u.size(), "");

    for (unsigned int j = 0; j < u.size(); ++j) {
      int p = std::stoi(u.substr(u.size() - 1 - j, 1));
      int kuriagari = 0;

      for (unsigned int k = 0; k < final_result.size(); ++k) {
        int q = std::stoi(final_result.substr(final_result.size() - 1 - k, 1));
        int r = p * q;
        temps.at(j) += std::to_string((kuriagari + r) % 10);
        kuriagari = (kuriagari + r) / 10;
        if (k == final_result.size() - 1 && kuriagari != 0) {
          temps.at(j) += std::to_string(kuriagari);
        }
      }
      std::reverse(temps.at(j).begin(), temps.at(j).end());

      for (unsigned int l = 0; l < j; ++l) {
        temps.at(j) += "0";
      }
      // std::cout << temps.at(j) << std::endl;
    }

    unsigned int maxlen = 0;
    for (unsigned int j = 0; j < u.size(); ++j) {
      if (temps.at(j).size() > maxlen) {
        maxlen = temps.at(j).size();
      }
    }

    for (unsigned int j = 0; j < u.size(); ++j) {
      while (temps.at(j).size() != maxlen) {
        temps.at(j).insert(0, "0");
      }
      // std::cout << temps.at(j) << std::endl;
    }

    final_result = "";
    int kuriagari = 0;
    for (unsigned int l = 0; l < maxlen; ++l) {
      int sum = 0;

      for (unsigned int j = 0; j < u.size(); ++j) {
        sum += std::stoi(temps.at(j).substr(maxlen - 1 - l, 1));
      }

      final_result += std::to_string((kuriagari + sum) % 10);
      kuriagari = (kuriagari + sum) / 10;
      if (l == maxlen - 1 && kuriagari != 0) {
        final_result += std::to_string(kuriagari);
      }
    }
    std::reverse(final_result.begin(), final_result.end());
    if (final_result.substr(0, 1) == "0") {
      final_result = "0";
    }
    // std::cout << final_result << std::endl;
  }

  // std::cout << final_result << std::endl;
  if (final_result.size() >= 19 && final_result > "1000000000000000000") {
    final_result = "-1";
  }

  std::cout << final_result << std::endl;

  return 0;
}