Problem:
碁石ならべ
問題
白と黒の碁石をテーブルの上にならべて遊ぶ.まずテーブルの左端に碁石を置く.次に左から 2 番目の場所に碁石を置く.これを n 回繰り返して n 個の碁石を横一列にならべる.ただし,新しく i 番目の碁石を置く際には,次のルールに従ってテーブル上の碁石を置き換える.
i が奇数の場合: テーブルに置いてあった碁石は置き換えず,新しい碁石を左
    から i 番目に置く.
i が偶数の場合: 新しく左から i 番目に置く碁石の色とテーブル上の右端の碁
    石の色が同じ場合は,テーブル上の碁石は置き換えず,新しい碁石を左から i
    番目に置く.そうでない場合,すなわち,新しく左から i 番目に置く碁石の色
    とテーブル上の右端の碁石の色が異なる場合は,まずテーブル上の右端の連続
    した同色の碁石を全て取り除き,i 番目の碁石と同色の碁石に置き換える.そ
    してテーブルの右端に i 番目の碁石を置く.
例えば,最初の 7 個の碁石を置いた時点で,
○○●●○○○
となっていたとする.   (○は白の碁石を,●は黒の碁石を表す. )
8 番目の碁石が白(○)の場合は,右端の碁石と同色なので,そのまま置く.し
    たがって,テーブル上の碁石は
○○●●○○○○
となる.
8 番目の碁石が黒(●)の場合は,右端の碁石(○)と色が異なるので,まず
    テーブルの右端にある 3 個の連続した白い碁石(○)を取り除き,黒い碁石
   (●)に置き換える.そして右端に 8 番目の碁石を置く.したがって,テーブ
    ル上の碁石は
○○●●●●●●
となる.
入力として置く碁石の順番が与えられたとき,n 個の碁石をならべ終わった後,テー
ブル上に置いてある白い碁石の個数を求めるプログラムを作成せよ.
入力
入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．
1 行目には正整数 n (1 ≤ n ≤ 100000) が書かれている.2 行目以降の第 i + 1 行目 (1 ≤ i ≤ n) には,i 番目に置く碁石の色を表す整数 c
i
が書かれており,c
i
が 0 なら i 番目に置く碁石の色が白であることを,1 なら i 番目に置く碁石の色が黒であることを表す.
採点用データのうち, 配点の 50% 分については, n ≤ 10000 を満たす.
n が 0 のとき入力の終了を示す. データセットの数は 10 を超えない．
出力
データセットごとに, n 個の碁石をならべ終わった後にテーブル上に置いてある白い碁石の個数を 1 行に出力する.
入出力例
入力例
8
1
0
1
1
0
0
0
0
8
1
0
1
1
0
0
0
1
0
出力例
6
2
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #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;
}
accuracy: 1
time_ms: 750
memory_kb: 3108
final_rank: 17

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

int a[100010];

int main(void){
	int i,j,k;
 
	cin.tie(0);  ios_base::sync_with_stdio(false);


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


		for(j=1;j<=n;j++){
			cin >> a[j];
			if(j%2==0){
			for(k=j-1;k>=1;k--){
				if(a[k]==a[j]) break;
				else a[k]=a[j];
			}
			}
		}


		int ans=0;
		for(j=1;j<=n;j++) if(a[j]==0) ans++;

		cout << ans << endl;


	}


	return 0;
}
accuracy: 1
time_ms: 860
memory_kb: 3516
final_rank: 20

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

typedef pair<int, int> P;
typedef pair<P, int> PP;
#define F first 
#define S second
#define INF (int) 2e9
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
int n;
vector<P> st;

int main(){
    while(cin >> n && n){
        int s = 0, e = 0, ans = 0;
        bool flag = false;
        st.clear();
        REP(i, 1, n + 1){
            int stone;
            cin >> stone;
            if(i % 2){
                if(st.empty()){
                    st.push_back(P(stone, 1));
                    continue;
                }
                if(st[st.size() - 1].F == stone){
                    st[st.size() - 1].S++;
                }else{
                    st.push_back(P(stone, 1));
                }
            }
            else{
                if(st[st.size() - 1].F == stone){
                    st[st.size() - 1].S++;
                }else{
                    if(st.size() >= 2){
                        st[st.size() - 2].S += st[st.size() - 1].S + 1;
                        st.pop_back();
                    }
                    else{
                        st[st.size() - 1].F = stone;
                        st[st.size() - 1].S++;
                    }
                    
                }               
            }
            // cout << st[st.size() - 1].F << " " << st[st.size() - 1].S << endl;

        }
        rep(i, st.size()){
            if(st[i].F == 0){
                ans += st[i].S;
            }
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3072
final_rank: 6

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<regex>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int Mod=998244353;
const int inf=mod*mod;
const double eps=1e-9;
signed main(){
	int n;
	while(cin>>n,n){
		int c;
		vector<pair<int,int>>rle;
		for(int i=0;i<n;i++){
			cin>>c;
			if(i%2==0){
				if(i==0||rle.back().first!=c)
					rle.emplace_back(c,1);
				else
					rle.back().second++;
			}else{
				if(rle.back().first==c)
					rle.back().second++;
				else{
					int num=rle.back().second+1;
					rle.pop_back();
					if(rle.empty())
						rle.emplace_back(c,num);
					else
						rle.back().second+=num;
				}
			}
		}
		int ans=0;
		for(auto x:rle)
			if(x.first==0)
				ans+=x.second;
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3344
final_rank: 12

code_snippet: #include<iostream>
#include<stack>

using namespace std;

int main()
{
	while (true)
	{
		int n;
		cin >> n;
		if (n == 0)
			break;
		stack<pair<int, int>> q;
		q.push(make_pair(-1, -1));
		int c;
		for (int i = 1; i <= n; i++)
		{
			cin >> c;
			pair<int, int> rear = q.top();
			int amount = rear.first;
			int color = rear.second;
			if (i % 2 == 1)
			{
				if (color == c)
				{
					q.pop();
					q.push(make_pair(amount + 1, c));
				}
				else
					q.push(make_pair(1, c));
			}
			else
			{
			    q.pop();
				int newAmount = amount + 1;
				pair<int, int> newRear = q.top();
				if (newRear.second == c)
				{
					newAmount += newRear.first;
					q.pop();
				}
			    q.push(make_pair(newAmount,c));
			}
		}
		int ans = 0;
		while (!q.empty())
		{
			pair<int, int> seg = q.top();
			if (seg.second == 0)
				ans += seg.first;
			q.pop();
		}
		cout << ans << endl;
	}

    //system("pause");
}
accuracy: 1
time_ms: 10
memory_kb: 3084
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (ll i = 0; i < N; i++)

int main()
{
  while(1){
  int n;
  cin >> n;
  if(n == 0) return 0;
  int nn[n];
  rep(i, n) cin >> nn[i];
  int wb[n][2] = {};
  int cur;
  int id = 0;
  if (nn[0] == 0)
  {
    cur = 0;
  }
  else
    cur = 1;
  wb[id][cur]++;

  for (int i = 1; i < n; i++)
  {
 //   cout << i+1 << " --------|" << endl;
  //  rep(ii,n) cout << nn[ii] << " |";
    if (nn[i] == nn[i - 1]) // same
    {
      wb[id][cur]++;
    }
    else // diff
    {
      cur = nn[i];
      if (i % 2 == 0) // odd
      {
     //   cout << " incre id " << id << " i " << i << endl;
        id++;
        wb[id][cur]++;
      }
      else // even
      {
        if (id == 0)
        {
          if (nn[i] == 0)
          {
            wb[id][0] = wb[id][1];
            wb[id][1] = 0;
            nn[i] = 0;
          }
          else
          {
        //    cout << " black first " << id << " " << wb[id][1] << " " << wb[id][0] << endl;
            wb[id][1] = wb[id][0];
            wb[id][0] = 0;
            nn[i] = 1;
          }
          //id--;
          wb[id][cur]++;
        }
        else // di > 1
        {
          if (nn[i] == 0)
          {
       //     cout << " white " << id << " " << wb[id-1][0] << " " << wb[id][1] << endl;
            wb[id-1][0] += wb[id][1];
            wb[id][1] = 0;
            nn[i] = 0;
          }
          else
          {
           //  cout << " black " << id << " " << wb[id-1][1] << " " << wb[id][0] << endl;
            wb[id-1][1] += wb[id][0];
            wb[id][0] = 0;
            nn[i] = 1;
          }
          id--;
          wb[id][cur]++;
        }
      }
    }
    /*
    rep(k, 2)
    {
      rep(m, 3)
      {
        cout << wb[m][k] << " * ";
      }
      cout << endl;
    }*/
  } // for
  ll cnt = 0;
  rep(i, id + 1) cnt += wb[i][0];
  cout << cnt << endl;
}
}
accuracy: 1
time_ms: 10
memory_kb: 4160
final_rank: 18

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

typedef long long ll;
void solve(int n) {
    vector<pair<int, int>> v;
    int f;
    cin >> f;
    v.push_back({f, 1});
    for (int i = 2; i <= n; i++) {
        int now;
        cin >> now;
        if (i % 2 == 1) {
            if (now == 0) {
                if (!v.back().first)
                    v.back().second++;
                else
                    v.push_back({0, 1});
            } else {
                if (v.back().first)
                    v.back().second++;
                else
                    v.push_back({1, 1});
            }
        } else {
            if (now == 0) {
                if (!v.back().first)
                    v.back().second++;
                else {
                    int cnt = v.back().second;
                    v.pop_back();
                    v.push_back({0, cnt + 1});
                }
            } else {
                if (v.back().first)
                    v.back().second++;
                else {
                    int cnt = v.back().second;
                    v.pop_back();
                    v.push_back({1, cnt + 1});
                }
            }
            while (!(v.size() == 1) && v.back().first == v[v.size()-2].first) {
                int cnt = v.back().second;
                v.pop_back();
                v.back().second += cnt;
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
        if (v[i].first == 0) ans += v[i].second;
    }
    cout << ans << endl;
}

int main() {
    while (1) {
        int n;
        cin >> n;
        if (!n) break;
        solve(n);
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3180
final_rank: 10

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <numeric>
#include <iomanip>
#include <stack>
#include <tuple>

using namespace std;

using ll = long long;

template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

const int inf = (1 << 30) - 1;

int main() {

    // オセロ？

    vector<int> output;

    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        vector<int> c(n);
        for (int i = 0; i < n; ++i) cin >> c[i];

        // first : 個数 second : 色
        stack<pair<int, int>> st;
        st.emplace(1, c[0]);
        for (int i = 1; i < n; ++i) {
            auto [cnt, color] = st.top();
            st.pop();

            if (i % 2 == 1) {
                if (c[i] == color) {
                    st.emplace(cnt + 1, color);
                }
                else {
                    int add = 0;
                    if (st.size() != 0) {
                        auto [p_cnt, p_color] = st.top();
                        st.pop();
                        add = p_cnt;
                    }
                    int new_cnt = add + cnt + 1;
                    st.emplace(new_cnt, c[i]);
                }
            }
            else { // そのまま入れる
                if (c[i] == color) st.emplace(cnt + 1, color);
                else {
                    st.emplace(cnt, color);
                    st.emplace(1, 1 - color);
                }
            }
        }

        int ans = 0;
        while (st.size()) {
            auto [cnt, color] = st.top();
            st.pop();
            if (color == 0) ans += cnt;
        }

        output.emplace_back(ans);
    }

    for (auto ans : output) cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3212
final_rank: 11

code_snippet: #include <bitset>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
#include <iomanip>
#include <stack>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <math.h>


using namespace std;

typedef long long ll;

typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define mp(x, y) make_pair(x, y)

#define rep(i, n) for(int i=0; i<(n); ++i)

#define all(v) v.begin(), v.end()
//sort( all(v) )　などと使える

//nが素数かをO(√n)で判断
bool is_prime(int n) {
    bool res = true;

    if (n == 1) return false;
    else if (n == 2) return true;

    else {
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                res = false;
                break;
            }
        }
    }

    return res;
}



int perm(int n, int r) {
    if (r == 1) return n;
    return n * perm(n - 1, r - 1);
}

int fact(int n) {
    if (n == 1) return 1;
    return fact(n - 1) * n;
}

int comb(int n, int r) {
    int a = min(n, r), b = max(n, r);
    return perm(a, b) / fact(a);
}

//aの逆元　a^(-1)
ll inv_mod(ll a, ll mod) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= mod;
    if (u < 0) u += mod;
    return u;
}


//n!
ll fact_mod(ll n, ll mod) {
    ll f = 1; for (int i = 2; i <= n; i++) f = f * (i % mod) % mod;
    return f;
}

ll pow_mod(ll x, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

//nCr(mod P)
ll comb_mod(ll n, ll r, ll mod) {
    if (r > n - r) r = n - r;
    if (r == 0) return 1;
    ll a = 1;
    for (int i = 0; i < r; i++) a = a * ((n - i) % mod) % mod;
    ll b = pow_mod(fact_mod(r, mod), mod - 2, mod);
    return (a % mod) * (b % mod) % mod;
}

bool isContain(int v, int s) {
    return s & (1 << v);
}

//unionFind ランクとりバージョン
/*
注意点
・tree.isSame(a, b)とtreeに対してisSameを使う
・スペルはisSame
*/
struct unionFind {
    vector<int> par, rnk;

    unionFind(int N) : par(N), rnk(N, 0) {
        for (int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    void unite(int x, int y) {
        x = root(x), y = root(y);

        if (x == y) return;
        else if (rnk[x] < rnk[y]) par[x] = y;
        else {
            par[y] = x;
            if (rnk[x] == rnk[y]) rnk[y]++;
        }
    }

    bool isSame(int x, int y) {
        return root(x) == root(y);
    }
};

/*クラスカル法
注意点！
・long long 型
・関数kraskulの引数は(頂点数、辺集合)の順
・unionFindはランクバージョンを使用すること
*/

struct edge {
    int s, t;
    long long cost;
};

bool comp(const edge& left, const edge& right) {
    return left.cost < right.cost;
}

vector<edge> es;
long long kraskul(int N, vector<edge> es) {
    long long res = 0;

    unionFind tree(N);

    //sortを忘れないように!
    //compを忘れないように！
    sort(es.begin(), es.end(), comp);

    for (int i = 0; i < es.size(); i++) {
        edge e = es[i];
        if (!tree.isSame(e.s, e.t)) {
            res += e.cost;
            tree.unite(e.s, e.t);
        }
    }

    return res;
}



typedef pair<int, int> Pair;

//max intはだいたい2e9
//max llはだいたい9e18

const int INF = 1e9;
const long long LINF = 1e18;

const ll mod = 100000;


int main() {
    //小数点１０コまで出力 main関数の頭に書く
    //cout << fixed << setprecision(10) << endl;
    int n;
    while (1) {
        cin >> n;
        if (n == 0) break;

        //碁石の列をvectorで管理
        //ex) 0001010011010など
        vector<int> osero;

        for (int i = 0; i < n; i++) {
            //各iに対して追加する石の色を受け取る
            int stone;
            cin >> stone;

            osero.push_back(stone);

            if (i % 2 == 0) continue;
            else {
                if (stone == osero[i - 1]) continue;
                else {
                    //処理がやや面倒
                    int j = i - 1;
                    while (osero[j] != stone && j >= 0) {
                        osero[j] = 1 - osero[j];
                        j--;

                        if (j < 0)break;
                    }
                }
            }
        }

        //white-colorの数
        int cnt = 0;
        for (int i = 0; i < osero.size(); i++) {
            if (osero[i] == 0) cnt++;
        }

        cout << cnt << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 760
memory_kb: 3572
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <cassert>
using namespace std;

bool solve(){
  int n;
  cin >> n;
  if(not n) return false;
  vector<int> C(n);
  // Black : 1
  // White : 0
  for(int i = 0; i < n; ++i){
    cin >> C[i];
  }
  vector<pair<int,int>> A;
  for(int i = 0; i < n; ++i){
    if(i%2){
      if(A.empty()) A.emplace_back(C[i],1);
      else if(A.back().first == C[i])
        ++A.back().second;
      else{
        pair<int,int> b = A.back();
        A.pop_back();
        b.first ^= 1;
        b.second += 1;
        if(A.size()){
          assert(A.back().first == b.first);
          A.back().second += b.second;
        }else{
          A.emplace_back(b);
        }
      }
    }else{
      if(A.size() and A.back().first == C[i])
        ++A.back().second;
      else
        A.emplace_back(C[i],1);
    }
  }
  int ans = 0;
  for(auto e : A){
    if(not e.first)
      ans += e.second;
  }
  cout << ans << '\n';
  return true;
}

int main(){
  while(solve());
}
accuracy: 1
time_ms: 10
memory_kb: 3436
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x)/gcd(x,y)*(y))
using namespace std;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<pair<int, int>>;
using VTIII = vector<tuple<int, int, int>>;
using Complex = complex<double>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a==b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a==b)){ a=b; return true; } return false; }
template<typename T> inline bool isin(const T& x, const T& l, const T& r){ return l <= x and x < r; }
inline ostream& fcout(int n = 10){ return cout << fixed << setprecision(n); }
inline ostream& rcout(int n = 2){ return cout << right << setw(n); }
constexpr int64_t INF = numeric_limits<int64_t>::max();
constexpr int32_t INF32 = numeric_limits<int32_t>::max();
constexpr int64_t MOD = 1000000007LL;
constexpr double PI = 3.14159265358979323846264338327950288;
constexpr double EPS = 1e-10;


int32_t main(){
    int n;
    while(cin >> n, n){
        int c;
        vector<tuple<int, int, int>> v;
        cin >> c;
        v.emplace_back(0, c, 1);
        rep(i, 1, n){
            cin >> c;
            int& vp = get<0>(v.back());
            int& vc = get<1>(v.back());
            int& vn = get<2>(v.back());
            if(i%2){
                if(vc == c) vn++;
                else {
                    int m = vn;
                    v.pop_back();
                    if(v.empty()) v.emplace_back(0, c, m+1);
                    else get<2>(v.back()) += m+1;
                }
            }
            else {
                if(vc == c) vn++;
                else v.emplace_back(vp+vn, c, 1);
            }
        }
        int ans = 0;
        for(auto t : v) if(get<1>(t) == 0) ans += get<2>(t);
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3112
final_rank: 9

code_snippet: #include<bits/stdc++.h>
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll color[12345678], ko[12345678];
signed main() {
	while (true) {
		ll n, a; cin >> n;
		if (n == 0) { break; }
		ll saigo = 0, z = 0;
		rep(i, n) {
			cin >> a; a += 2;
			if (i == 0) { color[z] = a; ko[z] = 1; z++; }
			else if (i % 2 == 0) {
				if (saigo == a) { ko[z - 1]++; }
				else { color[z] = a; ko[z] = 1; z++; }
			}
			else {
				if (saigo == a) { ko[z - 1]++; }
				else {
					if (z == 1) {
						color[z - 1] = a; ko[z - 1]++;

					}
					else {
						ko[z - 2] += 1 + ko[z - 1];
						color[z - 1] = 0; ko[z - 1] = 0; z--;
					}
				}
			}
			saigo = a;
		}
		ll ans = 0;
		rep(i, z) {
			//cout << color[i] << ' ' << ko[i] << endl;
			if (color[i] == 2) { ans += ko[i]; }
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3004
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

int N;
pair<int, int> StonesInfo[100000];

int main() {
	int num, curPos, sum;
	
	while (cin >> N, N) {
		cin >> num;
		StonesInfo[0] = { num,1 };
		curPos = 0;
		for (int i = 2; i <= N; ++i) {
			cin >> num;
			if ((i & 1) == 1) {
				if (StonesInfo[curPos].first == num) {
					++StonesInfo[curPos].second;
				}
				else {
					++curPos;
					StonesInfo[curPos] = { num,1 };
				}
				continue;
			}

			if (StonesInfo[curPos].first == num) {
				++StonesInfo[curPos].second;
				continue;
			}
			if (curPos == 0) {
				StonesInfo[curPos].first = num;
				++StonesInfo[curPos].second;
			}
			else {
				StonesInfo[curPos - 1].second += StonesInfo[curPos].second + 1;
				--curPos;
			}
		}
		sum = 0;
		for (int i = 0; i <= curPos; ++i) {
			if (StonesInfo[i].first == 0) {
				sum += StonesInfo[i].second;
			}
		}
		cout << sum << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3784
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
const int inf=1e9+7;
const ll INF=1LL<<61;
const ll mod=1e9+7;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    int i,j;
    int n;
    while(1){
        cin>>n;
        if(n==0) break;
        stack<P> st;
        int w=0;
        for(i=1;i<=n;i++){
            int x;
            cin>>x;
            if(i%2==1){
                if(i==1) st.push(P(x,1));
                else{
                    P u=st.top();
                    if(u.fi==x) st.top().se++;
                    else st.push(P(x,1));
                }
            }
            else {
                P u=st.top();
                if(u.fi==x) st.top().se++;
                else {
                    st.pop();
                    int r=1+u.se;
                    if(st.size()) {
                        r+=st.top().se;
                        st.pop();
                    }
                    st.push(P(x,r));
                }
            }
        }
        ll sum=0;
        while(st.size()){
            sum+=(1-st.top().fi)*st.top().se;
            st.pop();
        }
        cout<<sum<<endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3080
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<cmath>
#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;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3020
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }

int main() {
    
    int n;
    while (cin >> n && n) {
        stack<pii> stk;
        
        int ans = 0;
        rep(i, n) {
            int a;
            cin >> a;
            // cout << "sz " << sz(stk) << endl;
            if (i == 0) stk.push(pii(a, 1));
            else if (i % 2 == 0) { // 0indexedで奇数番目
                pii b = stk.top();
                stk.pop();
                if (b.F == a) {
                    stk.push(pii(a, b.S + 1));
                } else {
                    stk.push(b);
                    stk.push(pii(a, 1));
                }
            } else {
                pii b = stk.top();
                stk.pop();
                // cout << b.F << " " << b.S << endl;
                
                if (b.F != a && sz(stk)) {
                    pii c = stk.top();
                    stk.pop();
                    stk.push(pii(a, b.S + c.S + 1));
                } else {
                    stk.push(pii(a, b.S + 1));
                }
                
            }
            
            // cout << stk.top().F << " " << stk.top().S << endl;
            
        }
        while (!stk.empty()) {
            pii a = stk.top();
            // cout << a.F << " " << a.S << endl;
            if (a.F == 0) ans += a.S;
            stk.pop();
        }
        
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3068
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


signed main(void) {
    int n;
    while(cin >> n, n){
        vp v;
        rep(i,n){
            int t;
            cin >> t;
            if(v.size()){
                if(v.back().first == t){
                    v.back().second++;
                }else if(i%2 == 0){
                    v.push_back(pii(t,1));
                }else{
                    int tmp = v.back().second + 1;
                    v.pop_back();
                    if(v.size())v.back().second += tmp;
                    else v.push_back(pii(t,tmp));
                }
            }else{
                v.push_back(pii(t,1));
            }
            //cout << "!"<<i << endl;
            //rep(j,v.size())cout << v[j].first << " " << v[j].second << endl;
        }
        int ans = 0;
        rep(i,v.size())if(!v[i].first)ans += v[i].second;
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 2996
final_rank: 1

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>

using namespace std;


int N;
int c[100000];

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

		for (int i = 0; i < N; i++) {
			cin >> c[i];
		}

		int ans = 0;
		int ren[100000]; ren[0] = 0;
		int k = 0;
		int last = c[0];
		for (int i = 0; i < N; i++) {
			if (i % 2 == 1) {
				if (last != c[i]) {
					if (c[i] == 0) {
						ans += ren[k];						
					}
					else {
						ans -= ren[k];
					}

					if (k > 0) {
						ren[k - 1] += ren[k];
						k--;
					}					
				}
				ren[k]++;
			}
			else {
				if (last == c[i]) {
					ren[k]++;
				}
				else {
					k++;
					ren[k] = 1;
				}
			}

			if (c[i] == 0) {
				ans++;
			}

			last = c[i];
		}


		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3464
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef tuple<int,int,int>T;
typedef vector<T>VT;
int main(void){
	int n;
	int stone[100001];
	int i,j,k;
	int f,b,c;
	VT v;
	int sum;
	while(1){
		cin>>n;
		v.clear();
		if(n==0)	break;
		for(i=1;i<=n;i++)	cin>>stone[i];
		f=1;	b=1;	c=stone[i];
		v.push_back(make_tuple(f,b,c));
		for(i=2;i<=n;i++){
			if(i%2!=0){
				if(stone[i]!=stone[i-1]){
					f=i;	b=i;	c=stone[i];
					v.push_back(make_tuple(f,b,c));
				}
				else{
					get<1>(v[v.size()-1])++;
				}
			}
			else{//i%2==0
				if(stone[i]==stone[i-1]){
					get<1>(v[v.size()-1])++;
				}
				else{//stone[i]!=stone[i-1]
					v.push_back(make_tuple(i,i,stone[i]));
					/* for(j=0;j<v.size();j++)		cout<<get<0>(v[j])<<" "<<get<1>(v[j])<<" "<<get<2>(v[j])<<endl;
					cout<<endl; */
					j=v.size()-1;
					//cout<<j<<endl;
					while(j>-1){
						j--;
						if(stone[i]==get<2>(v[j]))	break;
					}
					//cout<<j<<endl;
					if(j==-1){
						v.clear();
						f=1;	b=i;	c=stone[i];
						v.push_back(make_tuple(f,b,c));
					}
					else{
						get<0>(v[v.size()-1])=get<0>(v[j]);
						for(k=v.size()-2;k>=j;k--){
							v.erase(v.begin()+k);
						}
					}
					/* for(j=0;j<v.size();j++)		cout<<get<0>(v[j])<<" "<<get<1>(v[j])<<" "<<get<2>(v[j])<<endl;
					cout<<endl; */
				}
			}
			/* for(j=0;j<v.size();j++)		cout<<get<0>(v[j])<<" "<<get<1>(v[j])<<" "<<get<2>(v[j])<<endl;
			cout<<endl; */
		}
		sum=0;
		for(i=0;i<v.size();i++){
			if(get<2>(v[i])==0){
				//cout<<get<1>(v[i])-(get<0>(v[i])-1)<<endl;
				sum+=get<1>(v[i])-(get<0>(v[i])-1);
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3408
final_rank: 13

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int cl(int a, int b) {
	return (a - (a%b)) / b;//b=0だと積む
}
int up(int a, int b) {
	return cl(a + b - 1, b);//b=0だと積む
}
int ord(int a, int p) {
	if (a%p == 0) {
		return ord(a / p, p) + 1;
	}
	else {
		return 0;
	}
}
int main() {
	int n;
	cin >> n;
	while(n!=0)
	{
	int ans = 0;
	vector<int> go(n);
	int ch, ba,fi;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (i == 0) {
			go[0] = 1;
			ch = 0;
			fi = x;
		}
		else if (i % 2 == 0) {
			if(x != ba){
				ch++;
			}
			go[ch]++;
		}
		else {
			if (x == ba) {
				go[ch]++;
			}
			else {
				if (ch == 0) {
					go[ch]++;
					fi = (fi + 1) % 2;
				}
				else {
					go[ch - 1] = go[ch - 1] + go[ch] + 1;
					go[ch] = 0;
					ch = ch - 1;
				}
			}
		}
		ba = x;
	}
	int wh = 1 - fi;
	for (int i = 0; i < n; i++) {
		ans = ans + wh * go[i];
		wh = 1 - wh;
	}
	cout << ans<<endl;
	cin >> n;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3068
final_rank: 4