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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

unsigned int N;
string S;
// vector<pair<int, int>> sp, sm;
struct p {
  int mv;
  int lv;
};
multiset<p> sp, sm;
bool operator<(const p &lhs, const p &rhs) {
  if (lhs.mv > rhs.mv)
    return true;
  if (lhs.mv == rhs.mv && lhs.lv > rhs.lv)
    return true;
  return false;
}

void ldiff(string, int &, int &);

int main() {
  ll diff = 0;
  ll depth = 0;
  int s1, s2;
  p s;
  unsigned int i;

  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> S;
    ldiff(S, s1, s2);
    s.lv = s1;
    s.mv = s2;
    diff += s.lv;
    if (s.lv >= 0) {
      sp.emplace(s);
    } else {
      s.mv -= s.lv;
      s.lv = -s.lv;
      sm.emplace(s);
    }
  }

  if (diff != 0) {
    printf("No\\n");
    return 0;
  }

  for (auto it = sp.begin(); it != sp.end(); it++) {
    //    printf("%d %d\\n", it->lv, it->mv);
    if (depth + it->mv < 0) {
      printf("No\\n");
      return 0;
    }
    depth += it->lv;
  }

  depth = 0;
  for (auto it = sm.begin(); it != sm.end(); it++) {
    //    printf("%d %d\\n", it->lv, it->mv);
    if (depth + it->mv < 0) {
      printf("No\\n");
      return 0;
    }
    depth += it->lv;
  }

  printf("Yes\\n");

  return 0;
}
void ldiff(string s, int &ld, int &md) {
  int len = s.size();

  ld = 0;
  md = 0;
  for (int i = 0; i < len; i++) {
    switch (s[i]) {
    case '(':
      ld++;
      break;
    case ')':
      ld--;
      break;
    }
    if (ld < md)
      md = ld;
  }
}
/*
  ���ʗ񂪂ł������
  �J�����ʂ̐��ƕ����ʂ̐�������
  �J�����ʂ�+1�A�����ʂ�-1�Ƃ��ĕ�����̓r���ŕ��ɂȂ�Ȃ�
  */


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

unsigned int N;
vector<string> S;
vector<pair<int, int>> sp, sm;

void ldiff(string, int &, int &);

bool compare(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.second > b.second)
    return true;
  if (a.first > b.first)
    return true;
  return false;
}
int main() {
  ll diff = 0;
  ll depth = 0;
  int s1, s2;
  unsigned int i;

  cin >> N;
  S.resize(N);
  for (i = 0; i < N; i++) {
    cin >> S[i];
    ldiff(S[i], s1, s2);
    diff += s1;
    if (s1 >= 0) {
      sp.push_back(make_pair(s1, s2));
    } else {
      sm.push_back(make_pair(s1, s2));
    }
  }

  if (diff != 0) {
    printf("No\\n");
    return 0;
  }

  sort(sp.begin(), sp.end(), compare);
  sort(sm.begin(), sm.end(), compare);

  for (i = 0; i < sp.size(); i++) {
    //    printf("%d %d\\n", sp[i].first, sp[i].second);
    if (depth + sp[i].second < 0) {
      printf("No\\n");
      return 0;
    }
    depth += sp[i].first;
  }
  for (i = 0; i < sm.size(); i++) {
    //    printf("%d %d\\n", sm[i].first, sm[i].second);
    if (depth + min(sm[i].first, sm[i].second) < 0) {
      printf("No\\n");
      return 0;
    }
    depth += sm[i].first;
  }

  printf("Yes\\n");

  return 0;
}
void ldiff(string s, int &ld, int &md) {
  int len = s.size();

  ld = 0;
  md = 0;
  for (int i = 0; i < len; i++) {
    switch (s[i]) {
    case '(':
      ld++;
      break;
    case ')':
      ld--;
      break;
    }
    if (ld < md)
      md = ld;
  }
}
/*
  ���ʗ񂪂ł������
  �J�����ʂ̐��ƕ����ʂ̐�������
  �J�����ʂ�+1�A�����ʂ�-1�Ƃ��ĕ�����̓r���ŕ��ɂȂ�Ȃ�
  */