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

#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int go[100001];
    for (int i = 0; i < n; i++) {
      scanf("%d", &go[i]);
    }
    int con[100001] = {0};
    con[0] = 1;
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        if (go[i - 1] == go[i])
          con[i] = con[i - 1] + 1;
        else
          con[i] = 1;
      } else {
        if (go[i - 1] == go[i])
          con[i] = con[i - 1] + 1;
        else {
          int add = i - 1 - con[i - 1];
          int color = (go[i - 1] == 0) ? 1 : 0;
          for (int j = i - 1; j > i - 1 - con[i - 1]; j--) {
            go[j] = color;
          }
          con[i] = con[i - 1] + 1 + con[add];
        }
      }
    }
    int cnt = 0;
    for (int i = 0; i < n; i++)
      if (go[i] == 0)
        cnt++;
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int go[100001];
    for (int i = 0; i < n; i++) {
      scanf("%d", &go[i]);
    }
    int con[100001] = {0};
    con[0] = 1;
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        if (go[i - 1] == go[i])
          con[i] = con[i - 1] + 1;
        else
          con[i] = 1;
      } else {
        if (go[i - 1] == go[i])
          con[i] = con[i - 1] + 1;
        else {
          int add;
          for (int j = i - 1; j > i - 1 - con[i - 1]; j--) {
            go[j] = (go[j] == 0) ? 1 : 0;
            add = j - 1;
          }
          con[i] = con[i - 1] + 1 + con[add];
        }
      }
    }
    int cnt = 0;
    for (int i = 0; i < n; i++)
      if (go[i] == 0)
        cnt++;
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main() {
  int a[1000000];
  bool a2[1000000];
  int n;
  while (cin >> n && n != 0) {
    int c = 0;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      bool l = x == 1 ? 1 : 0;
      if (c == 0) {
        a[0] = 1;
        a2[0] = l;
        c++;
      } else if (i % 2 == 0) {
        if (a2[c - 1] == l) {
          a[c - 1]++;
        } else {
          a[c] = 1;
          a2[c] = l;
          c++;
        }
      } else {
        if (a2[c - 1] == l) {
          a[c - 1]++;
        } else {
          if (c >= 2) {
            a[c - 2] += a[c - 1];
            a[c - 2]++;
            c--;
          } else {
            a[c - 1]++;
            a2[c - 1] = l;
          }
        }
      }
    }
    int sum = 0;
    for (int i = 0; i < c; i++) {
      if (!a2[i]) {
        sum += a[i];
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main() {
  bool a[100000];
  int n;
  while (cin >> n && n != 0) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      bool l;
      if (x == 0)
        l = false;
      else
        l = true;
      if (i % 2 == 1) {
        int j = i - 1;
        while (j >= 0 && a[j] != l) {
          if (l == false)
            sum++;
          else
            sum--;
          a[j] = l;
          j--;
        }
      }
      a[i] = l;
      if (!l)
        sum++;
    }
    cout << sum << endl;
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
int num[100005];
bool same[100005];
using namespace std;
pair<int, int> P[100005];
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &num[i]);
    }
    for (int i = 0; i < n - 1; i++) {
      if (num[i] == num[i + 1]) {
        same[i] = true;
      } else {
        same[i] = false;
      }
    }
    int ans = 0, st = 0, e = 0;
    for (int i = 0; i < n - 1; i++) {
      if (num[i] != num[i + 1]) {
        P[st] = make_pair(num[i], e);
        e = i + 1;
        st++;
      }
    }
    P[st] = make_pair(num[n - 1], e);
    for (int i = 0; i < st; i++) {
      if (P[i + 1].second % 2 == 1) {
        for (int j = i; j >= 0 && P[j].first != P[i + 1].first; j--) {
          P[j].first ^= 1;
        }
      }
    }
    for (int i = 0; i <= st; i++) {
      if (!P[i].first) {
        if (i == st) {
          ans += (n - P[i].second);
        } else {
          ans += (P[i + 1].second - P[i].second);
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
int num[100005];
bool same[100005];
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &num[i]);
    }
    for (int i = 0; i < n - 1; i++) {
      if (num[i] == num[i + 1]) {
        same[i] = true;
      } else {
        same[i] = false;
      }
    }
    int ans = 0;
    for (int i = 0; i < n - 1; i++) {
      if (!same[i]) {
        if (i % 2 == 0) {
          num[i] = 1 - num[i];
          for (int j = i - 1; num[j] != num[i] && j >= 0; j--) {
            num[j] = 1 - num[j];
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (num[i] == 0) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n;
int num[100005];
int main() {
  while (1) {
    vector<P> vec;
    vec.resize(0);
    scanf("%d", &n);
    if (!n)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &num[i]);
    }
    int st = 1;
    for (int i = 1; i < n; i++) {
      if (num[i] != num[i + 1]) {
        vec.push_back(make_pair(num[i], st));
        st = i + 1;
      }
    }
    vec.push_back(make_pair(num[n], st));
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i].second % 2 == 0) {
        int w = i;
        while (w > 0) {
          w--;
          if (vec[w].first != vec[i].first) {
            vec[w].first = 1 - vec[w].first;
          } else {
            break;
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i].first == 0) {
        if (i != vec.size() - 1) {
          ans += (vec[i + 1].second - vec[i].second);
        } else {
          ans += (n + 1 - vec[i].second);
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int num[100005], n;
int main() {
  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &num[i]);
    }
    queue<int> b, w;
    int last = -1;
    for (int i = 1; i <= n; i++) {
      if (i % 2 == 1) {
        if (last != num[i]) {
          if (num[i] == 1) {
            last = 1;
            if (!w.empty()) {
              w.pop();
            }
          } else {
            last = 0;
            if (!b.empty()) {
              b.pop();
            }
          }
        }
      } else {
        if (last != num[i]) {
          last = num[i];
          if (last == 1) {
            b.push(i);
          } else {
            w.push(i);
          }
        }
      }
    }
    vector<int> bb, ww;
    int bs = b.size();
    int ws = w.size();
    for (int i = 0; i < bs; i++) {
      bb.push_back(b.front());
      b.pop();
      b.push(bb[i]);
    }
    for (int i = 0; i < ws; i++) {
      ww.push_back(w.front());
      w.pop();
      w.push(ww[i]);
    }
    int ans = 0;
    if (bb[0] == 1) {
      for (int i = 0; i < ws; i++) {
        if (bs == ws) {
          if (i != ws - 1) {
            ans += (bb[i + 1] - ww[i]);
          } else {
            ans += (n - ww[i]);
          }
        } else {
          ans += (bb[i + 1] - ww[i]);
        }
      }
    } else {
      for (int i = 0; i < bs; i++) {
        ans += (ww[i] - bb[i]);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <vector>
#define reep(a, s, d) for (int a = d; a < s; a++)
#define rep(a, s) reep(a, s, 0)
using namespace std;
stack<int> B;
stack<int> W;
int main() {
  int n;
  while (cin >> n, n) {
    int w = 0, b = 0;
    reep(i, n + 1, 1) {
      bool c;
      cin >> c;
      if (c) {
        if (i % 2) {
          if (b > 0)
            b++;
          else {
            W.push(w);
            w = 0;
            b++;
          }
        } else {
          if (b > 0)
            b++;
          else {
            b = w + 1;
            if (!B.empty()) {
              b += B.top();
              B.pop();
            }
            w = 0;
          }
        }
      } else {
        if (i % 2) {
          if (w > 0)
            w++;
          else {
            B.push(b);
            b = 0;
            w++;
          }
        } else {
          if (w > 0)
            w++;
          else {
            w = b + 1;
            if (!W.empty()) {
              w += W.top();
              W.pop();
            }
            b = 0;
          }
        }
      }
    }
    while (!W.empty()) {
      w += W.top();
      W.pop();
    }
    cout << w << endl;
    while (!B.empty())
      B.pop();
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <vector>
#define reep(a, s, d) for (int a = d; a < s; a++)
#define rep(a, s) reep(a, s, 0)
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    stack<int> B;
    stack<int> W;
    int w = 0, b = 0;
    bool flg = false;
    reep(i, n + 1, 1) {
      bool c;
      cin >> c;
      if (c) {
        if (i % 2) {
          if (b > 0)
            b++;
          else {
            W.push(w);
            w = 0;
            b++;
          }
        } else {
          if (b > 0)
            b++;
          else {
            b = B.top() + w + 1;
            B.pop();
            w = 0;
          }
        }
      } else {
        if (i % 2) {
          if (w > 0)
            w++;
          else {
            B.push(b);
            b = 0;
            w++;
          }
        } else {
          if (w > 0)
            w++;
          else {
            w = W.top() + b + 1;
            W.pop();
            b = 0;
          }
        }
      }
    }
    while (!W.empty()) {
      w += W.top();
      W.pop();
    }
    cout << w << endl;
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define loop(i, n) for (int i = 1; i <= n; i++)
struct S {
  bool col;
  int len;
};
vector<S> v;

int main() {
  int n;
  bool c;
  while (cin >> n, n) {
    v.clear();
    loop(i, n) {
      cin >> c;
      if (i % 2 == 1) {
        if (v.empty() || v.back().col != c) {
          v.push_back((S){c, 1});
        } else {
          v.back().len++;
        }
      } else {
        v.back().col = c;
        v.back().len++;
        if (v[v.size() - 2].col == v.back().col) {
          v[v.size() - 2].len += v.back().len;
          v.pop_back();
        }
      }
    }
    int r = 0;
    for (int i = 0; i < v.size(); i++) {
      if (v[i].col == 0)
        r += v[i].len;
    }
    cout << r << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define loop(i, n) for (int i = 1; i <= n; i++)
bool c[100002];

int main() {
  int n;
  while (cin >> n, n) {
    loop(i, n) {
      cin >> c[i];
      if (i % 2 == 0) {
        int j = i - 1;
        while (1 <= j && c[j] != c[i]) {
          c[j] = c[i];
          j--;
        }
      }
      loop(j, i) cout << c[j] << " ";
      cout << endl;
    }
    int r = 0;
    loop(i, n) if (c[i] == 0) r++;
    cout << r << endl;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int st[100002] = {0};

int main() {
  int j;
  int n;
  int cnt = 0;
  cin >> n;
  while (n != 0) {
    for (int i = 0; i < n; i += 2) {
      cin >> st[i];
      if (n - i != 1) {
        cin >> st[i + 1];
        if (st[i] != st[i + 1]) {
          j = i;
          while (st[j] != st[j + 1]) {
            st[j] = st[j + 1];
            j--;
            if (j < 0) {
              break;
            }
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (st[i] == 0) {
        cnt++;
      }
    }
    cout << cnt << endl;
    cnt = 0;
    cin >> n;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int st[100002] = {0};

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

double EPS = 1e-10;

pii d[100100];

int main() {
  int n, t, ans;
  while (cin >> n && n) {
    d[0].fi = -1;
    d[0].se = 0;
    for (int i = 1; i <= n; i++) {
      cin >> t;
      d[i].fi = t;
      if (i % 2 == 1) {
        if (d[i].fi != d[i - 1].fi)
          d[i].se = 1;
        else
          d[i].se = d[i - 1].se + 1;
      } else {
        if (d[i].fi == d[i - 1].fi)
          d[i].se = d[i - 1].se + 1;
        else
          d[i].se = d[i - 1].se + d[i - 1 - d[i - 1].se].se + 1;
      }
    }
    ans = 0;
    int idx = n;
    while (idx > 0) {
      if (d[idx].fi == 0)
        ans += d[idx].se;
      idx -= d[idx].se;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int main() {
  int n, m;
  while (scanf("%d", &n) != EOF && n > 0) {
    vi v, u(2, -1);
    rep(i, n) {
      scanf("%d", &m);
      v.pb(m);
      if (i % 2 == 0)
        u[m] = i;
      else {
        if (v[i - 1] != m) {
          for (int j = u[m] + 1; j < i; j++) {
            v[j] = m;
          }
          for (; u[(m + 1) % 2] >= 0; --u[(m + 1) % 2]) {
            if (v[u[(m + 1) % 2]] != m)
              break;
          }
        }
        u[m] = i;
      }
      // cout<<i<<" ";
      // cout<<" "<<u[0]<<" "<<u[1]<<" ";
      // rep(j, i + 1) cout<<v[j];
      // cout<<endl;
    }
    int ans = 0;
    rep(i, n) {
      // cout<<v[i];
      if (v[i] == 0)
        ans++;
    }
    // cout<<endl;
    printf("%d\\n", ans);
  }
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2011/10/11 Tazoe

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    stack<int> go;

    int c;
    cin >> c;
    int p = c;
    int m = 1;

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

      if (i % 2 == 0) {
        if (c == p) {
          m++;
        } else {
          go.push(m);
          m = 1;
          p = c;
        }
      } else {
        if (c == p) {
          m++;
        } else {
          if (!go.empty()) {
            m += go.top();
            go.pop();
          }
          m++;
          p = c;
        }
      }
    }

    int cnt = 0;
    if (c == 0) {
      cnt += m;
      go.pop();
    }

    while (!go.empty()) {
      cnt += go.top();
      go.pop();
      if (go.empty())
        break;
      go.pop();
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// 2011/10/11 Tazoe

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    int go[100001];
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      int c;
      cin >> c;

      if (i % 2 == 0) {
        go[i] = c;
        if (c == 0)
          cnt++;
      } else {
        if (c == go[i - 1]) {
          go[i] = c;
          if (c == 0)
            cnt++;
        } else {
          for (int j = i - 1; j >= 0; j--)
            if (go[j] != c) {
              go[j] = c;
              if (c == 0)
                cnt++;
              else
                cnt--;
            } else {
              break;
            }
          go[i] = c;
          if (c == 0)
            cnt++;
        }
      }
    }

    cout << cnt << endl;
  }

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

#include <stdio.h>

int main() {

  int n[100001];
  int N;
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      scanf("%d", &n[i]);

    for (int i = 0; i < N; i++)
      if (i % 2 == 1) {
        for (int j = i - 1; j >= 0; j--)
          if (n[j] != n[i])
            n[j] = n[i];
          else if (n[j] == n[i])
            break;
      }
    int c = 0;
    for (int i = 0; i < N; i++)
      if (n[i] == 0)
        c++;

    printf("%d\\n", c);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int n[1000001];
int main() {
  n[1000001];
  int N, x, cou = 0, f = 0;
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;

    for (int i = 0; i < N; i++) {
      //	if(i>0)cout<<"    ["<<f<<endl;

      cin >> x;
      if (i == 0)
        n[i] = x;
      else if (i % 2 == 0)
        n[i] = x;
      else if (i % 2 == 1) {
        n[i] = x;
        /*
        if(n[i]!=n[i-1]){
        for(int j=i-1;j>=i-f-1;j--)
                n[j]=x;
        }
        */
        for (int j = i; j >= 0; j--) {
          if (n[j] != n[j - 1])
            n[j - 1] = n[j];
          else if (n[j] == n[j - 1])
            break;
        }
      }
      if (n[i] == n[i - 1]) {
        f++;
      } else if (n[i] != n[i - 1])
        f = 1;
    }
    for (int i = 0; i < N; i++)
      if (n[i] == 0)
        cou++;
    for (int i = 0; i < N; i++)
      cout << n[i];

    cout << cou << endl;
    cou = 0;
    f = 0;
    for (int i = 0; i <= N; i++)
      n[i] = 0;
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

vector<pint> go;

int main() {
  int n;
  while (n = in()) {
    go.clear();
    int i, next;
    for (i = 0; i < n; i++) {
      next = in();
      if (i % 2 == 1 && go[go.size() - 1].first != next) {
        if (go.size() == 1) {
          go[0].first = next;
        } else {
          go[go.size() - 2].second += go[go.size() - 1].second;
          go.pop_back();
        }
      }
      if (go.size() == 0 || go[go.size() - 1].first != next) {
        pint buf;
        buf.first = next;
        buf.second = 1;
        go.push_back(buf);
      } else
        go[go.size() - 1].second++;
      // int j;
      // for(j=0;j<go.size();j++)cout<<go[j].first<<" "<<go[j].second<<endl;
      // cout<<endl;
    }
    int res = 0;
    for (i = 0; i < go.size(); i++) {
      if (go[i].first == 0)
        res += go[i].second;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

vector<int> go;

int main() {
  int n;
  while (n = in()) {
    go.clear();
    int i, j, next;
    for (i = 0; i < n; i++) {
      next = in();
      if (i % 2 == 0 || go.size() == 0 || next == go[go.size() - 1])
        go.push_back(next);
      else {
        for (j = go.size() - 1; j >= 0; j--) {
          if (next != go[j])
            go[j] = next;
          else
            break;
        }
        go.push_back(next);
      }
      // for(j=0;j<go.size();j++)cout<<go[j];
      // cout<<endl;
    }
    int res = 0;
    for (i = 0; i < n; i++) {
      if (go[i] == 0)
        res++;
    }
    cout << res << endl;
  }
  return 0;
}