1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <vector>
#include <set>
using namespace std;


struct City
{
    int max;
    int days;
    int total;
    int index;
    bool operator<(const City&b)
    {
        return this->max < b.max;
    }
    bool operator>(const City&b) const
    {
        return this->max > b.max;
    }
};

void v2()
{
    int T=0;
    cin >> T;
    for (int t = 0;t<T;t++){
        int n;
        cin >> n;

        std::multiset<City,  greater <City>> cities;
        int leftToSave = 0;
        int rightToSave = 0;
        bool leftMayExist= true;
        int middleCityToSave = 0;
        for (int i=0;i<n;){
            int toSave = std::getchar();
            if (toSave == 48){
                if (leftMayExist){
                    leftToSave++;
                }
                else{
                    middleCityToSave++;
                }
                i++;
            }
            else if(toSave==49){
                i++;
                leftMayExist=false;
                if (middleCityToSave==1){
                        cities.insert(City{middleCityToSave, 2, middleCityToSave, i});
                }
                if (middleCityToSave>1){
                        cities.insert(City{middleCityToSave-1, 2, middleCityToSave, i});
                }
                middleCityToSave=0;
            }
        }
        if (leftMayExist){
            std::cout << 0 << std::endl;
            continue;
        }
        if (middleCityToSave>0){
            rightToSave = middleCityToSave;
        }

        int saved = 0;
        int day = 0;
        auto iter = cities.begin();
        while(iter!=cities.end())
        {
            int alivedOnLeft = leftToSave - day;
            int alivedOnRight = rightToSave - day;
            auto bestCity = *iter;
            int alivedInCity = bestCity.total - day*2;

            if (alivedOnLeft > alivedInCity-1 && leftToSave>0 && alivedOnLeft>0)
            {
                saved+=alivedOnLeft;
                leftToSave=0;
                day++;
            }
            else if (alivedOnRight > alivedInCity-1 && rightToSave>0 && alivedOnRight>0)
            {
                saved+=alivedOnRight;
                rightToSave=0;
                day++;
            }
            else
            {
                if (alivedInCity<=0){
                    iter = cities.end();
                }
                else
                {
                    if (alivedInCity>1){
                        day+=2;
                        saved += alivedInCity-1;
                    }
                    else
                    {
                        day++;
                        saved+=alivedInCity;
                    }
                    iter++;
                }

            }

        }

        std::cout << n-saved << std::endl;
    }
}
int main()
{
    v2();


}