Problem p00507 (C++) — buggy submission, verdict: Runtime Error

#include "algorithm"
#include "iostream"
#include "string"
using namespace std;

int main() {
  int n;
  int temp;
  int a[5][10000] = {-1};
  int ans[10000];
  int b, c;
  int digit;
  int digit_now = 0;
  int count = 0;
  int digit_num[5] = {0};
  bool flag = true;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> temp;
    digit = to_string(temp).length();
    a[digit - 1][digit_num[digit - 1]++] = temp;
  }
  for (int i = 0; i < 5; ++i) {
    sort(a[i], a[i] + digit_num[i]);
    if (digit_num[i] == n)
      flag = false;
  }
  if (flag) {
    while (1) {
      for (int i = 0; i < digit_now + 1; ++i) {
        // cout<<"i: "<<i<<endl;
        for (int j = 0; j < digit_num[i]; ++j) {
          b = a[i][j];
          // cout<<"b: a["<<i<<"]["<<j<<"]: "<<b<<endl;
          int k = digit_now - i;
          if (k < 0)
            break;
          for (int l = 0; l < digit_num[k]; ++l) {
            if (k == i && l == j)
              continue;
            if (a[k][l] != -1)
              count++;
            c = a[k][l];
            // cout<<"c: a["<<i<<"]["<<j<<"]: "<<c<<endl;
            ans[count - 1] = stoi(to_string(b) + to_string(c));
          }
        }
      }
      if (count > 3) {
        sort(ans, ans + count);
        // for(int i=0;i<count;++i)
        // cout<<"ans["<<i<<"]: "<<ans[i]<<endl;
        cout << ans[2] << endl;
        return 0;
      }
      digit_now++;
    }
  } else {
    while (1) {
      for (int i = 0; i < digit_now + 1; ++i) {
        // cout<<"i: "<<i<<endl;
        for (int j = 0; j < digit_num[i]; ++j) {
          b = a[i][j];
          // cout<<"b: a["<<i<<"]["<<j<<"]: "<<b<<endl;
          int k = digit_now - i;
          if (k < 0)
            break;
          for (int l = 0; l < digit_num[k]; ++l) {
            if (k == i && l == j)
              continue;
            if (a[k][l] != -1)
              count++;
            c = a[k][l];
            // cout<<"c: a["<<i<<"]["<<j<<"]: "<<c<<endl;
            if (count == 3) {
              cout << to_string(b) + to_string(c) << endl;
              return 0;
            }
          }
        }
      }
      digit_now++;
    }
  }
}


Error: 0

Fixed submission:

#include "algorithm"
#include "iostream"
#include "string"
using namespace std;

int main() {
  int n;
  int temp;
  int a[5][10000] = {-1};
  int ans[10000];
  int b, c;
  int digit;
  int digit_now = 0;
  int count = 0;
  int digit_num[5] = {0};
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> temp;
    digit = to_string(temp).length();
    a[digit - 1][digit_num[digit - 1]++] = temp;
  }
  for (int i = 0; i < 5; ++i) {
    sort(a[i], a[i] + digit_num[i]);
  }
  while (1) {
    for (int i = 0; i < digit_now + 1; ++i) {
      // cout<<"i: "<<i<<endl;
      for (int j = 0; j < digit_num[i]; ++j) {
        b = a[i][j];
        // cout<<"b: a["<<i<<"]["<<j<<"]: "<<b<<endl;
        int k = digit_now - i;
        if (k < 0)
          break;
        for (int l = 0; l < digit_num[k]; ++l) {
          if (k == i && l == j)
            continue;
          if (a[k][l] != -1)
            count++;
          c = a[k][l];
          // cout<<"c: a["<<i<<"]["<<j<<"]: "<<c<<endl;
          ans[count - 1] = stoi(to_string(b) + to_string(c));
        }
      }
    }
    if (count > 3) {
      sort(ans, ans + count);
      // for(int i=0;i<count;++i)
      // cout<<"ans["<<i<<"]: "<<ans[i]<<endl;
      cout << ans[2] << endl;
      return 0;
    }
    digit_now++;
  }
}