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

#include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

pair<long, long> MakePairValue(string s) {
  // a : 最終値
  // b : 制約
  long a = 0, b = 10000000; // S <= 1000000 なので余裕持って
  for (auto c : s) {
    if (c == '(') {
      a++;
    } else {
      a--;
      if (b > a && a < 0)
        b = a;
    }
  }
  return pair<long, long>(b, a);
}

// first で降順、second で降順ソート
bool compF(pair<long, long> a, pair<long, long> b) {
  if (a.first > b.first) {
    return true;
  } else if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return false;
  }
}

// second - first で降順、second で降順ソート
bool compFMinus(pair<long, long> a, pair<long, long> b) {
  long ta = a.second - a.first;
  long tb = b.second - b.first;
  if (ta > tb) {
    return true;
  } else if (ta == tb) {
    return a.second > b.second;
  } else {
    return false;
  }
}

// first 降順
bool compFAdd(pair<long, long> a, pair<long, long> b) {
  if (a.first > b.first)
    return true;
  return false;
}

int main() {
  long N;
  cin >> N;

  vector<pair<long, long>> pv;
  vector<pair<long, long>> pvMinus, pvAdd;
  pv.reserve(N);
  pvMinus.reserve(N);
  pvAdd.reserve(N);

  for (size_t i = 0; i < N; i++) {
    string s;
    cin >> s;
    auto a = MakePairValue(s);
    if (a.first < 0) {
      if (a.second > 0)
        pvAdd.push_back(a);
      else
        pvMinus.push_back(a);
    } else {
      pv.push_back(a);
    }
  }

  sort(pv.begin(), pv.end(), compF);
  sort(pvMinus.begin(), pvMinus.end(), compFMinus);
  sort(pvAdd.begin(), pvAdd.end(), compFAdd);

  // 前のほうが制約が緩く、最終値が大きい

  long long sum = 0;
  bool b = true;
  for (auto e : pv) {
    sum += e.second;
  }
  for (auto e : pvAdd) {
    if (sum < -e.first) {
      b = false;
      break;
    }
    sum += e.second;
  }
  if (b) {
    for (auto e : pvMinus) {
      if (sum < -e.first) {
        b = false;
        break;
      }
      sum += e.second;
    }
  }
  if (b && sum == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

pair<long, long> MakePairValue(string s) {
  // a : 最終値
  // b : 制約
  long a = 0, b = 10000000; // S <= 1000000 なので余裕持って
  for (auto c : s) {
    if (c == '(') {
      a++;
    } else {
      a--;
      if (b > a && a < 0)
        b = a;
    }
  }
  return pair<long, long>(b, a);
}

// first で降順、second で降順ソート
bool compF(pair<long, long> a, pair<long, long> b) {
  if (a.first > b.first) {
    return true;
  } else if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return false;
  }
}

// second - first で降順、second で降順ソート
bool compFMinus(pair<long, long> a, pair<long, long> b) {
  long ta = a.second - a.first;
  long tb = b.second - b.first;
  if (ta > tb) {
    return true;
  } else if (ta == tb) {
    return a.second > b.second;
  } else {
    return false;
  }
}

// first 昇順
bool compFAdd(pair<long, long> a, pair<long, long> b) {
  if (a.first <= b.first)
    return true;
  return false;
}

int main() {
  long N;
  cin >> N;

  vector<pair<long, long>> pv;
  vector<pair<long, long>> pvMinus, pvAdd;
  pv.reserve(N);
  pvMinus.reserve(N);
  pvAdd.reserve(N);

  for (size_t i = 0; i < N; i++) {
    string s;
    cin >> s;
    auto a = MakePairValue(s);
    if (a.first < 0) {
      if (a.second > 0)
        pvAdd.push_back(a);
      else
        pvMinus.push_back(a);
    } else {
      pv.push_back(a);
    }
  }

  sort(pv.begin(), pv.end(), compF);
  sort(pvMinus.begin(), pvMinus.end(), compFMinus);
  sort(pvAdd.begin(), pvAdd.end(), compFAdd);

  // 前のほうが制約が緩く、最終値が大きい

  long long sum = 0;
  bool b = true;
  for (auto e : pv) {
    sum += e.second;
  }
  for (auto e : pvAdd) {
    if (sum < -e.first) {
      b = false;
      break;
    }
    sum += e.second;
  }
  if (b) {
    for (auto e : pvMinus) {
      if (sum < -e.first) {
        b = false;
        break;
      }
      sum += e.second;
    }
  }
  if (b && sum == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}