#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; int main(){ int N, A, K, x=0, y=0, S=0; cin >> N; vector L, M; for (int i=0; i> A; if (A == 0) x++; else if (A == 1) x++, y++; else{ if (x == 0) continue; L.push_back(x); M.push_back(y); x = 0; y = 0; } } if (x > 0) L.push_back(x), M.push_back(y); K = L.size(); for (int i=0; i> dp(K+1, vector(S+1, 1e9)); dp[0][0] = 0; for (int i=1; i<=K; i++){ for (int j=0; j<=S; j++){ if (j-L[i-1]>=0) dp[i][j] = min(dp[i-1][j], dp[i-1][j-L[i-1]]+M[i-1]); } } if (dp[K][S] == 1e9) dp[K][S] = -1; cout << dp[K][S] << endl; return 0; }