#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") // #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") using ll = long long; constexpr int INF = 1001001001; // constexpr int mod = 1000000007; constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int dp[5005][5005][3]; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, zero = 0; cin >> N; vector A(N); for(int i = 0; i < N; ++i){ cin >> A[i]; zero += A[i] == 0; } for(int i = 0; i <= N; ++i){ for(int j = 0; j <= N; ++j){ for(int k = 0; k <= 2; ++k){ dp[i][j][k] = INF; } } } dp[0][0][2] = 0; for(int i = 0; i < N; ++i){ if(A[i] == 2){ for(int j = 0; j <= i; ++j){ for(int k = 0; k <= 2; ++k){ chmin(dp[i + 1][j][2], dp[i][j][k]); } } } else{ for(int j = 0; j <= i; ++j){ chmin(dp[i + 1][j + 1][0], dp[i][j][0] + (A[i] == 1)); chmin(dp[i + 1][j][1], dp[i][j][1] + (A[i] == 0)); chmin(dp[i + 1][j + 1][0], dp[i][j][2] + (A[i] == 1)); chmin(dp[i + 1][j][1], dp[i][j][2] + (A[i] == 0)); } } } int ans = min({dp[N][zero][0], dp[N][zero][1], dp[N][zero][2]}); if(ans == INF) ans = -1; else ans /= 2; cout << ans << endl; return 0; }