#include <bits/stdc++.h>

using namespace std;

int inf=1e8;

int main()
{
    //freopen("inp.in", "r", stdin);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector< vector<int> > nb_ones(2, vector<int>(n, 0));
    int aantal=0;
    for(int i=0; i<n; i++)
    {
        nb_ones[0][i]=aantal;
        if(s[i]=='0') aantal=0;
        else aantal++;
    }
    aantal=0;
    for(int i=n-1; i>=0; i--)
    {
        nb_ones[1][i]=aantal;
        if(s[i]=='0') aantal=0;
        else aantal++;
    }

    vector< vector< vector<int> > > dp(3, vector< vector<int> >(n, vector<int>(2, 0)));
    dp[0][0][0]=-inf;
    dp[0][0][1]=-inf;
    dp[1][0][0]=-inf;
    dp[1][0][1]=-inf;
    for(int curr=1; curr<n; curr++)
    {
        for(int last=0; last<=2; last++)
        {
            for(int niet_of_wel=0; niet_of_wel<=1; niet_of_wel++)
            {
                int i=curr;
                if(s[i]=='1')
                {
                    dp[last][curr][niet_of_wel]=dp[last][curr-1][niet_of_wel];
                    continue;
                }
                bool preceding_one=false;
                if(curr-1 >= 0 && s[curr-1]=='1') preceding_one=true;
                bool sequential_one=false;
                if(curr+1 < n && s[curr+1]=='1') sequential_one=true;
                bool can_move = false;
                if(preceding_one&&sequential_one) can_move=true;
                if(can_move)
                {
                    if(last==0)
                    {
                        if(niet_of_wel==0)
                        {
                            dp[0][curr][0]=max(dp[2][curr-1][0], dp[2][curr-1][1])+nb_ones[0][curr]-1;
                            if(nb_ones[0][curr]>=1) dp[0][curr][0]=max(dp[0][curr][0], max(dp[0][curr-1][0], dp[0][curr-1][1])+nb_ones[0][curr]-1);
                            dp[0][curr][0]=max(dp[0][curr][0], dp[1][curr-1][0]);
                        }
                        else
                        {
                            dp[0][curr][1]=max(dp[2][curr-1][0], dp[2][curr-1][1])+nb_ones[0][curr];
                        }
                        //dp[0][curr]=dp[2][curr-1]+nb_ones[0][curr];
                        //if(nb_ones[0][curr]>1) dp[0][curr]=max(dp[0][curr], dp[0][curr-1]+nb_ones[0][curr]-1);
                    }
                    else if(last==1)
                    {
                        if(niet_of_wel==0)
                        {
                            dp[1][curr][0]=max(dp[2][curr-1][0], dp[2][curr-1][1])+nb_ones[1][curr]-1;
                            if(nb_ones[0][curr]>1) dp[1][curr][0]=max(dp[1][curr][0], max(dp[0][curr-1][0], dp[0][curr-1][1])+nb_ones[1][curr]-1);
                            dp[1][curr][0]=max(dp[1][curr][0], dp[1][curr-1][0]+nb_ones[1][curr]-1);
                        }
                        else
                        {
                            dp[1][curr][1]=max(dp[2][curr-1][0], dp[2][curr-1][1])+nb_ones[1][curr];
                            if(nb_ones[0][curr]>1) dp[1][curr][1]=max(dp[1][curr][1], max(dp[0][curr-1][0], dp[0][curr-1][1])+nb_ones[1][curr]);
                            dp[1][curr][1]=max(dp[1][curr][1], dp[1][curr-1][0]+nb_ones[1][curr]);

                        }
                        //dp[1][curr]=dp[2][curr-1]+nb_ones[1][curr];
                        //if(nb_ones[0][curr]>1) dp[1][curr]=max(dp[1][curr], dp[0][curr-1]+nb_ones[1][curr]);

                    }
                    else
                    {
                        dp[2][curr][niet_of_wel]=max(dp[0][curr-1][0], max(dp[1][curr-1][0], dp[2][curr-1][0]));
                        dp[2][curr][niet_of_wel]=max(dp[2][curr][niet_of_wel], max(dp[0][curr-1][1], max(dp[1][curr-1][1], dp[2][curr-1][1])));
                    }
                }
                else
                {
                    if(last==0 || last==1)
                    {
                        dp[last][curr][niet_of_wel]=-inf;
                    }
                    else
                    {
                        dp[2][curr][niet_of_wel]=max(dp[0][curr-1][0], max(dp[1][curr-1][0], dp[2][curr-1][0]));
                        dp[2][curr][niet_of_wel]=max(dp[2][curr][niet_of_wel], max(dp[0][curr-1][1], max(dp[1][curr-1][1], dp[2][curr-1][1])));
                    }
                }
            }
        }
    }

    /*for(int i=0; i<=2; i++)
    {
        for(int j=0; j<n; j++)
        {
            for(int k=0; k<2; k++)
            {
                cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
            }
            cout << endl;
        }
        cout << endl;
    }*/
    /*for(int curr=1; curr<n; curr++)
    {
        for(int last=0; last<=1; last++)
        {
            int i=curr;
            if(s[i]=='1')
            {
                dp[last][curr]=dp[last][curr-1];
                continue;
            }
            bool preceding_one=false;
            if(curr-1 >= 0 && s[curr-1]=='1') preceding_one=true;
            bool sequential_one=false;
            if(curr+1 < n && s[curr+1]=='1') sequential_one=true;
            bool can_move = false;
            if(preceding_one&&sequential_one) can_move=true;
            if(can_move)
            {
                if(last==0)
                {
                    int voorganger=dp[0][curr-nb_ones[0][curr]];
                    dp[0][curr]=voorganger+nb_ones[0][curr];
                }
                else
                {
                    dp[1][curr]=max(dp[0][curr-1], dp[1][curr-1])+nb_ones[1][curr];
                }
            }
            else
            {
                if(last==0)
                {
                    dp[last][curr]=max(dp[last][curr-1], dp[1-last][curr-1]);
                }
                else
                {
                    dp[last][curr]=0;
                }
            }
        }
    }*/
    int ans=max(dp[0][n-1][0], max(dp[1][n-1][0], dp[2][n-1][0]));
    ans=max(ans, max(dp[0][n-1][1], max(dp[1][n-1][1], dp[2][n-1][1])));
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 114040