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

///
// File:  2728.cpp
// Author: ymiyamoto
//
// Created on Thu Dec 28 20:03:55 2017
//

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int64_t diff(string A, string B) {
  return min(abs(stol(A) - stol(B)),
             (int64_t)pow(10, A.size()) - abs(stol(A) - stol(B)));
}

bool comp(pair<int64_t, string> x, pair<int64_t, string> y) {
  if (x.first == y.first) {
    return x.second > y.second;
  }
  return x.first < y.first;
}

pair<int64_t, string> min_str(string A, vector<bool> used, string B) {
  if (A.size() == B.size()) {
    return {diff(A, B), B};
  } else {
    vector<pair<int64_t, string>> strs;
    for (uint32_t i = 0; i < used.size(); i++) {
      if (!used[i]) {
        used[i] = true;
        string b = string(B);
        b += i + '0';
        strs.push_back(min_str(A, used, b));
        used[i] = false;
      }
    }
    sort(strs.begin(), strs.end(), comp);
    if (strs.size() > 0) {
      return strs[strs.size() - 1];
    } else {
      return {-1, ""};
    }
  }
}

int32_t main() {
  string A;
  cin >> A;

  vector<bool> used(10, false);
  cout << min_str(A, used, "").second << endl;

  return 0;
}

Error: 0

Fixed submission:

///
// File:  2728.cpp
// Author: ymiyamoto
//
// Created on Thu Dec 28 20:03:55 2017
//

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int32_t diff(string A, string B) {
  return min(abs(stoi(A) - stoi(B)),
             (int32_t)pow(10, A.size()) - abs(stoi(A) - stoi(B)));
}

bool comp(pair<int32_t, string> x, pair<int32_t, string> y) {
  if (x.first == y.first) {
    return x.second > y.second;
  }
  return x.first < y.first;
}

pair<int32_t, string> min_str(string A, vector<bool> used, string B) {
  if (A.size() == B.size()) {
    return {diff(A, B), B};
  } else {
    vector<pair<int32_t, string>> strs;
    for (uint32_t i = 0; i < used.size(); i++) {
      if (!used[i]) {
        used[i] = true;
        string b = string(B);
        b += i + '0';
        strs.push_back(min_str(A, used, b));
        used[i] = false;
      }
    }
    sort(strs.begin(), strs.end(), comp);
    if (strs.size() > 0) {
      return strs[strs.size() - 1];
    } else {
      return {-1, ""};
    }
  }
}

int32_t main() {
  string A;
  cin >> A;

  vector<bool> used(10, false);
  cout << min_str(A, used, "").second << endl;

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

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

int main() {
  string S, res;
  cin >> S;
  ll n = S.size(), md = -1, MAX = 1, s = stoll(S);
  vector<int> digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (int i = 0; i < n; i++) {
    MAX *= 10;
  }
  do {
    ll val = 0;
    string v;
    for (int i = 0; i < n; i++) {
      val = val * 10 + digit[i];
      v.push_back('0' + digit[i]);
    }
    ll d = min(MAX - abs(s - val), abs(s - val));
    if (d > md) {
      md = d;
      res = v;
    }
  } while (next_permutation(digit.begin(), digit.end()));
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  string S, res;
  cin >> S;
  ll n = S.size(), md = -1, MAX = 1, s = stoi(S);
  vector<int> digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (int i = 0; i < n; i++) {
    MAX *= 10;
  }
  do {
    ll val = 0;
    string v;
    for (int i = 0; i < n; i++) {
      val = val * 10 + digit[i];
      v.push_back('0' + digit[i]);
    }
    ll d = min(MAX - abs(s - val), abs(s - val));
    if (d > md) {
      md = d;
      res = v;
    }
  } while (next_permutation(digit.begin(), digit.end()));
  cout << res << endl;
  return 0;
}
Problem p01829 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

string s, t, ans;
bool used[10];
ll Max = 0, ten = 1, a;
void dfs() {
  if (s.size() == t.size()) {
    ll b = stoll(t);
    ll k = min(abs(a - b), ten - abs(a - b));
    if (Max < k) {
      Max = k;
      ans = t;
    } else if (Max == k) {
      ll c = stoll(ans);
      if (b < c)
        ans = t;
    }
    return;
  }
  rep(i, 10) {
    if (!used[i]) {
      used[i] = true;
      t += char(i + '0');
      dfs();
      used[i] = false;
      t.pop_back();
    }
  }
}
int main() {
  cin >> s;
  a = stoll(s);
  rep(i, s.size()) ten *= 10;
  dfs();
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

ll n;
ll minu(ll a, ll b) { return min(abs(a - b), n - abs(a - b)); }
int main() {
  string s;
  cin >> s;
  n = pow(10, s.size());
  ll a = stoll(s);
  ll Max = 0;
  string ans;
  ll u = a >= n / 2 ? a - n / 2 : a + n / 2;
  for (ll v = u;; v--) {
    stringstream k;
    k << setfill('0') << setw(s.size()) << v;
    string t = k.str();
    string w = t;
    sort(t.begin(), t.end());
    if (unique(t.begin(), t.end()) - t.begin() != t.size())
      continue;
    ans = w;
    Max = minu(a, v);
    break;
  }
  for (ll v = u;; v++) {
    stringstream k;
    k << setfill('0') << setw(s.size()) << v;
    string t = k.str();
    string w = t;
    sort(t.begin(), t.end());
    if (unique(t.begin(), t.end()) - t.begin() != t.size())
      continue;
    if (minu(a, v) > Max)
      ans = w;
    break;
  }
  cout << ans << endl;
}
Problem p01830 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<char, int> P;
typedef pair<int, int> P1;

int main() {

  int n, l;
  char d;

  cin >> n;

  vector<P> x;

  priority_queue<P1, vector<P1>, greater<P1>> q;

  for (int i = 0; i < n; i++) {

    cin >> d >> l;

    if (!i) {
      x.push_back(P(d, l));
      continue;
    }

    if (d == x[x.size() - 1].first) {

      if (x[x.size() - 1].first == 'y')
        x[x.size() - 1].second = min(x[x.size() - 1].second, l);

      if (x[x.size() - 1].first == 'n')
        x[x.size() - 1].second = max(x[x.size() - 1].second, l);

    } else
      x.push_back(P(d, l));
  }

  for (int i = 0; i < x.size(); i++)
    if (x[i].first == 'y')
      q.push(P1(x[i].second, i));

  vector<int> used(x.size(), 0);

  int ans = 0;

  while (!q.empty()) {

    P1 t = q.top();
    q.pop();

    int cost = t.first, node = t.second;

    if (used[node])
      continue;
    used[node] = 1;

    ans++;

    for (int i = node - 1; i >= 0; i--) {
      if (x[i].first == 'n' && cost <= x[i].second)
        break;
      used[i] = 1;
    }

    for (int i = node + 1; i < x.size(); i++) {
      if (x[i].first == 'n' && cost <= x[i].second)
        break;
      used[i] = 1;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<char, int> P;
typedef pair<int, int> P1;

int main() {

  int n, l;
  char d;

  cin >> n;

  vector<P> x;

  priority_queue<P1, vector<P1>, greater<P1>> q;

  for (int i = 0; i < n; i++) {

    cin >> d >> l;

    if (!i) {
      x.push_back(P(d, l));
      continue;
    }

    if (d == x[x.size() - 1].first) {

      if (x[x.size() - 1].first == 'y')
        x[x.size() - 1].second = min(x[x.size() - 1].second, l);

      if (x[x.size() - 1].first == 'n')
        x[x.size() - 1].second = max(x[x.size() - 1].second, l);

    } else
      x.push_back(P(d, l));
  }

  for (int i = 0; i < n; i++)
    if (x[i].first == 'y')
      q.push(P(x[i].second, i));

  vector<int> used(x.size(), 0);

  int ans = 0;

  while (!q.empty()) {

    P t = q.top();
    q.pop();

    int cost = t.first, node = t.second;

    if (used[node])
      continue;
    used[node] = 1;

    ans++;

    for (int i = node - 1; i >= 0; i--) {
      if (x[i].first == 'n' && cost <= x[i].second)
        break;
      used[i] = 1;
    }

    for (int i = node + 1; i < n; i++) {
      if (x[i].first == 'n' && cost <= x[i].second)
        break;
      used[i] = 1;
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;

inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-')
      b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}
inline void read(char &x) {
  for (x = nc(); !(x >= 'a' && x <= 'z'); x = nc())
    ;
}

const int N = 1005;

int n, ans;
int a[N], b[N];

int main() {
  char order;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(order);
    read(a[i]);
    if (order == 'y')
      b[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    int t = 0;
    for (int j = 1; j <= n; j++)
      if (b[j] && (!t || a[j] < a[t]))
        t = j;
    if (!t)
      break;
    for (int j = t - 1; j; j--)
      if (!b[j] && a[j] >= a[t])
        break;
      else if (b[j])
        a[j] = -1;
    for (int j = t + 1; j <= n; j++)
      if (!b[j] && a[j] >= a[t])
        break;
      else if (b[j])
        a[j] = -1;
    a[t] = -1;
    int pnt = 0;
    for (int j = 1; j <= n; j++)
      if (a[j] != -1)
        a[++pnt] = a[j], b[pnt] = b[j];
    n = pnt;
    ++ans;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;

inline int read(char *s) {
  char c = getchar();
  int len = 0;
  for (; !(c == '<' || c == '>'); c = getchar())
    ;
  for (; c == '<' || c == '>'; s[++len] = c, c = getchar())
    ;
  return len;
}

int n;

char S[100005];
int a[100005];
int pnt;
char s[100005];

int main() {
  scanf("%d", &n);
  read(S);
  for (int i = 1; i <= n; i++)
    if (S[i] != S[i - 1])
      s[++pnt] = S[i], a[pnt] = 1;
    else
      a[pnt]++;
  int ans = n;
  int maxv = 0;
  if (s[1] == '<')
    maxv = max(maxv, a[1]), ans -= a[1];
  if (s[pnt] == '>')
    maxv = max(maxv, a[pnt]), ans -= a[pnt];
  ans += maxv;
  printf("%d\\n", ans);
}
Problem p01831 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  int N;
  string s;
  cin >> N >> s;

  deque<int> R, L;
  for (int i = 0; i < N; i++) {
    if (s[i] == '<') {
      L.push_back(i);
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == '<') {
      L.pop_front();
      if (R.size() == 0) {
        ans = max(ans, i + 1);
      } else {
        // ???????????????
        if (R.size() >= L.size() + 1) {
          int l = R[R.size() - 1 - L.size()];
          int r = N - 1;
          ans = max(ans, r - l + 1);
        } else {
          int l = 0;
          int r = L[R.size() - 1];
          ans = max(ans, r - l + 1);
        }
      }
    } else {
      if (L.size() == 0) {
        ans = max(ans, N - i);
      } else {
        if (L.size() >= R.size() + 1) {
          int l = 0;
          int r = L[R.size()];
          ans = max(ans, r - l + 1);
        } else {
          int l = R[R.size() - L.size()];
          int r = N - 1;
          ans = max(ans, r - l + 1);
        }
      }

      R.push_back(i);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  int N;
  string s;
  cin >> N >> s;

  deque<int> R, L;
  for (int i = 0; i < N; i++) {
    if (s[i] == '<') {
      L.push_back(i);
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    int ln = 0, rn = 0;
    if (s[i] == '<') {
      L.pop_front();
      if (R.size() == 0) {
        ans = max(ans, i + 1);
      } else {
        if (R.size() >= L.size() + 1) {
          int l = R[R.size() - 1 - L.size()];
          int r = N - 1;
          ans = max(ans, r - l + 1);
        } else {
          int l = 0;
          int r = L[R.size() - 1];
          ans = max(ans, r - l + 1);
        }
      }
    } else {
      if (L.size() == 0) {
        ans = max(ans, N - i);
      } else {
        if (L.size() >= R.size() + 1) {
          int l = 0;
          int r = L[R.size()];
          ans = max(ans, r - l + 1);
        } else {
          int l = R[R.size() - 1 - L.size()];
          int r = N - 1;
          ans = max(ans, r - l + 1);
        }
      }

      R.push_back(i);
    }
  }
  cout << ans << endl;
}
Problem p01831 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool comp(int a, int b) { return b < a; }

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

  vector<char> lr(n + 2, 0);
  vector<int> csuml(n + 2, 0), csumr(n + 2, 0);
  for (int i = 1; i <= n; i++) {
    cin >> lr[i];
  }
  for (int i = 1; i < n + 2; i++) {
    csuml[i] = csuml[i - 1];
    if (lr[i] == '>')
      csuml[i]++;
  }
  for (int i = n; i >= 0; i--) {
    csumr[i] = csumr[i + 1];
    if (lr[i] == '<')
      csumr[i]++;
  }

  int s = 1, e = n;
  int ans = 0;
  while (s <= e) {
    int m = (s + e) / 2;
    int adjst = 0;
    vector<int>::iterator it;
    if (csuml[m - 1] < csumr[m + 1] ||
        (csuml[m - 1] == csumr[m + 1] && lr[m] == '<')) {
      s = m + 1;
      if (lr[m] == '>')
        adjst = 1;
      it = lower_bound(csumr.begin(), csumr.end(),
                       csumr[m + 1] - csuml[m - 1] - adjst, comp);
      ans = max(ans, (int)(it - csumr.begin()) - 1);
    } else {
      e = m - 1;
      if (lr[m] == '<')
        adjst = 1;
      it = upper_bound(csuml.begin(), csuml.end(),
                       csuml[m - 1] - csumr[m + 1] - adjst);
      ans = max(ans, (int)(csuml.end() - it) - 1);
    }
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  vector<char> lr(n + 2, 0);
  vector<int> csuml(n + 2, 0), csumr(n + 2, 0);
  for (int i = 1; i <= n; i++) {
    cin >> lr[i];
  }
  for (int i = 1; i < n + 2; i++) {
    csuml[i] = csuml[i - 1];
    if (lr[i] == '>')
      csuml[i]++;
  }
  for (int i = n; i >= 0; i--) {
    csumr[i] = csumr[i + 1];
    if (lr[i] == '<')
      csumr[i]++;
  }

  int s = 1, e = n;
  while (s < e) {
    int m = (s + e) / 2;
    if (csuml[m - 1] < csumr[m + 1] ||
        (csuml[m - 1] == csumr[m + 1] && lr[m] == '<')) {
      s = m + 1;
    } else {
      e = m - 1;
    }
  }

  int ans = 0;
  for (int i = e - 1; i <= s + 1; i++) {
    vector<char> vec = lr;
    int p = i, dir = 0, cnt = 0;
    while (p != 0 && p != n + 1) {
      if (vec[p] == '<') {
        dir = -1;
        cnt++;
      } else if (vec[p] == '>') {
        dir = 1;
        cnt++;
      }
      vec[p] = 0;
      p += dir;
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
Problem p01831 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define full(c) c.begin(), c.end()
#define vector2d(name, type, h, w, init)                                       \\
  vector<vector<type>> name(h, vector<type>(w, init))
const int PRIME = 1000000007;
const int INT_INF = 2147483647;
const long long int LL_INF = 9223372036854775807;
const double PI = acos(-1);
const double EPS = 0.000000001;
typedef unsigned long long int ull;
typedef long long int ll;
using namespace std;

int main() {
  int i, n, l = 0, r = 0;
  string s;
  cin >> n;
  cin >> s;
  i = 0;
  while (s[i] == '<')
    i++, l++;
  i = n - 1;
  while (s[i] == '>')
    i--, r++;
  cout << n - min(l, r) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)

int main() {
  int N, ans = 0, i, j;
  string temp, temp2;

  cin >> N;
  cin >> temp2;

  for (j = 0; j < N; j++) {
    i = j;
    temp = temp2;
    int count = 0;

    while (1) {
      if (temp[i] == '<') {
        temp[i] = 'B';
        i--;
        while (i != -1) {
          if (temp[i] == 'B')
            i--;
          else
            break;
        }
      } else if (temp[i] == '>') {
        temp[i] = 'B';
        i++;
        while (i != N) {
          if (temp[i] == 'B')
            i++;
          else
            break;
        }
      }
      if (i == -1)
        break;
      if (i == N)
        break;
    }
    for (int j = 0; j < N; j++) {
      if (temp[j] == 'B')
        count++;
    }
    if (count > ans)
      ans = count;
  }

  cout << ans << endl;
  return 0;
}
Problem p01831 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX 100050
string s;
int n, ans, memo[MAX];
int calc(int x) {
  if (~memo[x])
    return memo[x];
  int res = 0;
  int l = x, r = x;
  while (1) {
    if (s[x] == '>')
      x = ++r;
    else
      x = --l;
    res++;
    if (l < 0 || n <= r)
      break;
  }
  ans = max(ans, res);
  return memo[x] = res;
}
int main() {
  cin >> n >> s;
  memset(memo, -1, sizeof(memo));
  int l = 0, r = s.size() - 1, m1, m2;
  while (l + 1 < r) {
    m1 = l + (r - l) / 3;
    m2 = l + (r - l) * 2 / 3;
    calc(l);
    calc(r);
    if (calc(m1) < calc(m2))
      l = m1 + 1;
    else
      r = m2 - 1;
  }
  calc(l);
  calc(r);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 100050
string s;
int n, ans, memo[MAX];
int calc(int x) {
  if (~memo[x])
    return memo[x];
  int res = 0;
  int l = x, r = x;
  while (1) {
    if (s[x] == '>')
      x = ++r;
    else
      x = --l;
    res++;
    if (l < 0 || n <= r)
      break;
  }
  ans = max(ans, res);
  return memo[x] = res;
}
int main() {
  cin >> n >> s;
  memset(memo, -1, sizeof(memo));
  int l = 0, r = s.size() - 1, m1, m2;
  while (l + 1 < r) {
    m1 = (l + r) / 3;
    m2 = (l + r) * 2 / 3;
    if (calc(m1) < calc(m2))
      l = m1 + 1;
    else
      r = m2 - 1;
    if (ans == n)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p01839 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define EPS 1e-12

int main(void) {
  int n;
  cin >> n;
  string s[110];
  REP(i, n) cin >> s[i];

  int numa = 0;
  REP(i, n) {
    if (s[i] == "A") {
      numa++;
    } else {
      if (numa > 0)
        numa--;
      else if (numa == 0) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  if (!numa)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define EPS 1e-12

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    string s[110];
    REP(i, n) cin >> s[i];

    bool flag = true;
    int numa = 0;
    REP(i, n) {
      if (s[i] == "A") {
        numa++;
      } else {
        if (numa > 0)
          numa--;
        else if (numa == 0) {
          cout << "NO" << endl;
          flag = false;
          break;
        }
      }
    }

    if (!flag)
      continue;
    if (!numa)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p01839 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, c = 0, k = 0;
  string a;
  cin >> n;
  while (n--) {
    cin >> a;
    if (a == "A")
      c++;
    else
      c--;
    if (c < 0)
      k++;
  }
  if (k || c)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, c = 1, k = 0;
  char *a;
  cin >> n;
  while (n) {
    cin >> a;
    if (strlen(a) == 1)
      c++;
    else
      c--;
    if (n && !c)
      k++;
    n--;
  }
  if (k)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}
Problem p01839 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

int n;
char s[100][10];

int main() {
  scanf("%d", &n);
  int acounter = 0;
  bool ans = true;
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
    if (s[i][0] == 'A')
      acounter++;
    else {
      if (acounter > 0)
        acounter--;
      else
        ans = false;
    }
  }
  if (ans && acounter == 0)
    printf("YES\\n");
  else
    printf("NO\\n");
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int main() {
  int d, e;
  while (1) {
    scanf("%d%d", &d, &e);
    if (d == 0)
      break;
    double ans = abs(d - e);
    for (int x = 0; x <= d; x++) {
      int y = d - x;
      double l = sqrt(x * x + y * y);
      ans = min(ans, abs(l - e));
    }
    printf("%f\\n", ans);
  }
}
Problem p01839 (C++) — buggy submission, verdict: Runtime Error

// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

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

  int cnt = 0;
  bool wrong = false; // ????????????true????????¨?????????

  string s;
  rep(i, n) {
    cin >> s;
    if (s == "A") {
      cnt++;
    } else if (s == "Un") {
      if (cnt > 0) {
        cnt--;
      } else {
        wrong = true;
      }
    }
  }

  if (wrong || cnt > 0) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

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

  stack<string> st;
  bool fail = false;

  rep(i, n) {
    cin >> s;
    if (s == "A") {
      st.push(s);
    } else {
      if (st.top() == "A") {
        st.pop();
      } else {
        fail = true;
      }
    }
  }
  if (st.size() == 0 && !fail) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
Problem p01839 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
const int INF = sizeof(int) == sizeof(long long) ? 1e18 : 1e9;
const int MOD = 1000000007;
const long double EPS = 1e-8;

int n;
string s[110];
int main() {
  cin >> n;
  rep(i, n) cin >> s[i];
  int cnt = 0;
  rep(i, n) {
    cnt += s[i] == "A";
    cnt -= s[i] != "A";
    if (cnt < 0) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << (cnt ? "NO" : "YES") << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int k[n];
    for (int i = 0; i < n; i++)
      cin >> k[i];
    string s;
    cin >> s;
    int j = 0;
    for (int i = 0; i < s.size(); i++) {
      if (j == n)
        j = 0;
      s[i] -= k[j];
      if (s[i] < 65)
        s[i] += 58;
      if (s[i] > 91 && s[i] < 97)
        s[i] -= 6;
      j++;
    }
    cout << s << endl;
  }
}