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

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 2000 * 100 + 11;

int arr[NMAX];
int n, m, p, q, k, a, b, c, ans;
string s, str;
char ch;
vector<pii> v;

void solve() {
  cin >> n;
  multiset<pii> pos, neg;

  for (int i = 0; i < n; i++) {
    cin >> s;
    int min_d = 0, end_v = 0, cur = 0;
    for (auto ch : s) {
      if (ch == '(') {
        cur++;
      } else {
        cur--;
      }
      min_d = min(cur, min_d);
      end_v = cur;
    }
    if (end_v >= 0) {
      pos.insert({abs(min_d), end_v});
    } else {
      neg.insert({-(end_v + abs(min_d)), abs(min_d)});
    }
  }

  int cur = 0;
  for (auto e : pos) {
    int d = e.ff;
    int val = e.ss;

    if (d <= cur and cur >= 0) {
      cur += val;
    } else {
      cout << "No";
      return;
    }
  }

  for (auto e : neg) {
    int val = e.ff;
    int d = e.ss;

    if (d <= cur and cur >= 0) {
      cur += (-val) - d;
    } else {
      cout << "No";
      return;
    }
  }
  if (cur == 0)
    cout << "Yes";
  else
    cout << "No";
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 2000 * 100 + 11;

int arr[NMAX];
int n, m, p, q, k, a, b, c, ans;
string s, str;
char ch;
vector<pii> v;
int val[NMAX];
int depth[NMAX];
int taken[NMAX];

void solve() {
  cin >> n;
  for (int i = 0; i <= NMAX - 10; i++) {
    depth[i] = 1e10;
  }

  for (int i = 0; i < n; i++) {
    cin >> s;
    int min_d = 0, end_v = 0, cur = 0;
    for (auto ch : s) {
      if (ch == '(') {
        cur++;
      } else {
        cur--;
      }
      min_d = min(cur, min_d);
      end_v = cur;
    }
    v.pb({abs(min_d), end_v});
  }
  sort(all(v));

  for (int i = 0; i < n; i++) {
    val[i] = v[i].ss;
    depth[i] = v[i].ff;
    // tr(i,val[i],depth[i]);
  }
  int done = 0;
  int k = 0;
  while (done != n) {
    int p = 0;
    for (int a = n; a >= 1; a /= 2) {
      // tr(p,a,depth[p+a],k);
      while (depth[p + a] <= k) {
        // tr(p);
        p += a;
      }
      // tr(p,a);
      if (p >= n) {
        p = n - 1;
      }
    }
    // tr(p);
    // cout<<p<<" ";
    // p--;
    if (p > n) {
      cout << "No";
    }
    while (p >= 0 and taken[p])
      p--;
    // cout<<p;
    if (p < 0 or p > n) {
      cout << "No";
      return;
    }
    taken[p] = 1;
    k += val[p];
    done++;
    // tr(done,k);
  }
  if (k != 0) {
    cout << "No";
    return;
  }
  cout << "Yes";
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}