// Nachia くんだよ #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int N; vector A; vector> WV; vector dp; int main() { cin >> N; A.resize(N); rep(i,N) cin >> A[i]; WV.push_back({0,0}); int tgSum = 0; rep(i,N){ if(A[i] == 0 || A[i] == 1) WV.back().first++; if(A[i] == 0) tgSum++; if(A[i] == 1) WV.back().second++; if(A[i] == 2) WV.push_back({0,0}); } dp.assign(tgSum+1,1001001); dp[0] = 0; for(auto wv : WV){ for(int k=tgSum; k>=wv.first; k--){ dp[k] = min(dp[k],dp[k-wv.first]+wv.second); } } if(dp[tgSum] >= N) cout << "-1\n"; else cout << dp[tgSum] << "\n"; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;