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

#include <algorithm>
#include <iostream>
#include <string>
class kuohao {
public:
  int must_have = 0;
  int this_num = 0;
  int countl = 0;
  bool operator<(const kuohao &a) {
    if (this->must_have > 0 && a.must_have > 0) {
      if (this->this_num < 0 && a.this_num < 0) {
        return this->countl > a.countl;
      }
      return this->this_num > a.this_num;
    }
    return (this->must_have) < (a.must_have);
  }
};
int main() {
  int n;
  std::cin >> n;
  std::string strs[n];
  kuohao k[n];
  for (int i = 0; i < n; ++i) {
    std::cin >> strs[i];
  }
  for (int i = 0; i < n; ++i) {
    int temp = strs[i].size();
    int count = 0;
    int countl = 0;
    int countMH = 0;
    for (int j = 0; j < temp; ++j) {
      if (strs[i][j] == '(') {
        ++count;
        ++countl;
      }
      if (strs[i][j] == ')') {
        --count;
        --countl;
      }
      if (count < countMH) {
        ++(k[i].must_have);
        countMH = count;
        countl = 0;
      }
    }
    k[i].this_num = count;
    k[i].countl = countl;
  }
  std::sort(k, k + n);
  int count = 0;
  for (int i = 0; i < n; ++i) {
    if (count >= k[i].must_have) {
      count += (k[i].this_num);
    } else {
      int j;
      for (j = i + 1; j < n; ++j) {
        if (count >= k[j].must_have && k[j].must_have != -1) {
          if (k[j].this_num < 0) {
            std::cout << "No" << std::endl;
            return 0;
          }
          count += (k[j].this_num);
          k[j].must_have = -1;
          k[j].this_num = 0;
          --i;
          break;
        }
      }
      if (j >= n) {
        std::cout << "No" << std::endl;
        return 0;
      }
    }
  }
  if (count == 0) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
class kuohao {
public:
  int must_have = 0;
  int this_num = 0;
  bool operator<(const kuohao &a) { return (this->this_num) < (a.this_num); }
};
int main() {
  int n;
  std::cin >> n;
  std::string strs[n];
  kuohao k[n];
  bool b[n];
  for (int i = 0; i < n; ++i) {
    std::cin >> strs[i];
    b[i] = false;
  }
  for (int i = 0; i < n; ++i) {
    int temp = strs[i].size();
    int count = 0;
    int countMH = 0;
    for (int j = 0; j < temp; ++j) {
      if (strs[i][j] == '(') {
        ++count;
      }
      if (strs[i][j] == ')') {
        --count;
      }
      if (count < countMH) {
        ++(k[i].must_have);
        countMH = count;
      }
    }
    k[i].this_num = count;
  }
  std::sort(k, k + n);
  int count = 0;
  int test = 0;
  std::vector<int> ls[100000] = {};
  for (int i = n - 1; i >= 0; ++i) {
    for (int j = k[i].must_have; j < 100000; ++j) {
      ls[j].push_back(i);
    }
  }
  while (test < n) {
    int hold = test;
    for (int i = 0; i < ls[count].size(); ++i) {
      if (!(b[ls[count][i]])) {
        b[ls[count][i]] = true;
        count += k[ls[count][i]].this_num;
        test++;
        break;
      }
    }
    if (hold == test) {
      std::cout << "NO" << std::endl;
      return 0;
    }
  }
  if (count == 0) {
    std::cout << "YES" << std::endl;
  } else {
    std::cout << "NO" << std::endl;
  }
  return 0;
}