Problem p03107 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("mmx,avx,fma")
// #pragma GCC optimize ("unroll-loops")

using namespace __gnu_pbds;
using namespace std;

template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
template <char d = ' ', class T>
ostream &operator<<(ostream &out, vector<T> &v) {
  for (auto &e : v) {
    out << e << d;
  }
  return out;
}
template <char d = ' ', class T> ostream &operator<<(ostream &out, set<T> &v) {
  for (auto &e : v) {
    out << e << d;
  }
  return out;
}

#define pb push_back
#define mk make_pair
#define X first
#define Y second
#define cont continue
#define ret return
#define For(i, a, b) for (int i = a; i < b; i++)
#define forn(i, a) for (int i = 0; i < a; i++)
#define ford(i, a, b) for (int i = b - 1; i >= a; i--)
#define fordn(i, a) for (int i = a - 1; i >= 0; i--)
#define forr(x, arr) for (auto &x : arr)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REE(s_)                                                                \
  {                                                                            \
    cout << s_ << '\n';                                                        \
    exit(0);                                                                   \
  }
#define endl '\n'
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())

#define int long long
typedef double dd;
typedef long double ldd;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef set<int> si;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <typename T> std::string toString(T val) {
  std::ostringstream oss;
  oss << val;
  return oss.str();
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
// const int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
// const int dx[8] = {2, 2, 1, 1, -1, -1, -2, -2};
// const int dy[8] = {1, -1, 2, -2, 2, -2, 1, -1};

const int mod = 1e9 + 7;

// int powq(int a, int n) {
//     int res = 1;
//     while (n){
//         if (n & 1) {
//             res *= a;
//             res %= mod;
//             --n;
//         }
//         else {
//             a *= a;
//             a %= mod;
//             n >>= 1;
//         }
//
//     }
//     res %= mod;
//     return res;
// }

signed main() {
  //    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef KULA
  freopen("input.txt", "r", stdin);
  //        freopen("output.txt", "w", stdout);
#else
  //        freopen("input.in", "r", stdin);
  //        freopen("output.out", "w", stdout);
#endif
  char x;
  cin >> x;
  deque<char> q;
  q.pb(x);
  int gg = 0;
  while (cin >> x) {
    if (q.back() != x) {
      gg += 2;
      q.pop_front();
    } else
      q.pb(x);
  }
  cout << gg;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("mmx,avx,fma")
// #pragma GCC optimize ("unroll-loops")

using namespace __gnu_pbds;
using namespace std;

template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
template <char d = ' ', class T>
ostream &operator<<(ostream &out, vector<T> &v) {
  for (auto &e : v) {
    out << e << d;
  }
  return out;
}
template <char d = ' ', class T> ostream &operator<<(ostream &out, set<T> &v) {
  for (auto &e : v) {
    out << e << d;
  }
  return out;
}

#define pb push_back
#define mk make_pair
#define X first
#define Y second
#define cont continue
#define ret return
#define For(i, a, b) for (int i = a; i < b; i++)
#define forn(i, a) for (int i = 0; i < a; i++)
#define ford(i, a, b) for (int i = b - 1; i >= a; i--)
#define fordn(i, a) for (int i = a - 1; i >= 0; i--)
#define forr(x, arr) for (auto &x : arr)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REE(s_)                                                                \
  {                                                                            \
    cout << s_ << '\n';                                                        \
    exit(0);                                                                   \
  }
#define endl '\n'
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())

#define int long long
typedef double dd;
typedef long double ldd;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef set<int> si;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <typename T> std::string toString(T val) {
  std::ostringstream oss;
  oss << val;
  return oss.str();
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
// const int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
// const int dx[8] = {2, 2, 1, 1, -1, -1, -2, -2};
// const int dy[8] = {1, -1, 2, -2, 2, -2, 1, -1};

const int mod = 1e9 + 7;

// int powq(int a, int n) {
//     int res = 1;
//     while (n){
//         if (n & 1) {
//             res *= a;
//             res %= mod;
//             --n;
//         }
//         else {
//             a *= a;
//             a %= mod;
//             n >>= 1;
//         }
//
//     }
//     res %= mod;
//     return res;
// }

signed main() {
  //    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef KULA
  freopen("input.txt", "r", stdin);
  //        freopen("output.txt", "w", stdout);
#else
  //        freopen("input.in", "r", stdin);
  //        freopen("output.out", "w", stdout);
#endif
  char x;
  cin >> x;
  deque<char> q;
  q.pb(x);
  int gg = 0;
  while (cin >> x) {
    if (q.size() && q.back() != x) {
      gg += 2;
      q.pop_front();
    } else
      q.pb(x);
  }
  cout << gg;
  return 0;
}