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

#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std;
int main(void) {
  int i, a, b[1001], c[1001], j, flg = 0, tt, ht, ba, tn = 1, h;
  scanf("%d", &a);
  while (a != 0) {
    ba = 0;
    tn = 1;
    h = 1;
    for (i = 0; i <= 1001; i++) {
      c[i] = 1000000;
      b[i] = 1000000;
    }
    ht = a;
    tt = a;
    for (i = 1; i <= a; i++) {
      scanf("%d", &b[i]);
    }
    for (i = 1; i <= a * 2; i++) {
      flg = 0;
      for (j = 1; j <= a; j++) {
        if (i == b[j]) {
          flg = 1;
        }
      }
      if (flg != 1) {
        c[h] = i;
        h++;
      }
    }
    sort(b + 1, b + a + 1);
    sort(c + 1, c + a + 1);
    while (tt != 0 && ht != 0) {
      flg = 0;
      if (tn % 2 != 0) {
        if (ba == 0) {
          sort(b, b + a);
          ba = b[0];
          b[0] = 1000000;
          tt--;
        } else {
          for (i = 1; i <= a; i++) {
            if (ba < b[i] && b[i] <= a * 2) {
              ba = b[i];
              b[i] = 100000;
              tt--;
              flg = 1;
              i = a;
            }
          }
          if (flg == 0) {
            ba = 0;
          }
        }
      } else {
        if (ba == 0) {
          sort(c, c + a);
          ba = c[0];
          c[0] = 1000000;
          ht--;
        } else {
          for (i = 1; i <= a; i++) {
            if (ba < c[i] && c[i] <= a * 2) {
              ba = c[i];
              c[i] = 100000;
              ht--;
              flg = 1;
              i = a;
            }
          }
          if (flg == 0) {
            ba = 0;
          }
        }
      }
      tn++;
    }
    printf("%d\\n", ht);
    printf("%d\\n", tt);
    scanf("%d", &a);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std;
int main(void) {
  int i, a, b[100], c[100], j, flg = 0, tt, ht, ba, tn = 1;
  scanf("%d", &a);
  while (a != 0) {
    ba = 0;
    tn = 1;
    for (i = 0; i <= a * 2; i++) {
      c[i] = 1000000;
      b[i] = 1000000;
    }
    ht = a;
    tt = a;
    for (i = 1; i <= a; i++) {
      scanf("%d", &b[i]);
    }
    for (i = 1; i <= a * 2; i++) {
      flg = 0;
      for (j = 1; j <= a; j++) {
        if (i == b[j]) {
          flg = 1;
        }
      }
      if (flg != 1) {
        c[i / 2] = i;
      }
    }
    sort(b, b + a * 2);
    sort(c, c + a * 2);
    while (tt != 0 && ht != 0) {
      flg = 0;
      if (tn % 2 != 0) {
        if (ba == 0) {
          sort(b, b + a);
          ba = b[0];
          b[0] = 1000000;
          tt--;
        } else {
          for (i = 0; i < a; i++) {
            if (ba < b[i] && b[i] <= a * 2) {
              ba = b[i];
              b[i] = 100000;
              tt--;
              flg = 1;
              i = a;
            }
          }
          if (flg == 0) {
            ba = 0;
          }
        }
      } else {
        if (ba == 0) {
          sort(c, c + a);
          ba = c[0];
          c[0] = 1000000;
          ht--;
        } else {
          for (i = 0; i < a; i++) {
            if (ba < c[i] && c[i] <= a * 2) {
              ba = c[i];
              c[i] = 100000;
              ht--;
              flg = 1;
              i = a;
            }
          }
          if (flg == 0) {
            ba = 0;
          }
        }
      }
      tn++;
    }
    printf("%d\\n", ht);
    printf("%d\\n", tt);
    scanf("%d", &a);
  }
  return 0;
}