#include using namespace std; int main(){ int n; cin >> n; vectora(n); int cnt = 0; for(int i = 0; i < n;i++){ cin >> a[i]; if(a[i] == 0)cnt++; } const int inf = 1e9; vectordp(cnt + 1, inf); dp[0] = 0; int cost = 0, num = 0; a.push_back(2); for(int i = 0; i <= n; i++){ if(a[i] == 2){ for(int j = cnt; j >= 0; j--){ if(j + num <= cnt){ dp[j + num] = min(dp[j + num], dp[j] + cost); } } cost = 0; num = 0; } else{ num++; cost += a[i]; } } if(dp[cnt] == inf)cout << -1 << endl; else cout << dp[cnt] << endl; }