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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  cin >> n;
  vs s(n);
  REP(i, n) cin >> s[i];
  vector<pii> v;
  REP(i, n) {
    int l = 0, r = 0;
    REP(j, s[i].size()) {
      if (s[i][j] == '(')
        r++;
      else {
        if (r > 0)
          r--;
        else
          l++;
      }
    }
    v.emplace_back(l, r);
  }
  bool f = true;
  REP(i, n) {
    if (v[i].first != 0 || v[i].second != 0)
      f = false;
  }
  if (f) {
    puts("Yes");
    return 0;
  }
  int lmax = 0, l = -1, rmax = 0, r = -1;
  REP(i, n) {
    if (v[i].first == 0 && v[i].second == 0)
      continue;
    if (v[i].first == 0) {
      if (v[i].second > rmax) {
        rmax = v[i].second;
        r = i;
      }
    } else if (v[i].second == 0) {
      if (v[i].first > lmax) {
        lmax = v[i].first;
        l = i;
      }
    }
  }

  if (l == -1 || r == -1) {
    puts("No");
    return 0;
  }
  vector<bool> flag(n);
  flag[l] = flag[r] = true;
  int tmp = rmax;
  while (1) {
    int ti = -1, tmax = -INF;
    REP(i, n) {
      if (flag[i] || v[i].first > tmp)
        continue;
      if (v[i].second - v[i].first > tmax ||
          (v[i].second - v[i].first == tmax && v[i].first > v[ti].first)) {
        ti = i;
        tmax = v[ti].second - v[ti].first;
      }
    }
    if (ti == -1 || tmax < 0)
      break;
    tmp += tmax;
    flag[ti] = true;
  }
  while (1) {
    int ti = -1, tmax = -INF;
    REP(i, n) {
      if (flag[i] || v[i].first > tmp)
        continue;
      if (tmax < v[i].first) {
        ti = i;
        tmax = v[i].first;
      }
    }
    if (ti == -1)
      break;
    tmp += v[ti].second - v[ti].first;
    flag[ti] = true;
  }

  f = true;
  REP(i, n) if (!flag[i]) f = false;
  if (tmp != lmax)
    f = false;
  if (f)
    puts("Yes");
  else
    puts("No");
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  cin >> n;
  vs s(n);
  REP(i, n) cin >> s[i];
  vector<pii> v;
  REP(i, n) {
    int l = 0, r = 0;
    REP(j, s[i].size()) {
      if (s[i][j] == '(')
        r++;
      else {
        if (r > 0)
          r--;
        else
          l++;
      }
      //	cout<<l << " " << r<<endl;
    }
    //	cout<<endl;
    //	cout<<l << " " << r<<endl;
    //	v.emplace_back(l, r);
  }
  bool f = true;
  REP(i, n) {
    cout << v[i].first << " " << v[i].second << endl;
    if (v[i].first != 0 || v[i].second != 0)
      f = false;
  }
  if (f) {
    puts("Yes");
    return 0;
  }
  int lmax = 0, l = -1, rmax = 0, r = -1;
  REP(i, n) {
    if (v[i].first == 0 && v[i].second == 0)
      continue;
    if (v[i].first == 0) {
      if (v[i].second > rmax) {
        rmax = v[i].second;
        r = i;
      }
    } else if (v[i].second == 0) {
      if (v[i].first > lmax) {
        lmax = v[i].first;
        l = i;
      }
    }
  }
  //	cout<<l << " " << r<<endl;

  if (l == -1 || r == -1) {
    puts("No");
    return 0;
  }
  vector<bool> flag(n);
  flag[l] = flag[r] = true;
  int tmp = rmax;
  bool update = true;
  while (update) {
    update = false;
    int tmax = -1, ti;
    REP(i, n) {
      if (flag[i])
        continue;
      if (v[i].first <= tmp && v[i].second - v[i].first > tmax) {
        tmax = v[i].second - v[i].first;
        ti = i;
        update = true;
      }
    }
    if (!update) {
      puts("No");
      return 0;
    }
    tmp += tmax;
    flag[ti] = true;
  }
  f = true;
  REP(i, n) if (!flag[i]) f = false;
  if (tmp != lmax)
    f = false;
  if (f)
    puts("Yes");
  else
    puts("No");
}