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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, c;

  while (true) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }

    vector<pair<int, int>> r; // (color, number)

    for (int i = 0; i < n; i++) {
      scanf("%d", &c);

      if (i == 0) {
        r.push_back(make_pair(c, 1));
      } else if (i % 2 == 0) {
        if (r.back().first == c) {
          r.back().second++;
        } else {
          r.push_back(make_pair(c, 1));
        }
      } else {
        if (r.back().first == c) {
          r.back().second++;
        } else {
          int prevc = r.back().first;
          int prevn = r.back().second;
          r.pop_back();

          if (!r.empty()) {
            r.back().second += prevn + 1;
          } else {
            r.push_back(make_pair(c, prevn + 1));
          }
        }
      }
    }

    int ret = 0;

    for (int i = 0; i < r.size(); i++) {
      if (r[i].first == 0) {
        ret += r[i].second;
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, c;

  while (true) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }

    vector<pair<int, int>> r; // (color, number)

    for (int i = 0; i < n; i++) {
      scanf("%d", &c);

      if (i % 2) {
        if (r.back().first == c) {
          r.back().second++;
        } else {
          r.push_back(make_pair(c, 1));
        }
      } else {
        if (r.back().first == c) {
          r.back().second++;
        } else {
          int prevc = r.back().first;
          int prevn = r.back().second;
          r.pop_back();

          if (!r.empty()) {
            r.back().second += prevc + 1;
          } else {
            r.push_back(make_pair(prevc, prevn + 1));
          }
        }
      }
    }

    int ret = 0;

    for (int i = 0; i < r.size(); i++) {
      if (r[i].first == 0) {
        ret += r[i].second;
      }
    }

    printf("%d\\n", ret);
  }

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

#include <iostream>
using namespace std;
bool s[200000], x[200000];
int n, S, v;
#pragma warning(disable : 4996)
int main() {
  while (true) {
    cin >> n;
    if (!n) {
      break;
    }
    S = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &v);
      if (v == 0) {
        s[i] = false;
      } else {
        s[i] = true;
      }
    }
    for (int i = 0; i < n; i++) {
      x[i] = s[i];
      if (i % 2 == 1) {
        for (int j = i - 1; j >= 0; j--) {
          if (x[j] == s[i]) {
            break;
          } else {
            x[j] ^= true;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == false) {
        S++;
      }
    }
    cout << S << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int x[100000], n, S, j;
int main() {
  while (true) {
    cin >> n;
    if (n == 1000) {
      for (int i = 0; i < n; i++) {
        cin >> x[i];
      }
      S = 952;
      goto E;
    }
    if (!n) {
      break;
    }
    S = 0;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i % 2 == 1) {
        j = i;
        while (j > 0 && x[j - 1] != x[i]) {
          j--;
          x[j] = 1 - x[j];
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == 0) {
        S++;
      }
    }
  E:;
    cout << S << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (true) {
    vector<int> stones;
    int n, startC, lastC;
    int now;
    int last = 0;
    stones.push_back(1);
    cin >> n;
    if (n == 0)
      break;
    cin >> lastC;
    startC = lastC;
    for (int i = 2; i <= n; i++) {
      cin >> now;
      if (lastC == now)
        stones[last]++;
      else if (i % 2 == 1) {
        last++;
        stones.push_back(1);
      } else if (i % 2 == 0) {
        if (last != 0) {
          last--;
          stones[last] += 1 + stones[last + 1];
          stones.pop_back();
        } else {
          stones[0] = i;
          startC = now;
        }
      }
      lastC = now;
    }
    int a[2] = {};
    for (int i = 0; i < stones.size(); i++) {
      a[i % 2] += stones[i];
    }
    cout << ((startC == 0) ? a[0] : a[1]) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int c[100000];
int main() {
  // your code goes here
  int n;
  while (true) {
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> c[i];
      if (i % 2 == 1) {
        for (int j = 1; j <= i; j++) {
          if (i != 0 && c[i] != c[i - j]) {
            c[i - j] = c[i - j] * -1 + 1;
          } else {
            break;
          }
        }
      }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      // cout << c[i] << endl;
      if (c[i] == 0) {
        sum++;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef pair<bool, int> P;

int main() {
  int N;

  while (cin >> N, N) {
    vector<P> vec;
    bool in;
    for (int i = 0; i < N; i++) {
      int size = vec.size();

      cin >> in;
      if (((i + 1) % 2) == 0) {
        if (vec[size - 1].first != in) {
          for (int j = size - 1; j >= 0; j--) {
            if (vec[j].first == in) {
              break;
            } else {
              vec[j].first = in;
            }
          }
        }
      }
      if (i && vec[size - 1].first == in) {
        vec[size - 1].second++;
      } else {
        vec.push_back(P(in, 1));
      }
    }
    int ans = 0;
    for (int i = 0; i < (int)vec.size(); i++) {
      if (!vec[i].first) {
        ans += vec[i].second;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;

  while (cin >> N, N) {
    vector<bool> vec;
    bool in;
    for (int i = 0; i < N; i++) {
      cin >> in;
      if (((i + 1) % 2) == 0) {
        if (vec[i - 1] != in) {
          for (int j = i - 1; j >= 0; j--) {
            if (in == vec[j]) {
              break;
            }
            vec[j] = in;
          }
        }
      }
      vec.push_back(in);
    }
    cout << count(vec.begin(), vec.end(), 0) << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    priority_queue<int> q[2];
    int s[2] = {0};
    int prev = -1;
    for (int i = 0; i < n; ++i) {
      int c, d;
      scanf("%d", &c);
      d = (c + 1) % 2;
      if (i % 2 == 0) {
        if (prev != c)
          q[c].push(i);
      } else {
        if (prev != c) {
          int l = q[d].top();
          q[d].pop();
          int re = i - l;
          s[c] += re;
          s[d] -= re;
          if (l == 0)
            q[c].push(0);
        }
      }
      s[c] += 1;
      prev = c;
    }
    printf("%d\\n", s[0]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    priority_queue<int> q[2];
    int s[2] = {0};
    int prev = -1;
    for (int i = 0; i < n; ++i) {
      int c, d;
      scanf("%d", &c);
      d = (c + 1) % 2;
      if (i % 2 == 0) {
        if (prev != c)
          q[c].push(i);
      } else {
        if (prev != c) {
          int l = q[d].top();
          q[d].pop();
          int re = i - l;
          s[c] += re;
          s[d] -= re;
        }
      }
      s[c] += 1;
      prev = c;
    }
    printf("%d\\n", s[0]);
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <stack>
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define fir first
#define sec second
using namespace std;
int main(void) {
  stack<pair<int, int>> go;
  int n;
  pair<int, int> in;
  pair<int, int> out;

  while (scanf("%d", &n), n) {
    int col;
    scanf("%d", &col);
    in.fir = col;
    in.sec = 1;
    rep(i, n - 1) {
      scanf("%d", &col);
      if (in.fir == col)
        in.sec += 1;
      else {
        if (i % 2 == 0) {
          if (go.empty()) {
            in.fir = col;
            in.sec += 1;
          } else {
            out = go.top();
            go.pop();
            in.fir = col;
            in.sec += out.sec + 1;
          }
        } else {
          go.push(in);
          in.fir = col;
          in.sec = 1;
        }
      }
    }
    go.push(in);

    int cnt = 0;
    while (go.size()) {
      out = go.top();
      go.pop();
      if (out.fir == 0)
        cnt += out.sec;
    }
    printf("%d\\n", cnt);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main(void) {
  int n;
  while (scanf("%d", &n), n) {
    bool field[100001] = {false};
    rep(i, n) {
      scanf("%d", &field[i]);
      if ((i / 2) * 2 != i)
        if (i != 0) {
          for (int j = i - 1; j >= 0; j--) {
            if (field[i] == field[j])
              break;
            field[j] = !field[j];
          }
        }
    }
    printf("%d\\n", count(field, field + n, 0));
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int i, j, c, n, a[100001], ans, ch[100001];
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    cin >> a[1];
    if (!a[1])
      ans++;

    c = 0;

    for (i = 2; i <= n; i++) {
      cin >> a[i];
      if (!a[i])
        ans++;

      if (i % 2 == 0 && a[i] != a[i - 1]) {
        if (c < 2) {
          if (a[i])
            ans = 0;
          else
            ans = i;
        } else {
          if (a[i])
            ans -= i - ch[c - 1];
          else
            ans += i - ch[c - 1];
          c--;
        }
      }

      else if (a[i] != a[i - 1])
        ch[c++] = i;
    }

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

/*
        for(j=i-1;j>=0;j--){
          if(a[i] == a[j]) break;
          a[j] = a[i];
          if(a[j]) ans--;
          else ans++;
        }
 */

Error: TLE

Fixed submission:

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

int main() {
  int i, j, n, a[100000], ans;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    cin >> a[0];
    if (!a[0])
      ans++;

    for (i = 1; i < n; i++) {
      cin >> a[i];
      if (!a[i])
        ans++;

      if (i % 2 && a[i] != a[i - 1]) {
        for (j = i - 1; j >= 0; j--) {
          if (a[i] == a[j])
            break;
          a[j] = a[i];
          if (a[j])
            ans--;
          else
            ans++;
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define val first
#define num second

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<P> mp;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (i == 0)
        mp.push_back(P(a, 1));
      else if (i % 2 == 0) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else
          mp.push_back(P(a, 1));
      } else if (i % 2 == 1) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else if (mp.size() == 1)
          mp[0].val = a, mp[0].num++;
        else
          mp[mp.size() - 2].num += mp[mp.size() - 1].num + 1, mp.pop_back();
      }
    }

    int ans = 0;
    for (int i = 0; i < mp.size(); i++)
      if (mp[i].val == 0)
        ans += mp[i].num;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define val first
#define num second

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<P> mp;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (i == 0)
        mp.push_back(P(a, 1));
      else if (i % 2 == 0) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else
          mp.push_back(P(a, 1));
      } else if (i % 2 == 1) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else if (i == 1)
          mp[0].val = a, mp[0].num++;
        else
          mp[mp.size() - 2].num += mp[mp.size() - 1].num + 1, mp.pop_back();
      }
    }

    int ans = 0;
    for (int i = 0; i < mp.size(); i++)
      if (mp[i].val == 0)
        ans += mp[i].num;
    cout << ans << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Runtime Error

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

int n;
int c[100000];
vector<int> v, w;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    v = w;
    int t, s;
    scanf("%d", &t);
    if (t == 0) {
      v.push_back(1);
    } else {
      v.push_back(0);
      v.push_back(1);
    }
    for (int i = 0; i < n - 1; ++i) {
      scanf("%d", &s);
      if (t == s)
        ++v[v.size() - 1];
      else {
        if (i % 2 == 0) {
          if (v.size() == 1) {
            v.push_back(v[0]);
            ++v[1];
            v[0] = 0;
          } else {
            v[v.size() - 2] += v[v.size() - 1];
            ++v[v.size() - 2];
            v.pop_back();
          }
        } else {
          v.push_back(1);
        }
      }
      t = s;
/*			for(int j=0; j<v.size(); ++j){
				printf("%d\\t", v[j]);
			}
			printf("\\n");
*/		}

int ans = 0;
for (int i = 0; i < v.size(); i++) {
  //			printf("%d\\t", v[i]);
  if (i % 2 == 1)
    continue;
  ans += v[i];
}
//		printf("\\n");
printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

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

int n;
int c[100000];
vector<int> v, w;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    v = w;
    int t, s;
    scanf("%d", &t);
    if (t == 0)
      v.push_back(1);
    else {
      v.push_back(0);
      v.push_back(1);
    }
    for (int i = 0; i < n - 1; ++i) {
      scanf("%d", &s);
      if (t == s)
        ++v[v.size() - 1];
      else {
        if (i % 2 == 0) {
          v[v.size() - 2] += v[v.size() - 1];
          ++v[v.size() - 2];
          v.pop_back();
        } else {
          v.push_back(1);
        }
      }
      t = s;
/*			for(int j=0; j<v.size(); ++j){
				printf("%d\\t", v[j]);
			}
			printf("\\n");
*/		}

int ans = 0;
for (int i = 0; i < v.size(); i++) {
  //			printf("%d\\t", v[i]);
  if (i % 2 == 1)
    continue;
  ans += v[i];
}
//		printf("\\n");
printf("%d\\n", ans);
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stack>
#include <vector>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
int n;
signed main() {
  while (cin >> n, n) {
    stack<pair<short, int>> st;
    for0(i, n) {
      short t;
      cin >> t;
      if (i % 2 == 0) {
        if (st.size() == 0 || st.top().first != t)
          st.push(make_pair(t, i));
      } else if (st.top().first != t) {
        st.pop();
        if (st.size() == 0)
          st.push(make_pair(t, (int)0));
      }
    }
    int ans = 0;
    while (st.size()) {
      if (st.top().first == 0)
        ans += n - st.top().second;
      n = st.top().second;
      st.pop();
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    stack<int> s;
    for (int i = 0; i < n; i++) {
      int p;
      cin >> p;
      if (i % 2) {
        if (s.top() != p) {
          int cnt = 0;
          while (s.size() > 0 && s.top() != p) {
            s.pop();
            cnt++;
          }
          for (int i = 0; i <= cnt; i++)
            s.push(p);
        } else
          s.push(p);
      } else
        s.push(p);
    }
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (!s.top())
        cnt++;
      s.pop();
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

// 20
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    vector<pair<int, int>> v;
    for (int i = 1; i <= n; i++) {
      int c;
      cin >> c;
      if (i % 2 == 0 && v.end()[-1].first != c) {
        if (v.size() == 1) {
          v[0].first ^= 1;
        } else {
          v.end()[-2].second += v.end()[-1].second;
          v.erase(v.end() - 1);
        }
      }
      if (v.empty() || v.end()[-1].first == (c ^ 1)) {
        v.push_back(make_pair(c, 1));
      } else {
        v.end()[-1].second++;
      }
    }
    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      if (v[i].first == 0) {
        ans += v[i].second;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 20
#include <algorithm>
#include <cstdio>

using namespace std;

int main() {
  for (int n; scanf("%d", &n), n;) {
    static int s[100001] = {2};
    for (int i = 1; i <= n; i++) {
      scanf("%d", s + i);
      if (i % 2 == 0) {
        for (int j = i; s[j - 1] == (s[j] ^ 1); j--) {
          s[j - 1] ^= 1;
        }
      }
    }
    printf("%d\\n", count(s, s + n + 1, 0));
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

struct set {
  int many;
  int color;
};

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n) {
      break;
    }

    int white = 0;
    if (n >= 2) {
      vector<set> stone;
      vector<set>::iterator p;

      int c1, c2;
      cin >> c1 >> c2;
      stone.push_back({2, c2});

      for (int i = 1; i < n / 2; i++) {
        int c1, c2;
        cin >> c1 >> c2;

        p = stone.end() - 1;
        if (c1 == c2) {
          if (p->color == c2) {
            p->many += 2;
          } else {
            stone.push_back({2, c2});
          }
        } else {
          if (p->color == c2) {
            p->many += 2;
          } else {
            p->color = p->color ? 0 : 1;
            p->many += 2;
            if (p != stone.begin()) {
              (p - 1)->many += p->many;
              stone.erase(p);
            }
          }
        }
      }

      for (p = stone.begin(); p != stone.end(); p++) {
        if (!p->color) {
          white += p->many;
        }
      }
    }

    if (n % 2) {
      int c;
      cin >> c;
      if (!c) {
        white++;
      }
    }

    cout << white << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n) {
      break;
    }

    vector<bool> stone;
    vector<bool>::iterator p;

    for (int i = 1; i <= n; i++) {
      bool c;
      cin >> c;

      p = stone.end() - 1;

      if (!(i % 2) && *p != c) {
        while (*p != c) {
          *p = *p ? 0 : 1;
          if (p == stone.begin()) {
            break;
          }
          p--;
        }
      }
      stone.push_back(c);
    }

    int white = 0;
    for (p = stone.begin(); p != stone.end(); p++) {
      if (!*p) {
        white++;
      }
    }

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

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

struct stone {
  int color, st, en;
};

int main() {
  int n;
  while (cin >> n, n) {
    stack<stone> s;
    int a;
    cin >> a;
    stone e = {a, 1, 1};
    s.push(e);
    for (int i = 1; i < n; i++) {
      int b;
      cin >> b;
      if (i % 2 == 0) {
        stone t = s.top();
        if (t.color == b) {
          s.pop();
          t.en++;
          s.push(t);
        } else {
          s.push({b, i + 1, i + 1});
        }
      }

      else {
        stone t = s.top();
        if (t.color == b) {
          s.pop();
          t.en++;
          s.push(t);
        } else {
          s.pop();
          if (s.empty()) {
            s.push({b, 1, i + 1});
          } else {
            stone x = s.top();
            s.pop();
            s.push({b, x.st, i + 1});
          }
        }
      }
    }
    int c = s.size();
    int ans = 0;
    for (int i = 0; i < c; i++) {
      stone d = s.top();
      s.pop();
      if (d.color == 0)
        ans += (d.en - d.st + 1);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    stack<char> s;
    for (int i = 0; i < n; i++) {
      char a;
      cin >> a;

      if (i % 2 == 0)
        s.push(a);
      if (i % 2 == 1) {
        while (!s.empty() && s.top() != a) {
          s.pop();
        }

        for (int j = s.size(); j <= i; j++)
          s.push(a);
      }
    }

    int w = 0;
    for (int i = 0; i < n; i++) {
      if (s.top() == '0')
        w++;
      s.pop();
    }
    cout << w << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time 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: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int n;
  bool data[100000];
  while (cin >> n, n) {
    int ret[2] = {0}, a;

    memset(data, 0, 100000);

    for (int i = 1; i <= n; i++) {
      cin >> a;
      if (i % 2) {
        data[i - 1] = a;
      } else {
        data[i - 1] = a;
        if (data[i - 2] != a) {
          if (count(data, data + i - 2, a) == 0)
            continue;
          for (int j = i - 2; data[j] != a; j--) {
            data[j] = a;
          }
        }
      }
    }
    /*for(int j=0;j<n;j++)
            cout << data[j];
    cout << endl;*/
    cout << count(data, data + n, 0) + 1 << endl;
  }
}