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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vectors<decltype(inner)>(x, inner);
}
template <class T> void show_with_brank(T a) {
  int cnt = 0;
  for (auto itr : a) {
    if (cnt++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    vector<string> ans;
    rep(i, n) cin >> s[i];
    rep(i, n) rep(j, s[i].size()) if (s[i][j] == '.') s[i][j] = ' ';
    rep(i, n) {
      rep(j, (int)s[i].size() - 1) if (s[i][j] == ' ' && s[i][j + 1] != ' ')
          s[i][j] = '+';
      ans.push_back(s[i]);
    }
    int maxi = 0;
    rep(i, n) maxi = max(maxi, (int)s[i].size());
    rep(i, n) {
      for (int j = s[i].size(); j < maxi; j++)
        ans[i].push_back(' ');
    }
    rep(i, ans.size()) rep(j, ans[i].size()) {
      if (ans[i][j] == '+') {
        int y = i;
        int cnt = 1;
        while (true) {
          if (y + cnt >= n) {
            cnt = INF;
            break;
          }
          if (ans[y + cnt][j] == '+')
            break;
          if (ans[y + cnt][j] != ' ') {
            cnt = INF;
            break;
          }
          cnt++;
        }
        if (cnt == INF)
          continue;
        for (int y = i + 1; y < i + cnt; y++) {
          ans[y][j] = '|';
        }
      }
    }
    rep(i, n) {
      rep(j, s[i].size()) cout << ans[i][j];
      cout << endl;
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vectors<decltype(inner)>(x, inner);
}
template <class T> void show_with_brank(T a) {
  int cnt = 0;
  for (auto itr : a) {
    if (cnt++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    vector<string> ans;
    rep(i, n) cin >> s[i];
    rep(i, n) rep(j, s[i].size()) if (s[i][j] == '.') s[i][j] = ' ';
    rep(i, n) {
      rep(j, (int)s[i].size() - 1) if (s[i][j] == ' ' && s[i][j + 1] != ' ')
          s[i][j] = '+';
      ans.push_back(s[i]);
    }
    int maxi = 0;
    rep(i, n) maxi = max(maxi, (int)s[i].size());
    rep(i, n) {
      for (int j = s[i].size(); j < maxi; j++)
        ans[i].push_back(' ');
    }
    rep(i, ans.size()) rep(j, ans[i].size()) {
      if (ans[i][j] == '+') {
        int y = i;
        int cnt = 1;
        while (true) {
          if (ans[y + cnt][j] == '+')
            break;
          if (y + cnt >= n || ans[y + cnt][j] != ' ') {
            cnt = INF;
            break;
          }
          cnt++;
        }
        if (cnt == INF)
          continue;
        for (int y = i + 1; y < i + cnt; y++) {
          ans[y][j] = '|';
        }
      }
    }
    rep(i, n) {
      rep(j, s[i].size()) cout << ans[i][j];
      cout << endl;
    }
  }
}