#include #include #include using namespace std; vector > v; int d[5009][5009], inf = 1000000000; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int m = 0, c0 = 0, c1 = 0; for (int i = 0; i <= n; i++) { int a = 2; if (i < n) cin >> a; if (a == 0) { c0++; m++; } else if (a == 1) c1++; else if (c0 || c1) { v.push_back(make_pair(c0 + c1, c1)); c0 = c1 = 0; } } for (int i = 1; i <= m; i++) d[0][i] = inf; for (int i = 1; i <= v.size(); i++) { int a = v[i - 1].first, w = v[i - 1].second; for (int j = 0; j <= m; j++) { d[i][j] = d[i - 1][j]; if (j >= a) d[i][j] = min(d[i][j], d[i - 1][j - a] + w); } } int ans = d[(int)v.size()][m]; if (ans >= inf) cout << -1 << '\n'; else cout << ans << '\n'; return 0; }