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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

bool f(const vector<pint> &data1, const vector<pint> &data2) {
  int sum1 = 0, sum2 = 0;
  REP(i, data1.size()) {
    if (sum1 - data1[i].first < 0)
      return false;
    sum1 += data1[i].second;
  }
  REP(i, data2.size()) {
    if (sum2 - data2[i].first < 0)
      return false;
    sum2 += data2[i].second;
  }
  //  cout << sum1 << ";" << sum2 << endl;
  return sum1 == sum2;
}

int main() {
  int N;
  cin >> N;
  vector<string> S(N);
  REP(i, N) cin >> S[i];
  vector<pint> data1, data2;
  REP(i, N) {
    int cnt = 0;
    REP(j, S[i].length()) {
      if (S[i][j] == '(')
        cnt++;
      else
        cnt--;
    }
    const int diff = cnt;
    cnt = 0;
    int min_v = 0;
    if (diff >= 0) {
      REP(j, S[i].length()) {
        if (S[i][j] == '(')
          cnt++;
        else
          cnt--;
        chmin(min_v, cnt);
      }
      data1.push_back(make_pair(-min_v, diff));
    } else {
      for (int j = S[i].length() - 1; j >= 0; j--) {
        if (S[i][j] == ')')
          cnt++;
        else
          cnt--;
        chmin(min_v, cnt);
      }
      data2.push_back(make_pair(-min_v, -diff));
    }
  }
  sort(data1.begin(), data1.end());
  sort(data2.begin(), data2.end());
#ifdef DEBUG
  REP(i, data1.size()) {
    cout << data1[i].first << "," << data1[i].second << endl;
  }
  REP(i, data2.size()) {
    cout << data2[i].first << ":" << data2[i].second << endl;
  }
#endif
  bool ret = f(data1, data2);
  YesNo(ret);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

bool f(const vector<pint> &data1, const vector<pint> &data2) {
  int sum1 = 0, sum2 = 0;
  REP(i, data1.size()) {
    if (sum1 + data1[i].first < 0)
      return false;
    sum1 += data1[i].second;
  }
  REP(i, data1.size()) {
    if (sum2 + data2[i].first < 0)
      return false;
    sum2 += data2[i].second;
  }
  return sum1 == sum2;
}

int main() {
  int N;
  cin >> N;
  vector<string> S(N);
  REP(i, N) cin >> S[i];
  vector<pint> data1, data2;
  REP(i, N) {
    int cnt = 0;
    REP(j, S[i].length()) {
      if (S[i][j] == '(')
        cnt++;
      else
        cnt--;
    }
    const int diff = cnt;
    cnt = 0;
    int min_v = 0;
    if (diff >= 0) {
      REP(j, S[i].length()) {
        if (S[i][j] == '(')
          cnt++;
        else
          cnt--;
        chmin(min_v, cnt);
      }
      data1.push_back(make_pair(min_v, diff));
    } else {
      for (int j = S[i].length() - 1; j >= 0; j--) {
        if (S[i][j] == ')')
          cnt++;
        else
          cnt--;
        chmin(min_v, cnt);
      }
      data2.push_back(make_pair(min_v, -diff));
    }
  }
  sort(data1.begin(), data1.end());
  sort(data2.begin(), data2.end());
  bool ret = f(data1, data2);
  YesNo(ret);
  return 0;
}