alexjercan/bugnet huggingface dataset:
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pll pair<LL, LL>
#define pii pair<int, int>
#define X first
#define Y second
#define MAXN 1000010 // 1e3
#define lson l, mid, (rt << 1)
#define rson mid + 1, r, (rt << 1 | 1)
const double eps = 1e-10;
int n;
vector<int> root, edge[MAXN];
LL value[MAXN], cost[MAXN];
bool cmp(int i, int j) { return value[i] < value[j]; }
void init() {
  root.clear();
  for (int i = 0; i <= n; i++) {
    edge[i].clear();
    cost[i] = -1;
  }
}
void solve(int rt) {
  queue<int> q;
  stack<int> s;
  q.push(rt);
  s.push(rt);
  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    int len = edge[tmp].size();
    for (int i = 0; i < len; i++) {
      q.push(edge[tmp][i]);
      s.push(edge[tmp][i]);
    }
  }
  while (!s.empty()) {
    int tmp = s.top();
    s.pop();
    sort(edge[tmp].begin(), edge[tmp].end(), cmp);
    int len = edge[tmp].size();
    cost[tmp] = 0LL;
    LL res = 0;
    for (int i = 0; i < len; i++) {
      cost[tmp] += res + cost[edge[tmp][i]];
      res += value[edge[tmp][i]];
    }
    value[tmp] += res;
  }
}
int main() {
  while (~scanf("%d", &n)) {
    init();
    int x;
    for (int i = 0; i < n; i++) {
      scanf("%lld%d", &value[i], &x);
      if (x == -1)
        root.push_back(i);
      else
        edge[x].push_back(i);
    }
    int len = root.size();
    LL ans = 0;
    for (int i = 0; i < len; i++) {
      solve(root[i]);
      ans += cost[root[i]];
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

const int maxn = 1e6 + 5;
int a[maxn][2], b[maxn], bl;
vector<int> e[maxn];
vector<int> te[maxn];
long long tot = 0;
long long dfs(int node) {
  if (e[node].size() == 0)
    return a[node][0];
  // printf("good");
  long long temp;
  long long sum = 0, sum1 = 0;
  long long mx = 0;
  int l = e[node].size();
  for (int i = 0; i < l; i++) {
    temp = dfs(e[node][i]);
    te[node].push_back(temp);
    sum += temp;
    if (temp > mx)
      mx = temp;
  }
  sort(te[node].begin(), te[node].end());
  for (int i = 0; i < l; i++) {
    sum1 += te[node][i] * (l - i - 1);
  }
  tot += sum1;
  return sum + a[node][0];
}

int main() {
  int n;
  scanf("%d", &n);
  bl = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a[i][0], &a[i][1]);
    if (a[i][1] != -1)
      e[a[i][1]].push_back(i);
    else
      b[bl++] = i;
  }
  for (int j = 0; j < bl; j++) {
    dfs(b[j]);
  }
  printf("%lld\\n", tot);
  return 0;
}
Problem p01574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n, m;
  cin >> n >> m;

  Int g = n;
  for (Int i = 0; i < m; i++) {
    Int x;
    cin >> x;
    g = __gcd(g, x);
  }

  cout << (g == 1 ? "Yes" : "No") << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n, m;
  cin >> n >> m;
  auto check = [](Int n, Int x) {
    if (__gcd(n, x) == 1) {
      cout << "Yes" << endl;
      exit(0);
    }
  };

  vector<int> v;
  for (Int i = 0; i < m; i++) {
    Int x;
    cin >> x;
    check(n, x);
    if (x < n)
      v.emplace_back(x);
  }

  sort(v.begin(), v.end());

  function<void(Int)> dfs = [&](Int x) {
    for (Int i : v) {
      if (i >= x)
        break;
      for (Int j : v) {
        if (j >= i)
          break;
        check(i, j);
      }
      dfs(i);
    }
  };

  dfs(n);
  cout << "No" << endl;
  return 0;
}
Problem p01574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, L;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    cin >> L;
    N = __gcd(L, N);
  }

  if (N == 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  long long L[M];
  for (int i = 0; i < M; i++)
    cin >> L[i];
  bool ok = M == 1;
  for (int i = 0; i < M - 1; i++) {
    for (int j = i + 1; j < M; j++) {
      if (__gcd(L[i], L[j]) == 1)
        ok = 1;
    }
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p01600 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define x first
#define y second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

ll dp[1002][1002], K[1002][1002];

int main(void) {

  int n;
  cin >> n;

  vector<pii> v(n);
  rep(i, n) cin >> v[i].x >> v[i].y;

  fill(dp[0], dp[1001], INT_MAX);
  fill(K[0], K[1001], -1);
  rep(i, 1001) dp[i][i] = 0, K[i][i] = i;

  for (int w = 1; w < n; w++) {
    for (int i = 0; i + w <= n; i++) {
      for (int s = K[i][i + w - 1]; s <= K[i + 1][i + w]; s++) {
        int cost = abs(v[i].x - v[s + 1].x) + abs(v[s].y - v[i + w].y);
        if (dp[i][i + w] > dp[i][s] + dp[s + 1][i + w] + cost) {
          dp[i][i + w] = dp[i][s] + dp[s + 1][i + w] + cost;
          K[i][i + w] = s;
        }
      }
    }
  }

  cout << dp[0][n - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define x first
#define y second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

ll dp[1001][1001], K[1001][1001];

int main(void) {

  int n;
  cin >> n;

  vector<pii> v(n);
  rep(i, n) cin >> v[i].x >> v[i].y;

  fill(dp[0], dp[1001], INT_MAX);
  fill(K[0], K[1001], -1);
  rep(i, 1001) dp[i][i] = 0, K[i][i] = i;

  for (int w = 1; w < n; w++) {
    for (int i = 0; i + w <= n; i++) {
      for (int s = K[i][i + w - 1]; s <= K[i + 1][i + w]; s++) {
        int cost = abs(v[i].x - v[s + 1].x) + abs(v[s].y - v[i + w].y);
        if (dp[i][i + w] > dp[i][s] + dp[s + 1][i + w] + cost) {
          dp[i][i + w] = dp[i][s] + dp[s + 1][i + w] + cost;
          K[i][i + w] = s;
        }
      }
    }
  }

  cout << dp[0][n - 1] << endl;

  return 0;
}
Problem p01601 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;

  vector<bool> kai(10001, false);
  for (int i = 1; i < 10; i++) {
    kai[i] = true;
  }
  for (int i = 1; i < 10; i++) {
    kai[10 * i + i] = true;
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      kai[101 * i + 10 * j] = true;
      kai[1001 * i + 110 * j] = true;
    }
  }
  int ans = n;
  int ans2 = n;
  while (1) {
    if (kai[ans]) {
      cout << ans << endl;
      break;
    }
    if (kai[ans2]) {
      cout << ans2 << endl;
      break;
    }
    if (ans2 >= 10001)
      ans2 = n;
    ans--;
    ans2++;
  }

  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;

  vector<bool> kai(1001, false);
  for (int i = 1; i < 10; i++) {
    kai[i] = true;
  }
  for (int i = 1; i < 10; i++) {
    kai[10 * i + i] = true;
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      kai[101 * i + 10 * j] = true;
      kai[1001 * i + 110 * j] = true;
    }
  }
  int ans = n;
  int ans2 = n;
  while (1) {
    if (kai[ans]) {
      cout << ans << endl;
      break;
    }
    if (kai[ans2]) {
      cout << ans2 << endl;
      break;
    }
    if (ans2 >= 10001)
      ans2 = n;
    ans--;
    ans2++;
  }

  return 0;
}
Problem p01601 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, minx;
int x[100000];

void palind() {
  for (int i = 0; i < 100000; i++) {
    if (i < 10) {
      x[i] = 1;
    } else if (i >= 10 && i < 100 && i % 11 == 0) {
      x[i] = 1;
    } else if (i >= 100 && i < 1000 && i / 100 == i % 10) {
      x[i] = 1;
    } else if (i >= 1000 && i < 10000 && i / 1000 == i % 10 && i % 11 == 0) {
      x[i] = 1;
    } else if (i >= 10000 && i / 10000 == i % 10 &&
               (i / 1000) % 10 == (i / 10) % 10) {
      x[i] = 1;
    }
  }
}

int main() {
  palind();
  cin >> n;
  for (int i = n; i >= 0; i--) {
    if (x[i] == 1) {
      minx = i;
      break;
    }
  }
  for (int i = n; i < 100000; i++) {
    if (x[i] == 1) {
      if (n - minx > i - n) {
        minx = i;
      };
      break;
    }
  }
  cout << minx << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int n, minx;
int x[100000];

void palind() {
  for (int i = 0; i < 100000; i++) {
    if (i < 10) {
      x[i] = 1;
    } else if (i < 100 && i % 11 == 0) {
      x[i] = 1;
    } else if (i < 1000 && i / 100 == i % 10) {
      x[i] = 1;
    } else if (i < 10000 && i / 1000 == i % 10 && i % 11 == 0) {
      x[i] = 1;
    } else if (i / 10000 == i % 10 && (i / 1000) % 10 == (i / 10) % 10) {
      x[i] = 1;
    }
  }
}

int main() {
  cin >> n;
  for (int i = n; i >= 0; i++) {
    if (x[i] == 1) {
      minx = i;
      break;
    }
  }
  for (int i = n; i < 100000; i++) {
    if (x[i] == 1) {
      if (n - minx > n - i) {
        minx = i;
      };
      break;
    }
  }
  cout << minx << endl;
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

int main() {
  int n;
  while (~scanf("%d", &n)) {
    deque<pair<char, int>> seq;
    for (int i = 0; i < n; i++) {
      char parentheses[2];
      int num;

      scanf("%s %d", parentheses, &num);
      seq.push_back(pair<char, int>(parentheses[0], num));
    }

    int max_round = seq.size();
    for (int round = 0; round < max_round; round++) {
      for (int i = 0; i + 1 < seq.size(); i++) {
        if (seq[i].first == '(' && seq[i + 1].first == ')') {
          if (seq[i].second < seq[i + 1].second) {
            seq[i + 1].second -= seq[i].second;
            seq.erase(seq.begin() + i);
            break;
          } else if (seq[i].second > seq[i + 1].second) {
            seq[i].second -= seq[i + 1].second;
            seq.erase(seq.begin() + i + 1);
            break;
          } else if (seq[i].second == seq[i + 1].second) {
            seq.erase(seq.begin() + i, seq.begin() + i + 2);
            break;
          }
        }
      }
    }

    printf("%s\\n", seq.size() == 0 ? "YES" : "NO");
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

int main() {
  int n;
  while (~scanf("%d", &n)) {
    deque<char> seq;
    for (int i = 0; i < n; i++) {
      char parentheses[2];
      int num;

      scanf("%s %d", parentheses, &num);
      for (int j = 0; j < num; j++) {
        seq.push_back(parentheses[0]);
      }
    }

    int max_round = seq.size();
    for (int round = 0; round < max_round; round++) {
      for (int i = 0; i + 1 < seq.size(); i++) {
        if (seq[i] == '(' && seq[i + 1] == ')') {
          seq.erase(seq.begin() + i, seq.begin() + i + 2);
          break;
        }
      }
    }

    printf("%s\\n", seq.size() == 0 ? "YES" : "NO");
  }
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, num, flg = 0;
  char ch;
  long long check = 0;

  cin >> n;
  while (n--) {
    cin >> ch >> num;
    if (ch == '(')
      check += num;
    else
      check -= num;

    if (check < 0) {
      flg = 1;
    }
  }
  if (check)
    flg = 1;

  if (flg)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, num;
  string str, s = "";

  cin >> n;
  while (n--) {
    cin >> str >> num;
    for (int i = 0; i < num; i++) {
      s += str;
    }
  }

  int flg = 0, check = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      check++;
    else
      check--;

    if (check < 0) {
      flg = 1;
      break;
    }
  }
  if (flg)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;

  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    long long x;
    char p;
    cin >> p >> x;
    if (p == '(')
      cnt += x;
    else if (p == ')')
      cnt -= x;
    if (cnt < 0) {
      cout << "NO" << endl;
      return 0;
    }
  }
  if (cnt == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string s = "";
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long x;
    char p;
    cin >> p >> x;
    for (int j = 0; j < x; j++) {
      s += p;
    }
  }
  if (s == "") {
    cout << "YES" << endl;
    return 0;
  } else {
    long long size = s.size();
    int cnt = 0;
    bool check = false;
    for (int i = 0; i < size; i++) {
      if (s[i] == '(')
        cnt++;
      else if (s[i] == ')') {
        if (cnt > 0)
          cnt--;
        else {
          check = true;
          break;
        }
      }
    }
    if (cnt == 0) {
      if (!check)
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 999999
#define rep(i, j) for (int i = 0; i < (j); ++i)
#define reps(i, j, k) for (int i = j; i < k; ++i)
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int n;
  scanf("%d", &n);
  int cnt = 0;
  bool res = false;
  rep(i, n) {
    char c;
    int num;
    cin >> c >> num;
    if (c == '(') {
      cnt += num;
    } else {
      cnt -= num;
      if (cnt < 0) {
        res = true;
      }
    }
  }
  if (cnt != 0)
    res = true;
  (!res) ? puts("YES") : puts("NO");
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 999999
#define rep(i, j) for (int i = 0; i < (j); ++i)
#define reps(i, j, k) for (int i = j; i < k; ++i)
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int n;
  scanf("%d", &n);
  string ans;
  rep(i, n) {
    string str;
    int num;
    cin >> str >> num;
    rep(j, num) ans += str;
  }
  int len = ans.size();
  bool fl = false, fr = false;
  bool res = false;
  int left = 0, right = 0;
  rep(i, len) {
    if (ans[i] == '(') {
      if (!fl) {
        if (fr) {
          fr = false;
          if (right != left) {
            res = true;
            break;
          }
          right = 0;
          left = 0;
        }
        fl = true;
        left++;
      } else if (fl) {
        left++;
      }
    } else if (ans[i] == ')') {
      if (!fr) {
        if (!fl) {
          res = true;
          break;
        }
        fr = true;
        right++;
        fl = false;
      } else if (fr) {
        right++;
      }
    }
  }
  (!res) ? puts("YES") : puts("NO");
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int main() {
  int n;
  while (cin >> n) {
    string s;
    int c = 0;
    rep(i, n) {
      char a;
      int b;
      cin >> a >> b;
      if (a == '(') {
        c += b;
      } else
        c -= b;
      if (c < 0) {
        cout << "NO" << endl;
        goto next;
      }
    }
    if (c == 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  next:;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int main() {
  int n;
  while (cin >> n) {
    string s;
    rep(i, n) {
      char a;
      int b;
      cin >> a >> b;
      rep(j, b) { s += a; }
    }
    int check = 0;
    rep(i, s.size()) {
      if (s[i] == '(')
        check++;
      if (s[i] == ')')
        check--;
      if (check < 0) {
        cout << "NO" << endl;
        goto next;
      }
    }
    if (check == 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  next:;
  }
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int n;
  cin >> n;
  int co = 0;
  bool han = true;
  rep(i, n) {
    string in;
    int a;
    cin >> in >> a;
    if (in == "(")
      co += a;
    else if (in == ")")
      co -= a;
    if (co < 0)
      han = false;
  }
  if (co != 0 || han == false)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
}
/*  rep(j,a)s=s+in;
}
bool han=true;
int co=0;
rep(i,s.size()){
if(s[i]=='(')co++;
else if(s[i]==')')co--;
if(co<0)han=false;
}
if(co!=0||han==false)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  rep(i, n) {
    string in;
    int a;
    cin >> in >> a;
    rep(j, a) s = s + in;
  }
  bool han = true;
  int co = 0;
  rep(i, s.size()) {
    if (s[i] == '(')
      co++;
    else if (s[i] == ')')
      co--;
    if (co < 0)
      han = false;
  }
  if (co != 0 || han == false)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
}
Problem p01602 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
using namespace std;

int main() {
  int n;
  cin >> n;
  char c;
  int x;
  cin >> c >> x;
  bool flag = true;
  if (c == ')' || n == 1) {
    cout << "NO" << endl;
    return 0;
  }

  stack<int> st;
  st.push(x);
  for (int i = 1; i < n; i++) {
    cin >> c >> x;
    int y = st.top();
    st.pop();
    if (c == '(') {
      st.push(x + y);
    } else {
      int z = y - x;
      if (z < 0) {
        flag = false;
        break;
      } else {
        st.push(z);
      }
    }
  }
  if (flag && st.top() >= 1) {
    flag = false;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
using namespace std;

int main() {
  int n;
  cin >> n;
  char c;
  int x;
  cin >> c >> x;
  bool flag = true;
  if (c == ')' || n == 1) {
    cout << "NO" << endl;
    return 0;
  }

  stack<int> st;
  st.push(x);
  for (int i = 1; i < n; i++) {
    cin >> c >> x;
    int y = st.top();
    st.pop();
    if (c == '(') {
      st.push(x + y);
    } else {
      int z = y - x;
      if (z < 0) {
        flag = false;
        break;
      } else {
        st.push(z);
      }
    }
  }
  if (st.top() >= 1) {
    flag = false;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main(void) {

  int n;
  cin >> n;
  string in;
  int x, now = 0;
  bool fg = false;

  for (int i = 0; i < n; i++) {
    cin >> in >> x;
    if (in == "(")
      now += x;
    else
      now -= x;
    if (now < 0)
      fg = true;
  }
  if (fg || now > 0)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main(void) {

  int n;
  cin >> n;
  string s, in;
  int x;

  for (int i = 0; i < n; i++) {
    cin >> in >> x;
    for (int j = 0; j < x; j++)
      s += in;
  }

  stack<char> st;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      st.push(s[i]);
    else if (st.empty()) {
      cout << "NO" << endl;
      return 0;
    } else
      st.pop();
  }
  cout << "YES" << endl;

  return 0;
}