alexjercan/bugnet huggingface dataset:
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int board[100010];

int main() {
  int N;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    int ans = 0;

    for (int i = 0; i < N; i++) {
      cin >> board[i];
      if (i % 2 == 1) {
        for (int j = i - 1; j >= 0; j--) {
          if (board[j] == board[i])
            break;
          board[j] = board[i];
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (board[i] == 0)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int N;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    int board[100010] = {0};
    int ans = 0;

    for (int i = 0; i < N; i++) {
      cin >> board[i];
      if (i % 2 == 1) {
        for (int j = i - 1; j >= 0; j--) {
          if (board[j] == board[i])
            break;
          board[j] = board[i];
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (board[i] == 0)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
ll n;
vector<P> v; // first ???????????? second: 0or1
ll x;
int main() {
  while (1) {
    cin >> n;
    if (!n)
      return 0;
    v.clear();
    for (ll i = 1; i <= n; i++) {
      cin >> x;
      if (i == 1 || (i % 2 == 1 && v[v.size() - 1].second != x)) { //??°??????
        v.push_back(P(i, x));
      } else if (i % 2 == 0 && v[v.size() - 1].second != x) { //???????¶????
        if (v.size() == 1) {
          v[0].second = x;
        } else {
          v.erase(v.end() - 1);
        }
      }
    }
    ll ans = 0;
    for (ll i = 0; i < v.size(); i++) {
      if (v[i].second == 0)
        ans += (i == v.size() - 1 ? n + 1 : v[i + 1].first) - v[i].first;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
ll n;
vector<P> v; // first ???????????? second: 0or1
ll x;
int main() {
  {
    while (1)
      cin >> n;
    if (!n)
      return 0;
    v.clear();
    for (ll i = 1; i <= n; i++) {
      cin >> x;
      if (i == 1 || (i % 2 == 1 && v[v.size() - 1].second != x)) { //??°??????
        v.push_back(P(i, x));
      } else if (i % 2 == 0 && v[v.size() - 1].second != x) { //???????¶????
        if (v.size() == 1) {
          v[0].second = x;
        } else {
          v.erase(v.end() - 1);
        }
      }
    }
    ll ans = 0;
    for (ll i = 0; i < v.size(); i++) {
      if (v[i].second == 0)
        ans += (i == v.size() - 1 ? n + 1 : v[i + 1].first) - v[i].first;
    }
    cout << ans << endl;
  }
}
Problem p00450 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n;
  while (cin >> n, n) {
    int dp[100000] = {0};
    int pos = 0;
    rep(i, n) {
      int c;
      cin >> c;
      if ((i + 1) % 2 == 1) { // odd
        if (dp[pos] == 0) {
          if (c)
            dp[pos]++;
          else
            dp[pos]--;
          continue;
        }
        if (c == 1 && dp[pos] > 0) {
          dp[pos]++;
        } else if (c == 0 && dp[pos] < 0) {
          dp[pos]--;
        } else {
          pos++;
          if (c == 1)
            dp[pos]++;
          else
            dp[pos]--;
        }
      } else {
        if (c == 1 && dp[pos] > 0) {
          dp[pos]++;
        } else if (c == 0 && dp[pos] < 0) {
          dp[pos]--;
        } else {
          if (pos == 0) {
            dp[pos] *= -1;
          } else {
            dp[pos - 1] -= dp[pos];
            dp[pos] = 0;
            pos--;
          }
          if (c == 1)
            dp[pos]++;
          else
            dp[pos]--;
        }
      }
    }
    int sum = 0;
    rep(i, 100000) {
      if (dp[i] < 0)
        sum += dp[i];
    }
    cout << -sum << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, c = 0;
  vector<int> v(n);
  vector<int>::iterator p;
  p = v.begin();
  cin >> n >> m;
  v.push_back(m);
  for (int i = 1; i < n; i++) {
    p = v.begin();
    p = p + i - 1;
    cin >> m;
    v.push_back(m);
    for (int j = 0; j < i; j++) {
      if (*p != m)
        v[i] = m;
      p--;
    }
  }
  p = v.begin();
  while (p != v.end()) {
    if (*p == 0)
      c++;
    p++;
  }
  cout << c << endl;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

int n, c[112345];
int ans;

int main() {
  while (true) {
    n = 0;
    memset(c, 0, sizeof(c));
    ans = 0;

    in >> n;
    if (n == 0) {
      break;
    }
    rep(i, n) { in >> c[i]; }

    rep(i, n) {
      int j;
      for (j = i + 1; j < n; ++j) {
        if (c[i] != c[j]) {
          if ((j + 1) % 2 == 0) {
            for (int k = j - 1; k >= 0; --k) {
              if (c[k] == c[j]) {
                break;
              }
              c[k] = c[j];
            }
          } else {
            --j;
          }
          break;
        }
      }
      i = j;
    }
    rep(i, n) { ans += (c[i] == 0 ? 1 : 0); }

    out << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

int n, c[112345];
int ans;

int main() {
  while (true) {
    n = 0;
    memset(c, 0, sizeof(c));
    ans = 0;

    in >> n;
    if (n == 0) {
      break;
    }
    rep(i, n) { in >> c[i]; }

    rep(l, n) { out << c[l] << " "; }
    out << std::endl;
    rep(i, n) {
      int j;
      for (j = i + 1; j < n; ++j) {
        if (c[i] != c[j] && (j + 1) % 2 == 0) {
          for (int k = j - 1; k >= 0; --k) {
            if (c[k] == c[j]) {
              break;
            }
            c[k] = c[j];
          }
          break;
        }
      }
      i = j;
      rep(l, j + 1) { out << c[l] << " "; }
      out << std::endl;
    }
    rep(i, n) { ans += (c[i] == 0 ? 1 : 0); }

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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, c, cntw;
  vector<pair<int, int>> V;
  while (1) {
    cntw = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      cin >> c;
      if (i % 2 == 0) {
        if (c == 0 && V[V.size() - 1].first == 1)
          V[V.size() - 1].first = 0;
        else if (c == 1 && V[V.size() - 1].first == 0)
          V[V.size() - 1].first = 1;
      }
      if (V.size() != 0 && c == 0 && V[V.size() - 1].first == 0)
        V[V.size() - 1].second++;
      else if (c == 0)
        V.push_back(make_pair(c, 1));
      else if (V.size() != 0 && c == 1 && V[V.size() - 1].first == 1)
        V[V.size() - 1].second++;
      else if (c == 1)
        V.push_back(make_pair(c, 1));
      while (1) {
        int flag = 0;
        if (V.size() >= 2) {
          for (int j = 0; j < V.size() - 1; j++) {
            if (V[j].first == V[j + 1].first) {
              V[j].second += V[j + 1].second;
              V.erase(V.begin() + j + 1);
              flag = 1;
              break;
            }
          }
        }
        if (flag == 0)
          break;
      }
    }
    for (int i = 0; i < V.size(); i++) {
      if (V[i].first == 0)
        cntw += V[i].second;
    }
    cout << cntw << endl;
    V.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, c, cnt;
  vector<int> V;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      cin >> c;
      if (i % 2 == 0) {
        if (V.size() != 0) {
          if (c == 0 && V[V.size() - 1] == 1) {
            for (int j = V.size() - 1; j >= 0; j--) {
              if (V[j] == 1) {
                V[j] = 0;
              } else
                break;
            }
          } else if (c == 1 && V[V.size() - 1] == 0) {
            for (int j = V.size() - 1; j >= 0; j--) {
              if (V[j] == 0) {
                V[j] = 1;
              } else
                break;
            }
          }
        }
      }
      V.push_back(c);
    }
    cnt = 0;
    for (int i = 0; i < V.size(); i++) {
      if (V[i] == 0)
        cnt++;
    }
    cout << cnt << endl;
    V.clear();
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int ary[100010];

int main() {
  int N, hog, res, cnt, fla, bak;
  stack<int> st;
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 1; i <= N; i++) {
      cin >> ary[i];
    }
    if (ary[1] == 1)
      fla = 1;
    else
      fla = 0;
    st.push(1);
    for (int i = 2; i <= N; i++) {
      hog = ary[i];
      if (i % 2) {
        if (fla == hog) {
          st.top()++;
        } else {
          st.push(1);
          fla = 1 - fla;
        }
      } else {
        if (fla == hog) {
          st.top()++;
        } else {
          bak = st.top();
          st.pop();
          if (!st.empty()) {
            bak += st.top();
            st.pop();
          }
          bak++;
          st.push(bak);
          fla = 1 - fla;
        }
      }
    }
    cnt = 0;
    while (!st.empty()) {
      hog = st.top();
      st.pop();
      if (fla == 0)
        cnt += hog;
      fla = 1 - fla;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int ary[100010];

int main() {
  int N, hog, res, cnt;
  bool fla;
  stack<int> st;
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 1; i <= N; i++) {
      cin >> ary[i];
    }
    fla = true;
    for (int i = 1; i <= N; i++) {
      hog = ary[i];
      if (fla) {
        st.push(hog);
      } else {
        cnt = 0;
        while (st.top() != hog) {
          st.pop();
          cnt++;
          if (st.empty())
            break;
        }
        for (int j = 0; j < cnt; j++) {
          st.push(hog);
        }
        st.push(hog);
      }
      fla = !fla;
    }
    res = 0;
    while (!st.empty()) {
      if (st.top() == 0)
        res++;
      st.pop();
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
struct data {
  int color, cnt;
  data(int a, int b) {
    color = a;
    cnt = b;
  }
  data() {}
};
int main() {
  int n;
  while (cin >> n, n) {
    int a;
    cin >> a;
    int lc = a, cnt = 1;
    stack<data> S;
    for (int i = 1; i < n; i++) {
      cin >> a;
      if (lc == a) {
        cnt++;
      } else {
        if (i & 1) {
          if (!S.size()) {
            cnt++;
          } else {
            data d = S.top();
            S.pop();
            cnt += d.cnt;
            cnt++;
          }
        }

        else {
          S.push(data(lc, cnt));
          cnt = 1;
        }
        lc = a;
      }
      // cout<<i<<":"<<lc<<" "<<cnt<<endl;
    }
    S.push(data(lc, cnt));
    int sum = 0;
    while (S.size()) {
      data d = S.top();
      S.pop();
      // cout<<d.color<<" "<<d.cnt<<endl;
      if (d.color == 0)
        sum += d.cnt;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    bitset<100000> bs;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      bs[i] = a;
      if (i & 1) {
        for (int j = i - 1; j >= 0 && bs[j] != a; j--)
          bs[j] = a;
      }
    }
    cout << n - bs.count() << endl;
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cassert>
#include <cstdio>
#include <iostream>
#define F first
#define S second
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef pair<int, bool> IB;
typedef pair<IB, int> P; // P(IB(cnt,1 or 0),prev)

int main() {
  int n;
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    P p[n + 2];
    int now = 1, cnt = 0;
    rep(i, n + 2) p[i] = P(IB(-1, false), -1);
    bool deb[n + 2];

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

      // cout << "print ---- "  << i-1  <<", now = " << now<< endl;
      //  for(int j=1;j<=n;j++){
      // cout  << "P("<<p[j].F.F << ","<<p[j].F.S <<"," <<p[j].S <<")" << endl;
      //  }

      bool input;
      cin >> input;
      deb[i] = input;
      if (i == 1) {
        p[i] = P(IB(1, input), -1);
        now = 1;
        continue;
      }

      /* odd */
      if (i % 2 != 0) {
        if (input == p[now].F.S) {
          // cout << "IN!-rev " << i << " " << now << endl;
          p[now].F.F++;
        } else {
          p[i] = P(IB(1, input), now);
          // cout << "IN!" << i << " - now " << now<< endl;
          now = i;
        }
        continue;
      }

      /* even */

      if (input == p[now].F.S) {
        p[now].F.F++;
        // cout << "IN!-even1 " << i << endl;
      } else {
        // cout << "IN!-even2 " << i << ", now= "<< now << endl;
        p[now].F.F++;
        p[now].F.S = input;

        if (-1 != p[now].S) {
          // cout <<"phowaaaaaaaaaaaaaaaaa" << i << endl;
          p[p[now].S].F.F += p[now].F.F;
          int post = p[now].S;
          p[now] = P(IB(-1, false), -1);
          now = post;
        }
      }
    }

    // cout << "print ---- last"   << endl;
    // for(int i=1;i<=n;i++){
    // cout << "color = " << deb[i] << ", P("<<p[i].F.F << ","<<p[i].F.S <<","
    // <<p[i].S <<")" << endl;
    // }

    for (int i = 1; i <= n; i++)
      if (!p[i].F.S && p[i].F.F != -1)
        cnt += p[i].F.F;
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <string>
#define NN 1000000

using namespace std;

int main() {
  int n, array[NN];

  while (1) {
    for (int i = 0; i < NN; i++) {
      array[i] = 0;
    }

    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {

      cin >> array[i];

      if ((i + 1) % 2 == 0) { /*ツつョツつ、ツつキツつ、*/
        if (array[i - 1] != array[i]) {
          for (int j = i - 2; j >= 0; j--) {
            if (array[i - 1] == array[j])
              array[j] = array[i];
            else {
              break;
            }
          }
          array[i - 1] = array[i];
        }
      }

      for (int i = 0; i < n; i++) {
        cout << "array[" << i << "] = " << array[i] << endl;
      }
    }

    int count = 0;
    for (int i = 0; i < n; i++) {
      if (array[i] == 0)
        count++;
    }
    cout << count << endl;

    /*   for(int i=0;i<n;i++){
      cout << "array[" << i <<"] = " << array[i] << endl;
      } */
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;

int a[MAX], p[MAX / 2];

void solve() {
  int n;
  for (;;) {
    cin >> n;
    if (!n)
      break;
    int ans = 0;
    FOR(i, n) cin >> a[i];
    for (int i = 0; 2 * i + 1 < n; i++) {
      if (a[2 * i] == a[2 * i + 1]) {
        p[i] = a[2 * i];
        continue;
      }
      p[i] = a[2 * i + 1];
      int t = i - 1;
      while (t >= 0 && p[t] != p[i])
        p[t--] = p[i];
    }
    for (int i = 0; 2 * i + 1 < n; i++) {
      ans += (p[i] ^ 1) << 1;
    }
    if (n & 1 && a[n - 1] == 0)
      ans++;
    cout << ans << endl;
  }
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;

int a[MAX], g[MAX];

void solve() {
  int n;
  for (;;) {
    cin >> n;
    if (!n)
      break;
    FOR(i, n) cin >> a[i];
    FOR(i, n) {
      g[i] = a[i];
      if (!i & 1)
        continue;
      int t;
      for (t = i - 1; t >= 0 && g[t] != g[i]; t--)
        ;
      FORR(j, t + 1, i) g[j] ^= 1;
    }
    int tot = 0;
    FOR(i, n) tot += g[i];
    cout << n - tot << endl;
  }
}

int main() {
  solve();
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;

int main() {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<P> stone;
    for (int i = 0; i < n; i++) {
      int color;
      cin >> color;
      if (i == 0) {
        stone.push_back(P(color, 1));
      } else if (i % 2 == 0) {
        if (stone[stone.size() - 1].first == color) {
          stone[stone.size() - 1].second++;
        } else {
          stone.push_back(P(color, 1));
        }
      } else {
        if (stone[stone.size() - 1].first == color) {
          stone[stone.size() - 1].second++;
        } else {
          stone[stone.size() - 1].second++;
          stone[stone.size() - 1].first = 1 - stone[stone.size() - 1].first;
          if (stone.size() > 1) {
            stone[stone.size() - 2].second += stone[stone.size() - 1].second;
            stone.pop_back();
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < stone.size(); i++) {
      if (stone[i].first == 0)
        ans += stone[i].second;
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    int stone[n + 1];
    for (int i = 0; i < n; i++) {
      int color;
      cin >> color;
      if (i % 2 == 0 || color == stone[i - 1]) {
        stone[i] = color;
      } else {
        stone[i] = color;
        for (int j = i - 1; j >= 0; j--) {
          if (stone[i] == stone[j])
            break;
          stone[j] = 1 - stone[j];
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (stone[i] == 0)
        ans++;
    }
    cout << ans << endl;
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#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 all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  int n;
  while (cin >> n, n) {
    int a;
    cin >> a;
    vp in;
    in.pb(pii(a, 1));
    rep(i, n - 1) {
      cin >> a;
      int s = in.size();
      if (i % 2 || in[s - 1].first == a) {
        if (in[s - 1].first == a)
          in[s - 1].second++;
        else
          in.pb(pii(a, 1));
      } else {
        in[s - 1].first ^= 1;
        in[s - 1].second++;
        if (s > 1) {
          in[s - 2].second += in[s - 1].second;
          in.erase(in.begin() + s - 1);
        }
      }
    }
    int co = 0;
    rep(i, in.size()) if (!in[i].first) co += in[i].second;
    cout << co << 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;
  while (cin >> n, n) {
    vector<bool> in;
    rep(i, n) {
      bool a;
      cin >> a;
      if (i % 2)
        if (a != in[in.size() - 1])
          for (int j = in.size() - 1; j >= 0; j--) {
            if (in[j] == a)
              break;
            in[j] = a;
          }
      in.pb(a);
    }
    int co = 0;
    rep(i, n) if (in[i]) co++;
    cout << n - co << endl;
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

pii go[100005];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  while (cin >> n, n) {
    fill(go, go + 100005, pii(-1, -1));
    int head = 0, a;
    cin >> a;
    go[0] = pii(a, 1);
    for (int i = 2; i <= n; ++i) {
      cin >> a;

      if (i % 2 == 1 || go[head].first == a) {
        if (go[head].first != a) {
          head++;
          go[head] = pii(a, 1);
          continue;
        }
        go[head].second++;
        continue;
      }

      if (0 < head && go[head - 1].first == a) {
        go[head - 1].second += go[head].second + 1;
        go[head] = pii(-1, -1);
        head--;
      } else {
        go[head] = pii(a, go[head].second + 1);
      }
    }

    int ans = 0;
    for (int i = 0; i <= head; ++i) {
      if (go[i].first == 0)
        ans += go[i].second;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

int go[100005];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  while (cin >> n, n) {
    fill(go, go + 100005, -1);
    int head = 1;
    for (int i = 1; i <= n; ++i) {
      int a;
      cin >> a;
      if (i == 0 || i % 2 == 1 || go[head - 1] == a) {
        go[head] = a;
        head++;
        continue;
      }

      int targ = go[head - 1], l = head - 2;
      for (; 0 <= l; --l)
        if (go[l] != targ)
          break;

      l++;
      head++;
      fill(go + l, go + head, a);
    }

    int ans = 0;
    for (int i = 0; i < head; ++i)
      if (go[i] == 0)
        ans++;

    cout << ans << endl;
  }

  return 0;
}