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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int ll
#define mod 100
#define gcd(m, n) __gcd(m, n)
#define rep(i, j, n) for (int i = j; i <= n; i++)
#define red(i, n, j) for (int i = n; i >= j; i--)
#define ME(x, y) memset(x, y, sizeof(x))
// int lcm(int a , int b){return a*b/gcd(a,b);}
// ll quickpow(ll a , ll b){ll
// ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} int
// euler1(int x){int ans=x;for(int
// i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return
// ans;} const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int
// n){ME(vis,true);int
// len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int
// j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return
// len}
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int, int>
#define fi first
#define se second
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld", &x);
const int N = 1e6 + 9;
const int maxn = 1e6 + 9;
const double esp = 1e-6;
char s[maxn];
int cnt;
struct node {
  int l, r;
} a[maxn];

bool cmp(node a, node b) {
  if (a.l >= a.r && b.l < b.r) { // 左括号多的放前面
    return true;
  }
  if (a.l < a.r && b.l >= b.r) { // 右括号多的放后面
    return false;
  }
  if (a.l >= a.r && b.l >= b.r) { // 同样是左括号多，右括号更少的放前面
    return a.r < b.r;
  }
  if (a.r >= a.l && b.r >= b.l) // 同样是右括号多，左括号更多的放前面
    return a.l > b.l;
}

void solve() {
  int n, ans = 0, q = 0;
  scanf("%lld", &n);
  rep(i, 1, n) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    q += len;
    stack<char> st;
    a[i].l = a[i].r = 0;
    rep(j, 1, len) {
      if (s[j] == '(') {
        a[i].l++;
      } else {
        if (a[i].l > 0) {
          a[i].l--;
          ans += 2;
        } else {
          a[i].r++;
        }
      }
    }
  }
  sort(a + 1, a + 1 + n, cmp);
  int l = 0;
  rep(i, 1, n) {
    int t = min(l, a[i].r);
    l -= t;
    ans += 2 * t;
    l += a[i].l;
  }
  if (ans == q)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

signed main() {
  // ios::sync_with_stdio(false);

  solve();
}


Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int ll
#define mod 100
#define gcd(m, n) __gcd(m, n)
#define rep(i, j, n) for (int i = j; i <= n; i++)
#define red(i, n, j) for (int i = n; i >= j; i--)
#define ME(x, y) memset(x, y, sizeof(x))
// int lcm(int a , int b){return a*b/gcd(a,b);}
// ll quickpow(ll a , ll b){ll
// ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} int
// euler1(int x){int ans=x;for(int
// i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return
// ans;} const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int
// n){ME(vis,true);int
// len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int
// j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return
// len}
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int, int>
#define fi first
#define se second
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld", &x);
const int N = 1e6 + 9;
const int maxn = 1e5 + 9;
const double esp = 1e-6;
char s[maxn];
int cnt;
struct node {
  int l, r;
} a[maxn];

bool cmp(node a, node b) {
  if (a.l >= a.r && b.l < b.r) { // 左括号多的放前面
    return true;
  }
  if (a.l < a.r && b.l >= b.r) { // 右括号多的放后面
    return false;
  }
  if (a.l >= a.r && b.l >= b.r) { // 同样是左括号多，右括号更少的放前面
    return a.r < b.r;
  }
  if (a.r >= a.l && b.r >= b.l) // 同样是右括号多，左括号更多的放前面
    return a.l > b.l;
}

void solve() {
  int n, ans = 0, q = 0;
  scanf("%lld", &n);
  rep(i, 1, n) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    q += len;
    stack<char> st;
    a[i].l = a[i].r = 0;
    rep(j, 1, len) {
      if (s[j] == '(') {
        a[i].l++;
      } else {
        if (a[i].l > 0) {
          a[i].l--;
          ans += 2;
        } else {
          a[i].r++;
        }
      }
    }
  }
  sort(a + 1, a + 1 + n, cmp);
  int l = 0;
  rep(i, 1, n) {
    int t = min(l, a[i].r);
    l -= t;
    ans += 2 * t;
    l += a[i].l;
  }
  if (ans == q)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

signed main() {
  // ios::sync_with_stdio(false);

  solve();
}