We are given an array of 0's and 1's. We can delete some elements (any subset, not necessarily contiguous) but we must preserve the relative order of the remaining elements. The condition: in the resulting sequence, there should be no occurrence of a 0 that comes right after a 1. i.e., we cannot have a pattern "1 0" as consecutive elements in the final CV. So the final sequence must be such that all 1's (if any) come after all 0's? Actually condition: no zero comes right after one. That means if there is a 1 at position i, then the next element (if any) cannot be 0. So the sequence could be all 0's (any number of zeros) and then optionally some ones after zeros? Wait, if it's all zeros: zeros can come after zeros, that's fine because it's not "1 then 0". If there are ones, they can appear after zeros, but they cannot be followed by a zero. So once we have a 1, all subsequent elements must be 1 (since a 0 after a 1 is prohibited). However, we might also have zeros after ones if we delete some zeros? Actually condition says: In the final sequence, no zero comes right after a one. So if the final sequence contains both 0 and 1, then any occurrence of a 1 must be followed by either nothing or a 1. So the sequence must be of the form: some zeros (maybe zero), then some ones (maybe zero). Because if there is a 1 and later a 0, then at the point of transition from 1 to 0, there would be a 0 right after a 1, which is forbidden. Thus the final sequence must be all zeros (any number) then all ones (any number). So it's a sequence that is non-decreasing (0 then 1) but not necessarily because 1 can't go down to 0. But 0 to 1 is allowed. So the final sequence must be a "sorted" sequence: all zeros followed by all ones. However, note that we could also have zeros after ones if we delete the intermediate ones? Wait if we have zeros after ones, the sequence would be ... 1 ... 0 ... In the final sequence, the 1 and 0 would appear in that order, and unless we delete either the 1 or the 0, it would be a problem. But if we keep both, the order is preserved, so the 1 will be earlier, and later there will be a 0. The condition is about consecutive elements: "no zero comes right after successful one". So it's not about any 0 after any 1, only about immediate adjacency. For example, final sequence: 1, then something else, then 0. If the "something else" is between them, then the 0 does not come right after the 1; it comes after something else. So the condition is only about consecutive pairs. So it's less strict: the sequence must avoid the pattern "1,0" as consecutive elements. But it could have 1,1,0? That would have 1,0 as consecutive? Actually in 1,1,0: the last pair is (1,0) which is 1 followed by 0 - prohibited. So that's not allowed. So after the first 1, you cannot have a 0 at any later position if that 0 is immediately preceded by a 1? Wait: If we have sequence: 1, 1, 1, 0: the last pair (1,0) is prohibited. So that's not allowed. So to avoid any 1 followed by 0 consecutively, the sequence cannot have a 0 that is directly after a 1. So if there is any 1 before a 0, there must be at least one other element between them that is not a 1? But if it's not a 1, it's a 0. But then we have 1,0,... if there's a 0 after a 1, and the 0 is not immediately after the 1 if there's something in between. However, the something in between could be a 0? But if it's a 0, then we would have 1,0,... and that pair (1,0) is immediate; if we put something else between, say we put a 1: 1,1,0, then the 1,0 is still present at the last pair. To avoid any 1,0 pair, if there is a 1 and later a 0, then between them there must be something that is not a 0? But the only possible elements are 0 and 1. So if we put a 1 between, we still have a 1,0 later. Actually let's think systematically: We have a subsequence we keep, preserving order. The condition is that for any consecutive indices in the subsequence, we cannot have (first is 1 and second is 0). So the subsequence must avoid "1,0" as a consecutive pattern.
What subsequences satisfy this? They are sequences that are "non-decreasing" but not exactly? Let's examine: For a binary sequence, avoiding the pattern 1,0 means that once we see a 1, all later elements must also be 1? Not exactly: Because if we have a 1, then the next element can be 1 or nothing, but cannot be 0. However, after a 1, we could later have a 0 if there is a 1 in between? Let's consider subsequence: 1, 1, 0: the last pair is 1,0 which is prohibited. So if there's any 0 after a 1, then the element immediately before that 0 must be something else. The only allowed element before a 0 is a 0 (since 1,0 is forbidden). So for a 0 to appear, its predecessor (if any) must be 0. That means the first element can be 0 or 1. If it's 1, then the next element must be 1 (if any). More generally, the condition says that if we have a 0 at position i (i>1) in the subsequence, then the previous element must be 0. So zeros can only appear as a contiguous block at the beginning of the subsequence. Because suppose we have a 0 that is not at the beginning. Then its previous element must be 0. So by induction, all elements before it must be 0. Thus indeed, the subsequence must be of the form: some number of zeros (maybe zero) followed by some number of ones (maybe zero). Because after the first 1 appears, you cannot have a 0 because then the 0's predecessor would be the last element before it, which could be 1 if it's the first 1? Actually if we have zeros after ones, consider sequence: 1,0. Not allowed. So to have a 0 after a 1, we need a preceding element before that 0 to be 0. But if we have 1, then later 0, the predecessor of that 0 is whatever is immediately before it. If there is at least one element between the 1 and the 0, that element must be 0 for the 0 to be allowed? Let's try: 1,1,0: predecessor of 0 is 1 -> not allowed. 1,0,0: predecessor of the first 0 after the 1 is 1? Actually sequence: 1,0,0. The second element is 0, its predecessor is 1 -> 1,0 pair, so invalid. So any 0 that appears after a 1, if it's the immediate next after the first 1, it's invalid. If we try to insert something between: the only possible element is 1. But if we insert 1, the last pair before the 0 is still 1,0. So indeed, any 0 that appears after any 1 will have some predecessor; if that predecessor is 1, it's invalid. Could it be 0? To have predecessor 0, there must be a 0 before it. But if there is a 0 before it, then we have a 0 after a 1? Actually if we have a 1, then later a 0, but there is a 0 between? That would require a 0 after the 1 and before the 0? But that 0 itself would be after the 1, and its predecessor would be... Let's try to construct: Start with 1. Then we want to eventually have a 0, but we need that 0 to be preceded by a 0. So we need to place a 0 just before that final 0, which means we need two zeros in a row after the 1. But the first of those zeros would be directly after a 1 (since after the 1, the next element we keep could be that zero). That would be 1,0 pair -> invalid. So cannot have any zero after a 1, because the first zero after any 1 would be directly after a 1 (since we are considering the subsequence order; there is no other element between the 1 and that zero unless we skip the zero, but we are including it). If there is a 1 at some position in the subsequence, and later a 0 appears, then consider the first 0 after that 1. The element immediately before that 0 in the subsequence is either the 1 itself (if there is no other element between them) or some other element. But if there is some other element between, that element must come after the 1 and before the 0. It could be a 1 or a 0? Could be a 0? If it's a 0, then that 0 is the first zero after the 1; then it would be preceded by the 1, so 1,0 pair -> invalid. If it's a 1, then the zero's predecessor is that 1 (since that 1 is the last element before the zero), still 1,0 invalid. So indeed, any zero that appears after a 1 is directly preceded by either a 1 or a 0. But if it's preceded by a 0, then that preceding 0 itself appears after the 1, and its predecessor is? By induction, the first zero after the 1 is preceded by a 1, causing violation. So it's impossible to have a 0 after a 1. Therefore, the final subsequence must have all zeros first, then all ones. That is, it must be a non-decreasing sequence (since 0 < 1, it's increasing only from 0 to 1, but cannot decrease). So final sequence is sorted: zeros then ones. So indeed, the final CV must be a subsequence that is first some zeros (maybe none) then some ones (maybe none). This is a classic problem: given binary sequence, we want to select a subsequence (preserving order) that is of the form 0...0 1...1, maximizing its length. Equivalent to: find longest subsequence that is non-decreasing (since binary, non-decreasing means 0's then 1's). But careful: a non-decreasing subsequence in binary is just a sequence that does not have a 1 followed by 0; that's exactly our condition. So the problem reduces to: find the longest non-decreasing subsequence of a binary array. Since n ≤ 100, we could do DP.
But we must check: is there any nuance? Could there be a possibility to have zeros after ones if we have deletions such that zeros are not consecutive to ones? Actually consider original: 1,0,1. If we keep all three, we have 1,0,1. The pair (1,0) is present, so invalid. If we delete the 0, we keep 1,1 which is valid. If we delete the first 1, keep 0,1: valid (0,1). If we keep 1,0? invalid. If we keep 0,1? valid. So the maximum length is 2. The longest non-decreasing subsequence is either two 1's? Actually original: [1,0,1] The longest non-decreasing subsequence is of length 2: either [1,1] (select indices 1 and 3) or [0,1] (indices 2 and 3). Indeed, maximum is 2. So seems consistent.
Thus, we need to compute the longest subsequence that is of the form 01. That is, we can choose some zeros from the original (in order) and then some ones (in order) such that all chosen zeros appear before any chosen ones. But note: the ones we choose after zeros must be after the last chosen zero in the original array. However, we could also choose only zeros or only ones.
We can solve by considering a split point: we decide to take some zeros from the prefix up to some index i (including i possibly), and then some ones from the suffix starting after i (or from i+1 onward). But careful: The zeros and ones we select can interleave? Actually since the subsequence must be zeros then ones, the chosen zeros must be in increasing order (preserved) and chosen ones must be in increasing order, and all zeros must come before all ones in the original order. That means there must exist a cut point such that all chosen zeros have original indices ≤ cut, and all chosen ones have indices ≥ cut+1? Actually they must maintain order; if we have a zero selected at index i and a one selected at index j, we need i < j (since zero appears before one in subsequence). So we could have zeros and ones interleaved in original indices? Wait, the condition that the subsequence is zeros then ones means that there is no one followed by zero in the subsequence. But does it require that all zeros appear before all ones in terms of original positions? Not necessarily; it's about the order in the subsequence. The subsequence picks elements in the original order. So if we pick a zero at index i and a one at index j, then in the subsequence the zero will come before the one if i < j. But what if we pick a one at index i and a zero at index j where i < j? Then the subsequence would have one then zero, which is invalid. So to have a valid subsequence, for any selected zero and any selected one, the zero must appear earlier in the original order than the one? Actually if we have both a zero and a one, the order in subsequence will be the order of their indices. So if we have a one at index i and a zero at index j with i < j, then the one comes before the zero in the subsequence, leading to a 1 then 0 pattern, which is forbidden. So that cannot happen. Therefore, all selected zeros must have indices less than all selected ones (strictly). So indeed, there must be a partition point such that all selected zeros are from the left part and all selected ones from the right part. However, it's also possible that some zeros appear after some ones in the original array but we don't select both; we might select only zeros from right part and no ones, or only ones from left part and no zeros. But if we select both a zero and a one, the zero must come from an earlier index than the one. So the set of indices for zeros is a subset of some prefix and the set for ones is a subset of the corresponding suffix, with no mixing. So we can choose a cut point k (0 ≤ k ≤ n) meaning we take zeros only from positions 1..k and ones only from positions k+1..n. However, note that we could also take zeros after the cut if we also take ones before? That would break the order. So not allowed.
Thus, the problem reduces to: maximize over all k from 0 to n: (number of zeros in positions 1..k) + (number of ones in positions k+1..n). Also we could consider k=n (only zeros) or k=0 (only ones). This yields the maximum length of such a subsequence. Because we can independently choose any subset of zeros from the prefix and any subset of ones from the suffix, and the subsequence order will be all zeros (in increasing order) then all ones (in increasing order). Since we want as many as possible, we would take all zeros in the prefix and all ones in the suffix. So the maximum length is max_{0<=k<=n} (Z(k) + O(n)-O(k)), where Z(k) = count of zeros in first k positions, O(k) = count of ones in first k positions, and total ones O(n). Alternatively, O_suffix = total ones - O(k). So expression: Z(k) + (total_ones - O(k)). But Z(k) + O(k) = k (since each element is either 0 or 1). So Z(k) = k - O(k). Then Z(k) + total_ones - O(k) = (k - O(k)) + total_ones - O(k) = total_ones + k - 2*O(k). That is another form.
But wait: Is it always optimal to take all zeros from prefix and all ones from suffix? Could there be a case where we skip some zeros in the prefix to allow including more ones from the suffix? The condition only requires that the selected zeros are all before selected ones. If we take all zeros in the prefix, they are before any suffix ones. There's no limit on number, so including more zeros doesn't prevent including ones from the suffix. However, if we take a zero that is after some one we want to include? No, because we are only taking zeros from the prefix and ones from the suffix; by definition, all zeros in prefix have indices <= k, all ones in suffix have indices > k, so any zero we take from prefix is before any one we take from suffix. So we can take all of them. Similarly, we can take all ones from suffix. So the maximum for a fixed k is indeed count of zeros in prefix + count of ones in suffix. Because we can always include all such elements, and they will be in the correct order (all zeros then all ones). There's no conflict. So yes.
Thus answer = max_{k=0..n} (prefix_zeros(k) + suffix_ones(k+1)). Compute prefix_zeros as cumulative zeros. Alternatively compute total ones, and for each i from 0 to n, compute zeros in first i + ones in last n-i. Since n is small (100) we can just compute directly.
But is there any nuance that we might also consider selecting only zeros or only ones? That is covered with k=0 (prefix zeros of length 0, suffix ones = total ones) yields total ones; and k=n (prefix zeros = total zeros, suffix ones = 0) yields total zeros. So it's fine.
Let's test with examples:
Example1: n=4, array: 1 1 0 1. total ones = 3, total zeros =1.
k=0: 0+3=3
k=1: zeros in first1: element 1? zero count=0; ones in suffix positions2-4: count of ones=2? Actually positions2:1, pos3:0, pos4:1 => ones=2 => total 2.
k=2: zeros first2: both 1 ->0; ones suffix pos3-4: ones=1 (only pos4) -> total1
k=3: zeros first3: includes 1,1,0 -> zeros=1; ones suffix pos4: ones=1 -> total2
k=4: zeros first4: zeros=1; ones suffix none=0 -> total1
max=3 -> output 3. matches.
Example2: n=6, array:0 1 0 0 1 0. total ones=2? Actually: positions:1:0,2:1,3:0,4:0,5:1,6:0 -> ones=2, zeros=4.
k=0: 0+2=2
k=1: zeros first1: count0? pos1=0 ->1; ones suffix pos2-6: ones: pos2=1, pos5=1 ->2? So total=3? Wait ones suffix: from 2 to 6, ones=2 => total=1+2=3.
k=2: zeros first2: positions1-2: zeros=1 (pos1) only ->1; ones suffix pos3-6: ones: pos5=1 only ->1 => total=2.
k=3: zeros first3: pos1-3: zeros: pos1,pos3 =>2; ones suffix pos4-6: ones: pos5=1 =>1 => total=3.
k=4: zeros first4: zeros: pos1,3,4 =>3; ones suffix pos5-6: ones: pos5=1 =>1 => total=4.
k=5: zeros first5: zeros: pos1,3,4,? pos5=1 no, so zeros=3? Actually pos1,3,4 are zeros, pos2=1,pos5=1, so zeros=3; ones suffix pos6: pos6=0 =>0 => total=3.
k=6: zeros all=4; ones suffix none=0 => total=4.
max=4. matches output.
Example3: n=1, 0 -> total zeros=1, ones=0. k=0: 0+0=0? Actually total ones =0, so k=0 gives 0; k=1: zeros=1, suffix ones=0 =>1. max=1. matches.
So this approach works.
But wait, is there any possibility to have a valid subsequence that is not of the form all zeros then all ones? We argued earlier it's necessary. Let's double-check with more complex sequences: Could we have something like 0,1,0 if we delete something? In the final subsequence, if we keep 0,1,0: pairs: (0,1) ok, (1,0) not allowed. So invalid. What about 0,0,1,0? The last pair (1,0) invalid. So indeed any occurrence of a 0 after a 1 leads to a direct predecessor of that 0 being something; that something if it's 1 then invalid; if it's 0, then that 0 itself is after a 1? For the first 0 after a 1, its predecessor is either the 1 (if adjacent) or some other element. That other element, if it's 0, then there is a previous 0 that is after the 1, but then the predecessor of that 0 is? It would be the element immediately before it. To have a chain of zeros after a 1, the first zero in that chain is immediately after a 1 (since the chain starts right after the 1, because if there is any element between the 1 and the first zero, that element would be something else. The only possible something else is 1? If it's 1, then the first zero is after that 1, so again the predecessor of that zero is 1. So indeed, any 0 after a 1 will be preceded by a 1 at the point of transition from ones to zeros. Since the subsequence is ordered, the first 0 after any 1 must have a predecessor that is either a 1 (if the 0 directly follows a 1) or something else? But the something else cannot be before the 1 because order; the something else would be after the 1 as well (since the 0 is after the 1, any element between them is also after the 1). So that something else must be either 0 or 1. If it's 0, then that 0 is also after the 1 and the predecessor of that 0 could be... but chain goes back until we find the first element after the 1. That first element after the 1 is either a 0 or a 1. If it's a 1, then the first 0 after that 1 would have predecessor 1. If it's a 0, then that 0 is immediately after the 1? Actually if the first element after the 1 is a 0, then that's 1,0 pair. So invalid. So indeed, any valid subsequence cannot have a 0 after a 1. So it's all zeros then all ones.
Thus the solution is straightforward.
But is there a nuance about "removing several of them (possibly zero)" meaning we can delete any elements but not reorder. Yes.
Thus answer = max_{k=0..n} (number of zeros in prefix of length k + number of ones in suffix of length n-k).
Implementation: compute prefix_zeros array where prefix_zeros[i] = number of zeros in first i elements (i from 0 to n). Similarly, suffix_ones[i] = number of ones from i to n (1-indexed). Then for k from 0 to n: total = prefix_zeros[k] + suffix_ones[k+1] (with suffix_ones[n+1]=0). Or compute total ones, and for each k, zeros = count of zeros in first k, ones in suffix = total_ones - ones_in_first_k. So we can loop through k and compute.
Given constraints n ≤ 100, trivial.
Let's confirm with some edge cases: All ones: e.g., n=5 all 1. total ones=5, zeros=0. For any k, zeros_prefix=0, suffix_ones = total_ones - ones_prefix. So expression = total_ones - ones_prefix. Max occurs when ones_prefix minimized? Actually ones_prefix at k=0 is 0, gives 5. So answer =5, valid subsequence can keep all ones, which is valid (no zeros). Good. All zeros: answer = total zeros = n, valid (no ones). Mixed but sorted already: e.g., 0,0,1,1 -> answer = n=4. Indeed we can keep all. Mixed but unsorted: the maximum is as computed.
Thus solution is simple.
But wait, are we allowed to delete any elements, but also can we keep them in the same order? Yes.
One more test: n=2: 1,0. total ones=1, zeros=1. k=0:0+1=1; k=1: zeros in first1? element1=1 -> zeros=0; ones suffix pos2:0? zero->0 => total0; k=2: zeros first2 includes 1 and0 -> zeros=1, suffix none=0 => total1. So answer=1. That's correct because keeping both gives 1,0 which is invalid, so max 1.
n=2: 0,1: answer 2. k=0:0+1=1; k=1: zeros=1, suffix ones=1 => total2; k=2: zeros=2, suffix0=0 => total2. So max 2.
Thus fine.
Given the simplicity, we can provide code. But before finalizing, double-check the problem statement: "no unsuccessful game which comes right after successful one". So indeed that is "no 0 immediately after 1". So our reasoning stands.
Now there is a possibility that the answer might be something else? Consider we might also be allowed to reorder? But the problem says "He should remove some elements from this array in such a way that no zero comes right after one." It doesn't allow swapping. So no reorder.
Thus solution done.
But given n up to 100 only, many solutions possible.
Let's also consider if we could get more by not taking all zeros in the prefix? For example, suppose prefix has many zeros but also some ones, and suffix has many ones. If we take all zeros from prefix, that's fine; but taking those zeros might cause some of the suffix ones to be before them? No, zeros are before all suffix ones because zeros indices <= k and ones indices > k. So it's fine. There's no downside.
Thus the simple formula holds.
Edge case: k=0 means prefix empty, zeros=0, suffix_ones = total_ones; that's all ones. k=n means prefix all, zeros = total_zeros, suffix_ones=0; that's all zeros.
Thus answer is max(total_ones, total_zeros, max_{k} (zeros up to k + ones after k)). But max total_ones and total_zeros are covered by k=0 and k=n respectively? Actually k=0 yields total_ones, k=n yields total_zeros. So just the max over all k covers them.
So we can implement easily.
But let's ensure we handle indexing correctly. We'll read n, read array a[1..n]. Compute total_ones = sum of a. Then we can loop k from 0 to n inclusive. For each k, compute zeros_in_prefix = k - ones_in_prefix, where ones_in_prefix = sum of a[1..k]. Then total = (k - ones_in_prefix) + (total_ones - ones_in_prefix). So we can compute ones_in_prefix cumulatively.
Pseudo:
int n; cin>>n;
vector a(n);
int total_ones = 0;
for(int i=0;i<n;i++){cin>>a[i]; total_ones+=a[i];}
int max_len=0;
int ones_prefix=0;
for(int k=0;k<=n;k++){
if(k>0) ones_prefix += a[k-1]; // because after processing first k elements
int zeros_prefix = k - ones_prefix;
int ones_suffix = total_ones - ones_prefix;
max_len = max(max_len, zeros_prefix + ones_suffix);
}
cout<<max_len;
But careful: In loop, for k=0, ones_prefix=0, zeros_prefix=0. For k=1, we need to add a[0]. So we can start with ones_prefix=0 and then for k from 0 to n, at each iteration we compute with current ones_prefix, then if k < n, we update ones_prefix += a[k] (since after processing k, next k+1 includes a[k]). But easier: loop k from 0 to n, but compute ones_prefix as sum of first k elements. We can precompute prefix sum array.
But anyway.
Now consider a tricky scenario: What if there are zeros after ones that we want to include as zeros, but we also want to include some ones that are before them? That would be invalid because order would be 1 then 0. So we must choose a split point. Our solution covers all possibilities by choosing the split.
Thus answer is correct.
But let's think if there is any alternative interpretation: "no unsuccessful game which comes right after successful one" might mean that we cannot have an unsuccessful game immediately after a successful game, but maybe we can have unsuccessful games after successful ones if there is a gap (i.e., a successful game then later an unsuccessful game, but not consecutive). The phrase "comes right after" implies immediately after, so only consecutive elements. That is what we used. Some might interpret "comes right after" as "occurs later" but that's not typical. The statement: "no unsuccessful game which comes right after successful one" means if a game is successful, the next game (immediately following) cannot be unsuccessful. So it's about adjacent pairs. So our interpretation is correct.
Thus the solution is trivial.
But maybe the problem expects DP? It's from Codeforces? Actually I recall a problem from Codeforces: "Hideo Kojima" maybe a problem from some contest, but it's likely exactly this. But there's a twist: maybe we can also delete elements to achieve that condition, but we might also be allowed to keep them in any order? No.
Check constraints: n ≤ 100. So even O(n^3) DP would be fine. But we have O(n).
Now, we should also consider whether the subsequence must be contiguous? It says "remove several of them" but not "consecutive", so it's any subsequence.
Thus answer is max_{k} prefixZeros + suffixOnes.
We can prove optimality: Any valid subsequence can be partitioned into zeros then ones. Let the last zero in the subsequence be at index i (or none). Then all zeros are from indices ≤ i, all ones from indices > i. So number of zeros ≤ zeros in prefix up to i, number of ones ≤ ones in suffix after i. So the length ≤ zeros_prefix(i) + ones_suffix(i+1). And our construction achieves exactly that for the i we choose. So optimum is max over i.
Thus solved.
Now implement.
But wait: The problem statement says: "no unsuccessful game which comes right after successful one in his CV." Could there be an interpretation that we cannot have an unsuccessful game that is directly after a successful game in the original order after deletions? That is what we used.
Thus answer is correct.
Now I'll produce final answer in Python or C++? The user asked "Print one integer — the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one." So we can just code.
But this is a reasoning chain; final output should be the solution. Since I'm ChatGPT, I should output the answer in the format they expect? The instruction says: "You are ChatGPT, a large language model...". They likely want an explanation? Actually the user gave a problem statement and asks for help. It seems like a typical competitive programming problem; they probably want the solution logic and maybe code. But as ChatGPT, we can respond with explanation and then provide code. However, the final answer should be the integer? That doesn't make sense; they want to compute it given input, but they didn't provide specific input? They provided examples. They are asking: "Help this genius of a man determine the maximum number of games he can leave in his CV." The output is the integer. But in this interaction, we are not receiving an input; we are to explain solution. So I'll provide a general solution explanation and possibly pseudo-code or actual code. The final output is not a specific integer; it's a solution.
Thus I'll provide a detailed explanation and the algorithm, and then maybe a code snippet.
Given that the user is likely preparing for coding contest, I'll present the solution in a clear manner.
I'll also consider if there is any alternative approach: DP[i][state] where state is whether we are in zeros or ones, but it's overkill.
But answer is simple: answer = max_{i=0}^{n} (cnt0[1..i] + cnt1[i+1..n]).
Thus I'll write.
Now think about edge cases: n=0? But n≥1.
Ok.
Now provide final response: Explanation, then algorithm, then complexity, then code in C++ or Python.
I'll provide code in Python for simplicity.
Let me craft the answer:
We can rephrase: The resulting sequence must be of the form 0...0 1...1. So we need to choose a split point and take all zeros to the left and all ones to the right. The answer is max over split points.
Implementation: Read n, array. Compute total_ones. Then initialize ans = 0, ones_sofar = 0. For i from 0 to n: (i represents number of elements taken as prefix) - Actually we can loop through indices as split after i. At each step, zeros = i - ones_sofar, ones = total_ones - ones_sofar, ans = max(ans, zeros + ones). Then if i < n, update ones_sofar += a[i].
That's it.
Let's test on examples:
Example2: n=6, a=[0,1,0,0,1,0], total_ones=2.
i=0: ones_sofar=0, zeros=0, ones=2 =>2, ans=2.
i=1: before update? Actually after i=0, we update ones_sofar+=a[0]? But careful: We want ones_sofar to be sum of first i elements after processing i? In loop I described: start with i=0, ones_sofar=0; then compute ans; then if i<n, ones_sofar += a[i]; then i++. That yields for i=0: compute with first 0 elements, ones_sofar=0; after, add a[0]=0, ones_sofar stays 0; i=1: compute with i=1, ones_sofar=0? That seems wrong because ones_sofar should be sum of first i elements. So alternative: precompute prefix sums.
Simpler: precompute prefix_zeros and prefix_ones arrays.
But we can also compute as: ones_prefix = 0; for i from 0 to n: zeros_prefix = i - ones_prefix; ones_suffix = total_ones - ones_prefix; ans = max(ans, zeros_prefix + ones_suffix); if i < n: ones_prefix += a[i].
This works because at i=0, we consider first 0 elements, ones_prefix=0 correct. Then we add a[0] (first element) to ones_prefix. So before i=1, ones_prefix becomes count of ones in first 1 element. At i=1, we consider first 1 element: zeros_prefix = 1 - ones_prefix (which is number of zeros in first 1), correct. So algorithm works.
Let's test with example2:
n=6, a=[0,1,0,0,1,0], total_ones=2.
Initialize ones_prefix=0, ans=0.
i=0: zeros=0-0=0, ones_suffix=2-0=2, sum=2, ans=2. Then i<6 => ones_prefix += a[0]=0 => still 0.
i=1: zeros=1-0=1, ones_suffix=2-0=2, sum=3, ans=3. Then ones_prefix += a[1]=1 => ones_prefix=1.
i=2: zeros=2-1=1, ones_suffix=2-1=1, sum=2, ans remains 3. Then ones_prefix += a[2]=0 => still 1.
i=3: zeros=3-1=2, ones_suffix=2-1=1, sum=3, ans=3. Then ones_prefix += a[3]=0 => still 1.
i=4: zeros=4-1=3, ones_suffix=2-1=1, sum=4, ans=4. Then ones_prefix += a[4]=1 => ones_prefix=2.
i=5: zeros=5-2=3, ones_suffix=2-2=0, sum=3. Then ones_prefix += a[5]=0 => ones_prefix=2.
i=6: zeros=6-2=4, ones_suffix=2-2=0, sum=4, ans=4.
So ans=4. Correct.
Works.
Now provide code.
But maybe we can even more simply compute: ans = max_{i,j}? Not needed.
Now answer.
Will there be any pitfalls? No.
Thus final answer: Provide explanation and code.
But the user might want just the number? No, it's a problem statement. So I'll give solution.
Now I'll craft final answer. The required condition—no zero immediately after a one—forces the remaining sequence to consist of some number of zeros (possibly none) followed by some number of ones (possibly none).
Thus we need to choose a subsequence (by deleting arbitrary elements) of the form (0\dots 0;1\dots1) with maximum length.
For any split point (k) ((0\le k\le n)), if we take all zeros from the first (k) positions and all ones from the last (n-k) positions, we obtain a valid subsequence of length
[
\text{zeros}[1..k] + \text{ones}[k+1..n].
]
Conversely, any valid subsequence can be seen as taking zeros only up to some index and ones only after that index, so its length is at most that expression.
Therefore, the answer is the maximum of this quantity over all (k).
We can compute it efficiently: first count the total number of ones, then scan from left to right while keeping the number of ones seen so far.
Algorithm
- Read (n) and the array (a) of 0/1.
- Let (totalOnes = \sum a_i).
- Initialize (onesPrefix = 0), (ans = 0).
- For (i = 0) to (n):
- (zerosPrefix = i - onesPrefix).
- (onesSuffix = totalOnes - onesPrefix).
- (ans = \max(ans, zerosPrefix + onesSuffix)).
- If (i < n): (onesPrefix \mathrel{+}= a_i) (add the current element to the prefix sum).
- Output (ans).
Complexity: (O(n)) time, (O(1)) extra space.
Example
For input
6
0 1 0 0 1 0
we have (totalOnes=2). The values for (k=0\ldots6) are:
(k=0: 0+2=2)
(k=1: 1+2=3)
(k=2: 1+1=2)
(k=3: 2+1=3)
(k=4: 3+1=4)
(k=5: 3+0=3)
(k=6: 4+0=4)
Maximum is (4), which matches the sample output.
Code (Python)
def solve() -> None:
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1+n]))
total_ones = sum(a)
ones_prefix = 0
ans = 0
for i in range(n+1):
zeros_prefix = i - ones_prefix
ones_suffix = total_ones - ones_prefix
ans = max(ans, zeros_prefix + ones_suffix)
if i < n:
ones_prefix += a[i]
print(ans)
if __name__ == "__main__":
solve()
This solves the problem for the given constraints.