Problem p03696 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_int.hpp>
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
///////////////////////////

string ca(string s) {
  if (s == "")
    return s;
  if (s.size() == 1)
    return s;
  bool b = false;
  rep(i, 0, s.size() - 2) {
    if (s.size() <= 1) {
      return s;
    }
    if (s[i] == '(' && s[i + 1] == ')') {
      s.erase(i, 2);
      i--;
      b = true;
    }
  }
  if (b) {
    return ca(s);
  } else {
    return s;
  }
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  string t = ca(s);
  // cout<<t<<endl;
  rep(i, 0, t.size() - 1) {
    if (t[i] == ')') {
      s = '(' + s;
    } else {
      s = s + ')';
    }
  }
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_int.hpp>
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
///////////////////////////

string ca(string s) {
  if (s == "")
    return s;
  if (s.size() == 1)
    return s;
  bool b = false;
  rep(i, 0, s.size() - 2) {
    if (s.size() <= 1) {
      return s;
    }
    if (s[i] == '(' && s[i + 1] == ')') {
      s.erase(i, 2);
      i--;
      b = true;
    }
  }
  if (b) {
    return ca(s);
  } else {
    return s;
  }
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  string t = ca(s);
  // cout<<t<<endl;
  if (t.size() > 0) {
    rep(i, 0, t.size() - 1) {
      if (t[i] == ')') {
        s = '(' + s;
      } else {
        s = s + ')';
      }
    }
  }
  cout << s << endl;
  return 0;
}