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

#include <algorithm>
#include <stdio.h>
using namespace std;
char A[10][100];
char B[10][100];
bool ans[10][800001];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < a; i++)
      scanf("%s", A[i]);
    for (int i = 0; i < b; i++)
      scanf("%s", B[i]);
    for (int i = 0; i < b; i++)
      for (int j = 0; j < 800001; j++)
        ans[i][j] = false;
    for (int i = 1; i <= 20; i++) {
      for (int j = 1; j <= 20; j++) {
        for (int k = 1; k <= 20; k++) {
          bool ok = true;
          int c = 0, d = 0, e = 0;
          for (int l = 0; l < a; l++) {
            int in = 0;
            for (int m = 0; A[l][m]; m++) {
              if (A[l][m] != '.')
                break;
              in++;
            }
            if (c * i + d * j + e * k != in)
              ok = false;
            for (int m = 0; A[l][m]; m++) {
              if (A[l][m] == '(')
                c++;
              if (A[l][m] == ')')
                c--;
              if (A[l][m] == '{')
                d++;
              if (A[l][m] == '}')
                d--;
              if (A[l][m] == '[')
                e++;
              if (A[l][m] == ']')
                e--;
            }
          }
          if (!ok)
            continue;
          c = 0;
          d = 0;
          e = 0;
          for (int l = 0; l < b; l++) {
            ans[l][c * i + d * j + e * k] = true;
            for (int m = 0; B[l][m]; m++) {
              if (B[l][m] == '(')
                c++;
              if (B[l][m] == ')')
                c--;
              if (B[l][m] == '{')
                d++;
              if (B[l][m] == '}')
                d--;
              if (B[l][m] == '[')
                e++;
              if (B[l][m] == ']')
                e--;
            }
          }
        }
      }
    }
    for (int i = 0; i < b; i++) {
      if (i)
        printf(" ");
      int at = 0;
      int count = 0;
      for (int j = 0; j < 800001; j++) {
        if (ans[i][j]) {
          at = j;
          count++;
        }
      }
      if (count > 1)
        printf("-1");
      else
        printf("%d", at);
    }
    printf("\\n");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
char A[10][100];
char B[10][100];
bool ans[10][81];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < a; i++)
      scanf("%s", A[i]);
    for (int i = 0; i < b; i++)
      scanf("%s", B[i]);
    for (int i = 0; i < b; i++)
      for (int j = 0; j < 81; j++)
        ans[i][j] = false;
    for (int i = 1; i <= 20; i++) {
      for (int j = 1; j <= 20; j++) {
        for (int k = 1; k <= 20; k++) {
          bool ok = true;
          int c = 0, d = 0, e = 0;
          for (int l = 0; l < a; l++) {
            int in = 0;
            for (int m = 0; A[l][m]; m++) {
              if (A[l][m] != '.')
                break;
              in++;
            }
            if (c * i + d * j + e * k != in)
              ok = false;
            for (int m = 0; A[l][m]; m++) {
              if (A[l][m] == '(')
                c++;
              if (A[l][m] == ')')
                c--;
              if (A[l][m] == '{')
                d++;
              if (A[l][m] == '}')
                d--;
              if (A[l][m] == '[')
                e++;
              if (A[l][m] == ']')
                e--;
            }
          }
          if (!ok)
            continue;
          c = 0;
          d = 0;
          e = 0;
          for (int l = 0; l < b; l++) {
            ans[l][c * i + d * j + e * k] = true;
            for (int m = 0; B[l][m]; m++) {
              if (B[l][m] == '(')
                c++;
              if (B[l][m] == ')')
                c--;
              if (B[l][m] == '{')
                d++;
              if (B[l][m] == '}')
                d--;
              if (B[l][m] == '[')
                e++;
              if (B[l][m] == ']')
                e--;
            }
          }
        }
      }
    }
    for (int i = 0; i < b; i++) {
      if (i)
        printf(" ");
      int at = 0;
      int count = 0;
      for (int j = 0; j < 81; j++) {
        if (ans[i][j]) {
          at = j;
          count++;
        }
      }
      if (count > 1)
        printf("-1");
      else
        printf("%d", at);
    }
    printf("\\n");
  }
}