#include int main() { int n; scanf("%d", &n); int i, j; int a[5003]; for (i = 0; i < n; i++) scanf("%d", &a[i]); a[n] = 2; int count[3] = { 0,0,0 }; for (i = 0; i < n; i++) count[a[i]]++; int cnt[5003][2], m = 0; cnt[0][0] = cnt[0][1] = 0; for (i = 0; i <= n; i++) { if (a[i] == 2) { m++; cnt[m][0] = cnt[m][1] = 0; } else cnt[m][a[i]]++; } int dp[5003][5003]; for (i = 0; i < 5003; i++) for (j = 0; j < 5003; j++) dp[i][j] = -1; dp[0][0] = cnt[0][0]; dp[0][cnt[0][0] + cnt[0][1]] = cnt[0][1]; for (i = 1; i < m; i++) { for (j = 0; j < 5003; j++) { if (dp[i - 1][j] >= 0) dp[i][j] = dp[i - 1][j] + cnt[i][0]; if (j >= cnt[i][0] + cnt[i][1]) { if (dp[i - 1][j - cnt[i][0] - cnt[i][1]] >= 0) { if (dp[i][j]<0 || dp[i][j]>dp[i - 1][j - cnt[i][0] - cnt[i][1]] + cnt[i][1]) dp[i][j] = dp[i - 1][j - cnt[i][0] - cnt[i][1]] + cnt[i][1]; } } } } if (dp[m - 1][count[0]] > 0) dp[m - 1][count[0]] /= 2; printf("%d\n", dp[m - 1][count[0]]); return 0; }