prithivMLmods/Coder-Stat huggingface dataset:
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll int64_t
const long mod = 1e9 + 7;

const long LINF = 1e13;
const long LLINF = 1e18;
const long ALPHABET = 26;

template <class T> void Swap(T &r, T &l) {
  T tmp = r;
  r = l;
  l = tmp;
}

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x; // for(int i=0; i<vec.size(); i++) is >> x[i]; とかでもいい
  return is;
}

// 最大公約数
int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;
  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
} // gcd

// 最小公倍数
int lcm(int m, int n) { // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
} // lcm

int main() {

  long a, b, k;

  cin >> a >> b >> k;

  long ans = 0;
  long count = 0;
  vector<long> stock;

  for (int i = 1; i < max(a, b); ++i) {
    if (a % i == 0 && b % i == 0) {
      count++;
      stock.push_back(i);
    }
  }

  ans = stock[stock.size() - k];

  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll int64_t
const long mod = 1e9 + 7;

const long LINF = 1e13;
const long LLINF = 1e18;
const long ALPHABET = 26;

template <class T> void Swap(T &r, T &l) {
  T tmp = r;
  r = l;
  l = tmp;
}

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x; // for(int i=0; i<vec.size(); i++) is >> x[i]; とかでもいい
  return is;
}

// 最大公約数
int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;
  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
} // gcd

// 最小公倍数
int lcm(int m, int n) { // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
} // lcm

int main() {

  long a, b, k;

  cin >> a >> b >> k;

  long ans = 0;
  long count = 0;
  vector<long> stock;

  for (int i = 1; i <= max(a, b); ++i) {
    if (a % i == 0 && b % i == 0) {
      count++;
      stock.push_back(i);
    }
  }

  ans = stock[stock.size() - k];

  cout << ans;
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, k;
  cin >> a >> b >> k;
  vector<int> ans;
  for (int i = 1; i < max(a, b); i++) {
    if ((a % i == 0) && (b % i == 0)) {
      ans.push_back(i);
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans[k - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, k;
  cin >> a >> b >> k;
  vector<int> ans;
  for (int i = 1; i <= min(a, b); i++) {
    if ((a % i == 0) && (b % i == 0)) {
      ans.push_back(i);
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans[k - 1] << endl;

  return 0;
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int A, B, K, N;
  cin >> A >> B >> K;
  vector<int> Div(0);

  for (int i = 1; i < max(A, B); i++) {
    if (A % i == 0 && B % i == 0) {
      Div.push_back(i);
      N++;
    }
  }
  cout << Div.at(N - K) << endl;
}

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

int main() {
  int A, B, K, N;
  cin >> A >> B >> K;
  vector<int> Div(0);

  for (int i = 1; i <= max(A, B); i++) {
    if (A % i == 0 && B % i == 0) {
      Div.push_back(i);
      N++;
    }
  }
  cout << Div.at(N - K) << endl;
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// define
typedef pair<int, int> pi;
#define int long long
#define rep(x, y) for (int i = x; i < y; i++)
#define REP(x, y) for (int j = x; j < y; j++)
#define Rep(x, y) for (int k = x; k < y; k++)
#define all(x) x.begin(), x.end()
#define pb push_back
#define vint vector<int>
#define vvint vector<vector<int>>
#define mod 100000007
#define elif else if
#define str string

signed main() {
  int N, M, O, P = 0, count = 0, count2 = 0;
  double d;
  char c;
  string S, T;
  vint A(0);
  cin >> N >> M >> O;
  rep(1, max(N, M)) {
    if (N % i == 0 && M % i == 0) {
      A.pb(i);
    }
  }
  cout << A[A.size() - O];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// define
typedef pair<int, int> pi;
#define int long long
#define rep(x, y) for (int i = x; i < y; i++)
#define REP(x, y) for (int j = x; j < y; j++)
#define Rep(x, y) for (int k = x; k < y; k++)
#define all(x) x.begin(), x.end()
#define pb push_back
#define vint vector<int>
#define vvint vector<vector<int>>
#define mod 100000007
#define elif else if
#define str string

signed main() {
  int N, M, O, P = 0, count = 0, count2 = 0;
  double d;
  char c;
  string S, T;
  vint A(0);
  cin >> N >> M >> O;
  for (int i = 1; i <= max(N, M); i++) {
    if (N % i == 0 && M % i == 0) {
      A.pb(i);
    }
  }
  cout << A[A.size() - O];
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int n1 = max(n, m), count = 0;
  for (int i = 0; i < n1; i++) {
    if (n % i == 0 && m % i == 0)
      count++;
    if (count == k) {
      cout << i << endl;
      break;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int n1 = max(n, m), count = 0;
  for (int i = n1; i >= 1; i--) {
    if (n % i == 0 && m % i == 0)
      count++;
    if (count == k) {
      cout << i << endl;
      break;
    }
  }
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int a, b, k;
  cin >> a >> b >> k;
  int waru[100], kazu;
  kazu = 0;
  for (int i = min(a, b); i >= 0; i--) {
    if (a % i == 0 && b % i == 0) {
      kazu++;
      waru[kazu] = i;
    }
  }
  cout << waru[k] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int a, b, k;
  cin >> a >> b >> k;
  int waru[100], kazu;
  kazu = 0;
  for (int i = min(a, b); i >= 1; i--) {
    if (a % i == 0 && b % i == 0) {
      kazu++;
      waru[kazu] = i;
    }
  }
  cout << waru[k] << endl;
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#define ll long long
#define vl vector<ll>
#define vvl vector<vector<ll>>
using namespace std;

// 自然数nの約数列挙 O(root(N))
// 使い方
// 自然数nを渡すとvector<long long>を返す
// ソート含めると O(root(N) + N log N)
vl divisor(ll n) {
  vl ret;
  for (ll i = 0; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }

  // これ入れたらオーダーちょい上がる
  sort(ret.begin(), ret.end());

  return ret;
}

// ユークリッドの互除法 ラメの定理よりO(log N)
// 使い方
// 整数a,bを渡すと最小公倍数を返す
ll gcd(ll a, ll b) {
  ll q = max(a, b);
  ll r = min(a, b);
  while (r) {
    ll t = q;
    q = r;
    r = t % r;
  }
  return q;
}

int main() {
  ll a, b, k;
  cin >> a >> b >> k;
  ll d = gcd(a, b);
  vl ngo = divisor(d);
  cout << ngo[ngo.size() - 1 - (k - 1)] << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#define ll long long
#define vl vector<ll>
#define vvl vector<vector<ll>>
using namespace std;

// 自然数nの約数列挙 O(root(N))
// 使い方
// 自然数nを渡すとvector<long long>を返す
// ソート含めると O(root(N) + N log N)
vl divisor(ll n) {
  vl ret;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }

  // これ入れたらオーダーちょい上がる
  sort(ret.begin(), ret.end());

  return ret;
}

// ユークリッドの互除法 ラメの定理よりO(log N)
// 使い方
// 整数a,bを渡すと最小公倍数を返す
ll gcd(ll a, ll b) {
  ll q = max(a, b);
  ll r = min(a, b);
  while (r) {
    ll t = q;
    q = r;
    r = t % r;
  }
  return q;
}

int main() {
  ll a, b, k;
  cin >> a >> b >> k;
  ll d = gcd(a, b);
  vl ngo = divisor(d);
  cout << ngo[ngo.size() - 1 - (k - 1)] << endl;
}
Problem p03106 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int a, b, k;
  vector<int> num;
  for (int i = 1; i <= max(a, b); i++) {
    if (i % a == 0 && i % b == 0) {
      num.push_back(i);
    }
  }
  sort(num.begin(), num.end(), greater<int>());
  cout << num[k - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int a, b, k;
  vector<int> num;
  cin >> a >> b >> k;
  for (int i = 1; i <= 100; i++) {
    if (a % i == 0 && b % i == 0) {
      num.push_back(i);
    }
  }
  sort(num.begin(), num.end(), greater<int>());
  cout << num[k - 1] << endl;
}
Problem p03106 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, k, count = 0, i, j;
  cin >> a >> b >> k;

  for (i = 100; i > 0; i++) {
    if (a % i == 0 && b % i == 0) {
      count++;
    }
    if (count == k) {
      break;
    }
  }

  cout << i << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, k, count = 0, i, j;
  cin >> a >> b >> k;

  for (i = max({a, b}); i > 0; i -= 1) {
    if (a % i == 0 && b % i == 0) {
      count++;
    }
    if (count == k) {
      break;
    }
  }

  cout << i << endl;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())

using namespace std;

string redbul;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> redbul;
  stack<char> rb;
  ll count = 0;
  char before_rb = redbul[0];
  rb.push(redbul[0]);
  for (int i = 1; i < redbul.length(); i++) {
    if (before_rb != redbul[i]) {
      count++;
      rb.pop();
    } else {
      rb.push(redbul[i]);
    }
    before_rb = redbul[i];
  }
  cout << count * 2 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())

using namespace std;

string redbul;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> redbul;
  stack<char> rb;
  ll count = 0;
  char before_rb = redbul[0];
  rb.push(redbul[0]);
  for (int i = 1; i < redbul.length(); i++) {
    if (before_rb != redbul[i]) {
      count++;
      rb.pop();
    } else {
      rb.push(redbul[i]);
    }
    if (rb.empty()) {
      i++;
      rb.push(redbul[i]);
    }
    before_rb = rb.top();
  }
  cout << count * 2 << endl;

  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  char a[10000];
  long n = 0;
  int i;
  int j = 0;
  int h = 0;
  for (i = 0; i < 1; i++) {
    scanf("%s", a);
  }
  // printf("%d\n", strlen(a));
  for (i = 0; i < strlen(a); i++) {
    // printf("%c", a[i]);
    if (a[i] == '0') {
      j++;
    } else if (a[i] == '1') {
      h++;
    }
  }
  if (j > h) {
    printf("%d", h * 2);
  } else {
    printf("%d", j * 2);
  }

  //   long b[a[1]];
  //   long c[a[1]];
  //   long d[a[1]];//番目
  //   long *e;
  //   e = (long *)malloc(sizeof(long) * a[0]*a[1]);
  //   long *f;
  //   f = (long *)malloc(sizeof(long) * a[0]*a[1]);
  //   for(i=0;i<a[0]*a[1];i++){
  //     e[i]=0;
  //     f[i]=0;
  //   }
  //   long g[a[0]];
  //     for(i=0;i<a[0];i++){
  //     g[i]=0;
  //   }
  //   for(j=0;j<a[1];j++){
  //     scanf("%ld",b+j);
  //     scanf("%ld",c+j);
  //     e[(b[j]-1)*a[1]+g[b[j]-1]]=c[j];
  //     f[(b[j]-1)*a[1]+g[b[j]-1]]=j;
  //     d[j]=1;
  //     g[b[j]-1]++;
  //     for(h=0;h<g[b[j]-1];h++){
  //       if(e[(b[j]-1)*a[1]+h]==0){}
  //       else if(e[(b[j]-1)*a[1]+h]<c[j]){
  //         d[j]++;
  //       }
  //       else if(e[(b[j]-1)*a[1]+h]>c[j]){
  //         d[f[(b[j]-1)*a[1]+h]]++;
  //       }
  //     }
  //   }
  // for(i=0;i<a[1];i++){
  //   printf("%06d",b[i]);
  //   printf("%06d\n",d[i]);
  // }
  // for(i=0;i<a[1]*a[0];i++){
  //   printf("%d,",e[i]);
  // }
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  char a[100000];
  long n = 0;
  int i;
  int j = 0;
  int h = 0;
  for (i = 0; i < 1; i++) {
    scanf("%s", a);
  }
  // printf("%d\n", strlen(a));
  for (i = 0; i < strlen(a); i++) {
    // printf("%c", a[i]);
    if (a[i] == '0') {
      j++;
    } else if (a[i] == '1') {
      h++;
    }
  }
  if (j > h) {
    printf("%d", h * 2);
  } else {
    printf("%d", j * 2);
  }

  //   long b[a[1]];
  //   long c[a[1]];
  //   long d[a[1]];//番目
  //   long *e;
  //   e = (long *)malloc(sizeof(long) * a[0]*a[1]);
  //   long *f;
  //   f = (long *)malloc(sizeof(long) * a[0]*a[1]);
  //   for(i=0;i<a[0]*a[1];i++){
  //     e[i]=0;
  //     f[i]=0;
  //   }
  //   long g[a[0]];
  //     for(i=0;i<a[0];i++){
  //     g[i]=0;
  //   }
  //   for(j=0;j<a[1];j++){
  //     scanf("%ld",b+j);
  //     scanf("%ld",c+j);
  //     e[(b[j]-1)*a[1]+g[b[j]-1]]=c[j];
  //     f[(b[j]-1)*a[1]+g[b[j]-1]]=j;
  //     d[j]=1;
  //     g[b[j]-1]++;
  //     for(h=0;h<g[b[j]-1];h++){
  //       if(e[(b[j]-1)*a[1]+h]==0){}
  //       else if(e[(b[j]-1)*a[1]+h]<c[j]){
  //         d[j]++;
  //       }
  //       else if(e[(b[j]-1)*a[1]+h]>c[j]){
  //         d[f[(b[j]-1)*a[1]+h]]++;
  //       }
  //     }
  //   }
  // for(i=0;i<a[1];i++){
  //   printf("%06d",b[i]);
  //   printf("%06d\n",d[i]);
  // }
  // for(i=0;i<a[1]*a[0];i++){
  //   printf("%d,",e[i]);
  // }
}
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cmath>
#include <stdio.h>
#include <tgmath.h>

using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
// rep…「0からn-1まで」の繰り返し
#define rep2(i, s, n) for (long long i = s; i <= (long long)(n); i++)
// rep2…「sからnまで」の繰り返し
#define repr(i, s, n) for (long long i = s; i >= (long long)(n); i--)
// repr…「ｓからnまで」の降順の繰り返し

typedef long long ll;

const ll inf = 1e18 + 7;
const ll mod = 1e9 + 7;

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

  ll ans = 0;

  bool jdg = true;

  while (jdg) {
    jdg = false;
    rep(i, s.size() - 1) {
      if (s[i] != s[i + 1]) {
        s.erase(s.begin() + i, s.begin() + i + 2);
        ans += 2;
        jdg = true;
        break;
      }
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <stdio.h>
#include <tgmath.h>

using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
// rep…「0からn-1まで」の繰り返し
#define rep2(i, s, n) for (long long i = s; i <= (long long)(n); i++)
// rep2…「sからnまで」の繰り返し
#define repr(i, s, n) for (long long i = s; i >= (long long)(n); i--)
// repr…「ｓからnまで」の降順の繰り返し

typedef long long ll;

const ll inf = 1e18 + 7;
const ll mod = 1e9 + 7;

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

  ll ans = 0;

  bool jdg = true;

  if (s.size() == 1) {
    cout << 0 << endl;
    return 0;
  }

  while (jdg) {
    jdg = false;
    rep(i, s.size() - 1) {
      if (s[i] != s[i + 1]) {
        s.erase(s.begin() + i, s.begin() + i + 2);
        ans += 2;
        jdg = true;
        break;
      }
    }
  }

  cout << ans << endl;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
using namespace std;
#include <algorithm>
int main(void) {
  char S[100000];
  scanf("%s", S);
  int count[2] = {0};
  int i = 0;
  while (S[i] != '\n') {
    if (S[i] == '1')
      count[0]++;
    else
      count[1]++;
    i++;
  }

  if (count[0] > count[1]) {
    printf("%d", count[1] * 2);
  } else
    printf("%d", count[0] * 2);

  return 0;
}

Fixed submission:
#include <stdio.h>
using namespace std;
#include <algorithm>
int main(void) {
  char S[100000];
  scanf("%s", S);
  int count[2] = {0};
  int i = 0;
  while (S[i] != '\0') {
    if (S[i] == '1')
      count[0]++;
    else
      count[1]++;
    i++;
  }

  if (count[0] > count[1]) {
    printf("%d", count[1] * 2);
  } else
    printf("%d", count[0] * 2);

  return 0;
}
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
cnt1 = 0
cnt2 = 0
while True:
    before = S
    S = before.replace("01", "", 1)
    if before != S:
        cnt1 += 1
    else:
        break

while True:
    before = S
    S = before.replace("10", "", 1)
    if before != S:
        cnt2 += 1
    else:
        break

print(2 * (cnt1 + cnt2))


Fixed submission:
S = input()
print(2 * min(S.count("1"), S.count("0")))
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

void Main() {
  vector<char> v;
  string S;
  cin >> S;
  int N = S.size();
  rep(i, N) { v.push_back(S[i]); }
  int removed = 0;
  int idx = 0;
  int vsize = v.size();
  while (idx < vsize) {
    // cout << idx << ", " << v.size() << endl;
    if (v[idx] != v[idx + 1]) {
      v.erase(v.begin() + idx);
      v.erase(v.begin() + idx);
      idx = 0; // 最初からチェックし直し
      removed += 2;
    } else
      idx++;
    vsize = v.size();
  }
  cout << removed << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  Main();
  return 0;
}

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

void Main() {
  vector<char> v;
  string S;
  cin >> S;
  int N = S.size();
  rep(i, N) { v.push_back(S[i]); }
  int removed = 0;
  int idx = 0;
  int vsize = v.size();
  while (idx < vsize - 1) {
    // cout << idx << ", " << v.size() << endl;
    if (v[idx] != v[idx + 1]) {
      v.erase(v.begin() + idx);
      v.erase(v.begin() + idx);
      idx = 0; // 最初からチェックし直し
      removed += 2;
    } else
      idx++;
    vsize = v.size();
  }
  cout << removed << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  Main();
  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -f;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
inline void chkmin(int &a, int b) {
  if (a > b)
    a = b;
}
inline void chkmax(int &a, int b) {
  if (a < b)
    a = b;
}
#define _ read()
int top, S[1000005];
int main() {
  string s;
  cin >> s;
  int n = s.length();
  for (int i = 0; i < n; i++) {
    if (s[i] != s[S[top]])
      top--;
    else
      S[++top] = i;
  }
  cout << n - top;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -f;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
inline void chkmin(int &a, int b) {
  if (a > b)
    a = b;
}
inline void chkmax(int &a, int b) {
  if (a < b)
    a = b;
}
#define _ read()
int top, S[1000005];
int main() {
  string s;
  cin >> s;
  int n = s.length();
  for (int i = 0; i < n; i++) {
    if (s[i] != s[S[top]] && top)
      top--;
    else
      S[++top] = i;
  }
  cout << n - top;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int count = 0;
  string s;
  cin >> n >> s;
  vector<char> c(n);
  for (int i = 0; i < n; i++) {
    c[i] = s.at(i);
    if (c[i] == '0') {
      count++;
    }
  }
  cout << 2 * min(count, n - count);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int count = 0;
  string s;
  cin >> s;
  n = s.length();
  vector<char> c(n);
  for (int i = 0; i < n; i++) {
    c[i] = s.at(i);
    if (c[i] == '0') {
      count++;
    }
  }
  cout << 2 * min(count, n - count);
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc120/tasks/abc120_b

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

typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

// int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int cnt = 0, pos;
  bool flag = true;
  string s;
  cin >> s;

  int i = 0;
  while (i < s.size() - 1) {
    // [0, 1]か[1, 0]のデータを見つけたら削除して、前に戻る
    if (s[i] == '0' && s[i + 1] == '1' || s[i] == '1' && s[i + 1] == '0') {
      s.erase(s.begin() + i, s.begin() + i + 2);
      cnt += 2;
      if (i > 0)
        i--;
    } else {
      i++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/abc120/tasks/abc120_b

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

typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

// int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int cnt = 0, pos;
  bool flag = true;
  string s;
  cin >> s;

  int i = 0;
  while (i < s.size() - 1 && s.size() >= 2) {
    // [0, 1]か[1, 0]のデータを見つけたら削除して、前に戻る
    if (s[i] == '0' && s[i + 1] == '1' || s[i] == '1' && s[i + 1] == '0') {
      s.erase(s.begin() + i, s.begin() + i + 2);
      cnt += 2;
      if (i > 0)
        i--;
    } else {
      i++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int INF = 1e9 + 5;
int main() {
  string s;
  cin >> s;
  int cnt = 0;
  bool q = true;
  while (q) {
    bool w = false;
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] != s[i + 1]) {
        s.erase(s.begin() + i, s.begin() + i + 2);
        i -= 2;
        cnt += 2;
        w = true;
      }
    }
    q = w;
  }
  cout << cnt;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int INF = 1e9 + 5;
int main() {
  string s;
  cin >> s;
  int cnt = 0;
  bool q = true;
  while (q) {
    bool w = false;
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] != s[i + 1]) {
        s.erase(s.begin() + i, s.begin() + i + 2);
        i -= 2;
        cnt += 2;
        w = true;
      }
    }
    if (s.size() == 0)
      break;
    q = w;
  }
  cout << cnt;
}
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;

  int N = S.length();
  int k = 0;
  int t = 0;

  int l = 0;

  while (k < N) {
    if ((S[k] == '0' && S[k + 1] == '1') || (S[k] == '1' && S[k + 1] == '0')) {
      for (int j = k; j < N; j++) {
        S[j] = S[j + 2];
      }
      l += 2;
      k -= 1;
    } else {
      k += 1;
    }
  }

  cout << l << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;

  int N = S.length();
  int k = 0;
  int t = 0;

  int l = 0;

  while (k < N) {
    if ((S[k] == '0' && S[k + 1] == '1') || (S[k] == '1' && S[k + 1] == '0')) {
      S.erase(k, 2);
      l += 2;
      k -= 1;
    } else {
      k += 1;
    }
  }

  cout << l << endl;

  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
  int count = 0;
  string s;

  cin >> s;

  int i = 0;
  while (true) {
    if (i > s.size() - 1 || s.empty()) {
      break;
    }

    if (s[i] == '0' && s[i + 1] == '1' || s[i] == '1' && s[i + 1] == '0') {
      s.erase(s.begin() + i + 1);
      s.erase(s.begin() + i);
      count += 2;
      i = 0;
    } else {
      i++;
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
  int count = 0;
  string s;

  cin >> s;

  int i = 0;
  while (true) {
    if (i > s.size() - 1 || s.empty()) {
      break;
    }

    if (s[i] == '0' && s[i + 1] == '1' || s[i] == '1' && s[i + 1] == '0') {
      s.erase(s.begin() + i + 1);
      s.erase(s.begin() + i);
      count += 2;
      if (i != 0) {
        i--;
      }
    } else {
      i++;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include "iostream"
#include "set"
#include "vector"

using namespace std;

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

  int n = s.size();
  vector<int> pre(n), post(n), dond(n);
  for (int i = 0; i < n; ++i)
    pre[i] = i - 1, post[i] = i + 1;

  int res = 0;
  for (int i = 1; i < n; i++) {
    if (pre[i] == -1) {
      continue;
    }

    if (s[i] != s[pre[i]]) {
      res += 2;
      pre[i + 1] = pre[pre[i]];
    }
  }
  printf("%d\n", res);
}

Fixed submission:
#include "iostream"
#include "set"
#include "vector"

using namespace std;

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

  int n = s.size();
  vector<int> pre(n), post(n), dond(n);
  for (int i = 0; i < n; ++i)
    pre[i] = i - 1, post[i] = i + 1;

  int res = 0;
  for (int i = 1; i < n; i++) {
    if (pre[i] == -1) {
      continue;
    }

    if (s[i] != s[pre[i]]) {
      res += 2;
      if (i + 1 < n)
        pre[i + 1] = pre[pre[i]];
    }
  }
  printf("%d\n", res);
}
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int mod = (int)1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); }
ll lcm(ll x, ll y) { return (x / gcd(x, y) * y); }

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = sz(s);
  bool nxt = true;
  while (nxt) {
    nxt = false;
    rep(i, n - 1) {
      if (s[i] == '0' && s[i + 1] == '1') {
        s[i] = s[i + 1] = '9';
        nxt = true;
      }
      if (s[i] == '1' && s[i + 1] == '0') {
        s[i] = s[i + 1] = '9';
        nxt = true;
      }
      s.erase(remove(all(s), '9'), s.end());
    }
    // dbg(s);
  }
  cout << n - sz(s) << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int mod = (int)1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); }
ll lcm(ll x, ll y) { return (x / gcd(x, y) * y); }

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = sz(s), one = 0, two = 0;
  rep(i, n) one += s[i] == '1';
  two = n - one;

  cout << min(one, two) * 2 << "\n";
  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

int main() {
  int cnt = 0, len;
  string s;
  cin >> s;
  len = s.size();
  for (int i = 0; i < len - 1; i++) {
    if (s.size() == 1)
      break;
    if ((s[i] == '0' && s[i + 1] == '1') || (s[i] == '1' && s[i + 1] == '0')) {
      cnt += 2;
      s.erase(i, 2);
      len = s.size();
      i = -1;
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  int cnt = 0, len;
  string s;
  cin >> s;
  len = s.size();
  for (int i = 0; i < len - 1; i++) {
    if (s.size() == 1)
      break;
    if ((s[i] == '0' && s[i + 1] == '1') || (s[i] == '1' && s[i + 1] == '0')) {
      cnt += 2;
      s.erase(i, 2);
      len = s.size();
      i -= 2;
    }
  }
  cout << cnt << endl;
}
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
/*
    Author: Mohammad faisal
    Problem: unification
    Date: 06/08/2019

*/
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  stack<char> con;
  con.push(s[0]);
  // cout<<"first top: "<<con.top()<<endl;
  int count = 0;
  int len = s.length();
  for (int i = 1; i < len; i++) {
    if ((con.top() == '0' && s[i] == '1') ||
        (con.top() == '1' && s[i] == '0')) {
      // cout<<"upper"<<endl;
      count++;
      con.pop();
      s[i] = '2';
      // i++;
    } else {
      // cout<<"nicheL : "<<endl;
      con.push(s[i]);
    }
  }
  // if(con.size()!=0){
  //   if((con.top()=='0' && s[len-1]=='1') || (con.top()=='1' && s[len]=='0')){
  //     count++;
  //   }
  // }
  cout << 2 * count << endl;
  return 0;
}


Fixed submission:
/*
    Author: Mohammad faisal
    Problem: unification
    Date: 06/08/2019

*/
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  int red = 0, blue = 0;
  red = count(s.begin(), s.end(), '0');
  blue = count(s.begin(), s.end(), '1');

  cout << min(red, blue) * 2 << endl;
  // cout<<2*count<<endl;
  return 0;
}
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()

num = 0
while True:
    p = S.find("01")
    if p > -1:
        S = S[:p] + S[p + 2 :]
        num += 2
    else:
        break
while True:
    p = S.find("10")
    if p > -1:
        S = S[:p] + S[p + 2 :]
        num += 2
    else:
        print(num)
        break


Fixed submission:
S = input()
print(min(S.count("0"), S.count("1")) * 2)
Problem p03107 (Python)

Buggy submission (status: Runtime Error):
S = input()
S = [Si for Si in S]

i = len(S) - 2
number_of_deleted_qube = 0
while i >= 0 and len(S) >= 2:
    current = S[i]
    right = S[i + 1]

    if current != right:
        del S[i + 1]
        del S[i]
        number_of_deleted_qube += 2

    i -= 1

print(number_of_deleted_qube)


Fixed submission:
S = input()
S = [Si for Si in S]

i = len(S) - 2
number_of_deleted_qube = 0
while i >= 0 and len(S) >= 2:
    if i >= len(S) - 1:
        i -= 1
        continue

    current = S[i]
    right = S[i + 1]

    if current != right:
        del S[i + 1]
        del S[i]
        number_of_deleted_qube += 2

    i -= 1

print(number_of_deleted_qube)
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
count = 0

while s.find("01") != -1 or s.find("10") != -1:
    count += 2 * s.count("01")
    s = s.replace("01", "")
    count += 2 * s.count("10")
    s = s.replace("10", "")
print(count)


Fixed submission:
s = input()
print(2 * min(s.count("0"), s.count("1")))
Problem p03107 (Python)

Buggy submission (status: Runtime Error):
S = list(int(input()))
a = S.count(0)
b = S.count(1)
ans = 2 * min(a, b)
print(ans)


Fixed submission:
S = list(input())
a = S.count("0")
b = S.count("1")
ans = 2 * min(a, b)
print(ans)
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
n = len(s)
i = 0
while True:
    before = len(s)
    s = s.replace("01", "").replace("10", "")
    after = len(s)
    if before == after:
        break
print(n - len(s))


Fixed submission:
s = input()
n0 = s.count("0")
n1 = s.count("1")
print(2 * min(n0, n1))
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
b = list(map(int, input()))
cnt = 0
while len(b) > 1:
    if len(list(set(b))) == 1:
        break
    if b[0] != b[1]:
        b = b[2:]
        cnt += 1
        continue
    temp = b[:1]
    b = b[1:]
    b.append(*temp)
print(cnt * 2)


Fixed submission:
s = input()
zom = min(s.count("1"), s.count("0"))
if len(set(list(s))) == 1:
    zom = 0
print(zom * 2)
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
def recursin_operation(S, len_init_s):
    S = list(S)
    N = len(S)
    lst = [i for i in range(N - 1) if S[i] != S[i + 1]]
    nums_of_deleted = []
    if len(lst) != 0:
        for i in lst:
            S_next = [S[j] for j in range(len(S)) if j < i or j > i + 1]
            nums_of_deleted.append(recursin_operation(S_next, len_init_s))
    else:
        nums_of_deleted.append(len_init_s - len(S))

    return max(nums_of_deleted)


def main():
    S = input()
    len_init_s = len(S)
    ans = recursin_operation(S, len_init_s)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
S = input()
len_S = len(S)
zero_lst = [S[i] for i in range(len_S) if S[i] == "0"]
one_lst = [S[i] for i in range(len_S) if S[i] == "1"]
ans = min(len(zero_lst), len(one_lst)) * 2
print(ans)
Problem p03107 (Python)

Buggy submission (status: Runtime Error):
s = input()
print(2 * min(s.count(0), s.count(1)))


Fixed submission:
s = input()
print(2 * min(s.count("0"), s.count("1")))
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
s = list(input())
n = len(s)
pos = 0
cnt = 0

while pos != n - 1 and n != 0:
    if s[pos] != s[pos + 1]:
        del s[pos]
        del s[pos]
        pos = 0
        n -= 2
        cnt += 2
    else:
        pos += 1

print(cnt)


Fixed submission:
s = input()
a = s.count("0")
b = s.count("1")
print(min(a, b) * 2)
Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 7;

char str[10005];
int main() {
  scanf("%s", str);

  int len = strlen(str);

  int l = 0, r = 0;
  int s = 0;
  for (int q = 0; q < len; q++) {
    if (str[q] == '1') {
      if (l > 0)
        l--, s += 2;
      else
        r++;
    } else {
      if (r > 0)
        r--, s += 2;
      else
        l++;
    }
  }

  printf("%d\n", s);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 7;

char str[200000];
int main() {
  scanf("%s", str);

  int len = strlen(str);

  int l = 0, r = 0;
  int s = 0;
  for (int q = 0; q < len; q++) {
    if (str[q] == '1') {
      if (l > 0)
        l--, s += 2;
      else
        r++;
    } else {
      if (r > 0)
        r--, s += 2;
      else
        l++;
    }
  }

  printf("%d\n", s);

  return 0;
}
Problem p03107 (Python)

Buggy submission (status: Runtime Error):
def solve(s):
    count = 0
    i = 0

    while True:
        if i >= len(s) - 1:
            break
        if s[i] != s[i + 1]:
            count += 1
            s = s[0:i] + s[i + 2 :]
            i -= 1
            continue
        i += 1
    return count


def main():
    S = input()
    # a, b, k = [int(a) for a in input().split()]

    print(solve(S) * 2)


if __name__ == "__main__":
    main()


Fixed submission:
def solve(s):
    one = 0
    zero = 0
    for i in range(len(s)):
        if s[i] == "1":
            one += 1
        elif s[i] == "0":
            zero += 1
    return min(zero, one)


def main():
    S = input()
    # a, b, k = [int(a) for a in input().split()]

    print(solve(S) * 2)


if __name__ == "__main__":
    main()
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string s;
  cin >> s;
  int count0 = 0, count1 = 0;
  for (int i = 0; i = s.size(); ++i) {
    if (s[i] == '0')
      ++count0;
    else
      ++count1;
  }
  int ans = 2 * min(count0, count1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string s;
  cin >> s;
  int count0 = 0, count1 = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '0')
      ++count0;
    else
      ++count1;
  }
  int ans = 2 * min(count0, count1);
  cout << ans << endl;
  return 0;
}
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
S_list = [int(S[i]) for i in range(len(S))]
ans = 0
rm_list = []
for aaa in range(len(S) // 2):
    for i in range(len(S_list) - 1):
        if S_list[i] != S_list[i + 1] and i not in rm_list:
            rm_list.append(i)
            rm_list.append(i + 1)
    S_list = [S_list[i] for i in range(len(S_list)) if i not in rm_list]
    if len(rm_list) == 0:
        break
    ans += len(rm_list)
    rm_list = []
print(ans)


Fixed submission:
S = input()
a = sum([int(S[i]) for i in range(len(S))])
b = len(S) - a
print(2 * min(a, b))
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
import re

s = input()
pattern = "(01)|(10)"
count = 0
diff = 1

while diff:
    ssub = re.sub(pattern, "", s)
    diff = len(s) - len(ssub)
    count += diff
    s = ssub


print(count)


Fixed submission:
import re

s = input()
zero = re.findall("0", s)
one = re.findall("1", s)
print(2 * min(len(zero), len(one)))
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
import sys


def solve(S: str):
    nS = len(S)
    pS = ""
    while pS != S:
        pS = S
        S = S.replace("01", "")
        S = S.replace("10", "")
    print(nS - len(S))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    solve(S)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
import sys


def solve(S: str):
    print(min(S.count("0"), S.count("1")) * 2)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    solve(S)


if __name__ == "__main__":
    main()
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
subS = S
cnt = 0
while True:
    flg = True
    if "01" in subS:
        flg = False
        subS = subS.replace("01", "", 1)
        cnt += 2
    if "10" in subS:
        flg = False
        subS = subS.replace("10", "", 1)
        cnt += 2
    if flg:
        break
print(cnt)


Fixed submission:
S = input()

zeroSum = 0
oneSum = 0
for _ in S:
    if _ == "0":
        zeroSum += 1
    if _ == "1":
        oneSum += 1

t = max(zeroSum, oneSum) - min(zeroSum, oneSum)
print(len(S) - t)
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
s = input()
n = len(s)
while True:
    ns = s.replace("01", "").replace("10", "")
    if ns == s:
        break
    s = ns
print(n - len(s))


Fixed submission:
#!/usr/bin/env python3
s = input()
n = len(s)
c = 0
p = ""
count = 0
for i in s:
    if i == p:
        count += 1
    else:
        c = count - c
        count = 1
    p = i
c = count - c
print(n - abs(c))
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
import re

S = input()
raw = len(S)
prev = len(S)
while True:
    S = re.sub(r"(10)+", "", S)
    S = re.sub(r"(01)+", "", S)
    if prev > len(S):
        prev = len(S)
    else:
        break
print(raw - len(S))


Fixed submission:
import re

S = input()
num_1 = S.count("1")
num_0 = S.count("0")
print(min(num_0, num_1) * 2)
Problem p03107 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()

ans = 0
while "01" in s or "10" in s:
    s_next = s.replace("01", "")
    ans += len(s) - len(s_next)
    s = s_next
    s_next = s.replace("10", "")
    ans += len(s) - len(s_next)
    s = s_next
print(ans)


Fixed submission:
s = input()

ans = 0

s_0 = s.replace("0", "")
n_0 = len(s) - len(s_0)
s_1 = s.replace("1", "")
n_1 = len(s) - len(s_1)

print(2 * min(n_0, n_1))
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  cin >> str;

  int start = str.size(), end;

  bool f = true;

  while (f) {
    f = false;

    for (int i = 0; i < str.size() - 3; ++i) {
      if (str.size() > 3) {
        if (str[i] != str[i + 3] && str[i + 1] != str[i + 2]) {
          str.erase(i + 1, 2);
          --i;
          f = true;
        }
      } else
        break;
    }
  }
  for (int i = 0; i < str.size() - 1; ++i) {
    if (str.size() > 1) {
      if (str[i] != str[i + 1]) {
        str.erase(i, 2);
        --i;
      }
    } else
      break;
  }
  end = str.size();

  cout << start - end << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  cin >> str;

  int start = str.size(), end;

  bool f = true;

  for (int i = 0; i < str.size() - 3; ++i) {
    if (str.size() > 3) {
      if (str[i] != str[i + 3] && str[i + 1] != str[i + 2]) {
        str.erase(i + 1, 2);
        i = max(0, i - 3);
      }
    } else
      break;
  }
  for (int i = 0; i < str.size() - 3; ++i) {
    if (str.size() > 3) {
      if (str[i] != str[i + 3] && str[i + 1] != str[i + 2]) {
        str.erase(i + 1, 2);
        i = max(0, i - 3);
      }
    } else
      break;
  }
  for (int i = 0; i < str.size() - 1; ++i) {
    if (str.size() > 1) {
      if (str[i] != str[i + 1]) {
        str.erase(i, 2);
        --i;
      }
    } else
      break;
  }
  end = str.size();

  cout << start - end << endl;

  return 0;
}
Problem p03107 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
using M = map<int, int>;

int main() {
  string s, t = "";
  cin >> s;
  int b;
  int ans = 0;
  do {
    t = "";
    b = 0;
    rep(i, s.size()) {
      if (i + 1 < s.size() && s[i] != s[i + 1]) {
        ans += 2;
        i++;
        b = 1;
      } else {
        t += s[i];
      }
    }
    s = t;
  } while (b);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
using M = map<int, int>;

int main() {
  string s, t = "";
  cin >> s;
  int a = 0, b = 0;
  rep(i, s.size()) {
    if (s[i] == '0')
      a++;
    else
      b++;
  }
  cout << min(a, b) * 2 << endl;

  return 0;
}