Problem p03696 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define BEGIN(x) x.begin()
#define END(x) x.end()
#define ALL(x) BEGIN(x), END(x)
#define PAIR make_pair
#define VEC(type) vector<type>
#define endl '\n'
#define FIR first
#define SEC second
#define PB push_back
#define SZ(x) ((int)x.size())

#define FOR(i, A, B) for (auto i = (A); i != (B); i++)
#define FORD(i, A, B) for (auto i = (A); i != (B); i--)

#define READRANGE(begin, end) FOR(it, begin, end) cin >> *it
#define READVEC(V) READRANGE(BEGIN(V), END(V))

using namespace std;

typedef long long lint;
typedef pair<int, int> Pii;
typedef pair<int, lint> Pil;
typedef pair<lint, lint> Pll;
typedef pair<lint, int> Pli;

template <class U, class V> istream &operator>>(istream &in, pair<U, V> &pr) {
  return in >> pr.first >> pr.second;
}

template <class U, class V> ostream &operator<<(ostream &out, pair<U, V> &p) {
  return out << p.first << " " << p.second;
}

template <class T> ostream &operator<<(ostream &out, vector<T> vec) {
  for (T &e : vec)
    out << e << " ";
  return out << endl;
}

// ---- BEGIN LIBRARY CODE ----
// ---- END LIBRARY CODE ----
void io_init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
};

string dp[256][128];
string S;

string mins(string s, string t) {
  int a = SZ(s), b = SZ(t);
  if (a < b)
    return s;
  else if (a > b)
    return t;
  return min(s, t);
};

string f(int i, int depth) {
  if (i == SZ(S))
    return string(depth, ')');

  string &ans = dp[depth][i];
  if (ans != "a")
    return ans;

  ans = string(400, ' ');
  if (depth < 250)
    ans = mins(ans, "(" + f(i, depth + 1));

  if (depth > 0)
    ans = mins(ans, ")" + f(i, depth - 1));

  if (depth > 0 && S[i] == ')')
    ans = mins(ans, ")" + f(i + 1, depth - 1));

  if (S[i] == '(')
    ans = mins(ans, "(" + f(i + 1, depth + 1));
  return ans;
};

void solveSingleCase(int caseNo) {
  int N;
  cin >> N;
  cin >> S;

  FOR(i, 0, 256)
  FOR(j, 0, 128)
  dp[i][j] = "a";

  cout << f(0, 0) << endl;
};

int main(int argc, char **argv) {
  io_init();

  // Actual program code starts here.
  int T = 1;
  // cin >> T;
  FOR(tcase, 1, T + 1) { solveSingleCase(tcase); }
  return 0;
};


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define BEGIN(x) x.begin()
#define END(x) x.end()
#define ALL(x) BEGIN(x), END(x)
#define PAIR make_pair
#define VEC(type) vector<type>
#define endl '\n'
#define FIR first
#define SEC second
#define PB push_back
#define SZ(x) ((int)x.size())

#define FOR(i, A, B) for (auto i = (A); i != (B); i++)
#define FORD(i, A, B) for (auto i = (A); i != (B); i--)

#define READRANGE(begin, end) FOR(it, begin, end) cin >> *it
#define READVEC(V) READRANGE(BEGIN(V), END(V))

using namespace std;

typedef long long lint;
typedef pair<int, int> Pii;
typedef pair<int, lint> Pil;
typedef pair<lint, lint> Pll;
typedef pair<lint, int> Pli;

template <class U, class V> istream &operator>>(istream &in, pair<U, V> &pr) {
  return in >> pr.first >> pr.second;
}

template <class U, class V> ostream &operator<<(ostream &out, pair<U, V> &p) {
  return out << p.first << " " << p.second;
}

template <class T> ostream &operator<<(ostream &out, vector<T> vec) {
  for (T &e : vec)
    out << e << " ";
  return out << endl;
}

// ---- BEGIN LIBRARY CODE ----
// ---- END LIBRARY CODE ----
void io_init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
};

string dp[256][128];
string S;

string mins(string s, string t) {
  int a = SZ(s), b = SZ(t);
  if (a < b)
    return s;
  else if (a > b)
    return t;
  return min(s, t);
};

string f(int i, int depth) {
  if (depth > 250)
    return string(400, 'a');

  if (i == SZ(S))
    return string(depth, ')');

  string &ans = dp[depth][i];
  if (ans != "a")
    return ans;

  ans = string(400, ' ');
  if (depth < 250)
    ans = mins(ans, "(" + f(i, depth + 1));

  if (depth > 0)
    ans = mins(ans, ")" + f(i, depth - 1));

  if (depth > 0 && S[i] == ')')
    ans = mins(ans, ")" + f(i + 1, depth - 1));

  if (S[i] == '(')
    ans = mins(ans, "(" + f(i + 1, depth + 1));
  return ans;
};

void solveSingleCase(int caseNo) {
  int N;
  cin >> N;
  cin >> S;

  FOR(i, 0, 256)
  FOR(j, 0, 128)
  dp[i][j] = "a";

  cout << f(0, 0) << endl;
};

int main(int argc, char **argv) {
  io_init();

  // Actual program code starts here.
  int T = 1;
  // cin >> T;
  FOR(tcase, 1, T + 1) { solveSingleCase(tcase); }
  return 0;
};