We are asked: given an array of n integers, we can perform a sequence of deletions subject to parity alternation. On the first move, we can delete any element. Then each subsequent move must delete an element whose parity differs from the previously deleted element's parity. We stop when no legal move can be made (i.e., no element of the required parity remains). We want to minimize the sum of elements that remain after we stop.
We can think of it as we choose a deletion order, respecting the parity alternation rule, and we stop when we cannot continue. The goal is to minimize sum of the undeleted elements. Equivalent: maximize sum of deleted elements? Actually, total sum fixed, so minimizing sum of remaining = maximizing sum of deleted elements, but there is a constraint on number of deletions we can achieve. Actually, we can delete some elements; we stop when we cannot make a move (i.e., we have no element of required parity). So we can only delete a sequence where the parities alternate, starting with any parity. And the sequence length is limited by counts of even and odd elements. In an alternating sequence, the counts of each parity used must satisfy that the first element determines the pattern. For example, if we start with even, then pattern: E, O, E, O, ... So after k moves, number of evens used = ceil(k/2) if starting with even, number of odds used = floor(k/2). If start with odd, swapped.
Thus maximum possible length of alternating deletion sequence given we have counts: e = number of even numbers, o = number of odd numbers. The max length we can achieve is something like: if e == o, we can delete all elements (2min(e,o) = n) by starting with either parity? Actually, if e == o, we can delete all if we start with odd and alternate: o, e, o, e,... final parity? Let's see: with e=o, starting with odd gives sequence length 2e (since each odd then even pair). But after last move, we have used all odds and evens? Sequence: odd, even, odd, even,... For length n = 2e, last move would be even if n even? Actually if start odd and e=o, then after 2e moves, we have used e odds and e evens, last move is even if number of moves even? Since start odd (move 1 odd), move 2 even, move 3 odd, ... so move i: odd if i odd, even if i even. For i=2e (even), last move is even. After that, we have no elements left, so we stop naturally. So it's okay. Similarly starting with even gives same. So when e==o, we can delete all.
If e > o, can we delete all? Let's see: Suppose e > o. To delete all, we need an alternating sequence of length n = e+o. If we start with even, pattern: even, odd, even, odd,... At the end, we would need evens = ceil(n/2) if start even. For n = e+o, if start even, number of evens used = ceil((e+o)/2). But we have e evens. We can't exceed e. So need ceil((e+o)/2) <= e. For e > o, (e+o) is odd? Actually e+o parity depends: if e > o, it's possible both. Let's compute difference: ceil((e+o)/2) <= e <=> (e+o+1)//2 <= e? For e > o, say e = o + d, d>0. Then (e+o)/2 = (2o+d)/2 = o + d/2. Ceil = o + ceil(d/2). Condition: o + ceil(d/2) <= o + d -> ceil(d/2) <= d, always true. But also need number of odds used = floor((e+o)/2) <= o. Floor((e+o)/2) = floor((2o+d)/2) = o + floor(d/2). Condition: o + floor(d/2) <= o -> floor(d/2) <= 0 => d <= 1. So for e > o, starting with even, the number of odds used is o + floor(d/2). If d>=2, then floor(d/2) >=1, so we would need more odds than available. So only if d <=1 i.e., e = o or e = o+1, we can satisfy both. Similarly if start with odd: then evens used = floor((e+o)/2) <= e? floor((e+o)/2) = o + floor(d/2) (since e+o = 2o+d) -> need o+ floor(d/2) <= e = o+d -> floor(d/2) <= d, always true; odds used = ceil((e+o)/2) = o+ ceil(d/2) <= o? That requires ceil(d/2) <= 0 -> d=0. So starting with odd works only if e <= o? Actually for e > o, starting with odd would require more odds than available if d>0. So to delete all, the counts must satisfy |e - o| <= 1. Indeed, if e = o, we can delete all starting either. If e = o+1, we can delete all starting with even (since then pattern even, odd, even,... yields evens = ceil(n/2) = (e+o+1)/2 = (2o+1+1)/2 = o+1 = e, odds = floor = o). If o = e+1, can delete all starting with odd. So if |e-o| <= 1, we can delete entire array; answer 0.
But if |e-o| > 1, we cannot delete all. Then we will be forced to stop with some elements remaining. The question: we can choose which elements to delete and the order to minimize sum of remaining elements. Since we want to minimize the sum left, we want to delete as many large-valued elements as possible? Actually we want to keep sum minimal, so we would like to delete large elements and keep small elements. But the parity constraint limits which elements we can delete at each step.
We can choose the starting parity and then the sequence, stopping when we cannot proceed. At that point, the remaining elements are those we haven't deleted. Since we stop when we need a parity that has no remaining element. So the deletion sequence will be an alternating sequence that ends when we attempt to delete a parity but there are no such elements left. That means after the sequence, there might be leftover elements of both parities? Actually suppose we have e > o+1. If we start with even, we can use up all odds? Let's simulate: Starting with even, then we need odd, then even, odd, etc. As long as there is at least one odd after an even, we can continue. The process stops when we need a parity that has no elements. So if we start with even and e > o, we will eventually run out of odds, leaving only evens. Because we alternate: after each odd, we need an even; but if we still have evens, we can continue. So the process ends when we need an odd and there are no odds left. At that moment, the remaining elements are all evens (the ones not yet deleted) and perhaps some odds? Actually if we run out of odds, then we cannot make the next move, so the last move was an even? Let's think: Sequence: first delete even (1), then delete odd (2), then even (3), odd (4), ... The process continues as long as both parities are available when needed. When we reach a point where we need an odd but there are no odds left, we stop. The last deleted element would be an even (since we just finished an even deletion and then need an odd). So at stop, all odds are gone (since we used all odds), but there may be some evens left (the ones not used because we couldn't continue). If we had started with odd, then we would run out of evens eventually if o > e? But e > o, starting odd would run out of evens earlier? Let's analyze both starting options for minimizing sum.
Similarly, if o > e+1, then starting with odd would eventually run out of evens, leaving only odds.
But maybe we could also decide to stop earlier voluntarily? The rule says: "Polycarp stops if he can't make a move." He can choose which element to delete at each move among those with correct parity. He could choose a strategy to stop earlier than forced? But the game continues as long as there is a legal move? Actually "Polycarp stops if he can't make a move." That implies he only stops when he cannot make a move; he cannot stop voluntarily if a move is possible. The wording: "If after some move Polycarp cannot make a move, the game ends." So it's forced: he must continue as long as there is at least one element of the required parity. So the process is deterministic given the choices of which element to delete among the allowed parity set. He can choose which element to delete, affecting which elements remain at the end, but the length of the sequence is forced by the counts and the starting parity? Actually the length may depend on choices? Because if we have extra elements of the parity that is abundant, we might be able to "skip" some? But we always must delete an element of the required parity when available. So as long as there is at least one element of the required parity, we have to delete one. But we can choose which one. That affects which elements of that parity remain for later steps, but not the fact that we will keep deleting until we run out of the other parity. Let's think carefully.
Suppose we have e evens, o odds. Consider starting parity S (even or odd). Then the sequence of required parities alternates: S, opposite, S, opposite, ... We will delete as long as the required parity for the next move has at least one element available. This is independent of which specific ones we delete. So the length of the sequence is determined solely by the counts? Let's formalize: Let we start with parity p0. Then after k moves, we have used certain counts of each parity. For the (k+1)-th move to be possible, we need that the required parity p_{k} (alternating pattern) has at least one remaining element. The process stops when we cannot proceed. Since we can always choose any element of required parity, the only factor for continuation is whether there is any element of that parity left. So the sequence length L is the maximum length such that for each step i from 1 to L, after i-1 deletions, the count of parity required at step i is >0. But this depends on the counts we have after previous deletions; however, since we delete one per step, the counts diminish. The condition is: let a_i be the required parity for move i (i=1,2,...). Then we need that the initial count of parity a_i is at least the number of times parity a_i appears among the first i moves? Actually more precisely: we need that after i-1 moves, there is at least one element of parity a_i. That is equivalent to: the initial count of parity a_i must be strictly greater than the number of times parity a_i has been used in moves 1..i-1. Since we can choose which specific ones to delete, we can always satisfy that as long as count > usage. So the sequence can proceed until one of the parities is exhausted when it is required. The maximum possible L given starting parity p0 is determined by the counts: It is the largest integer L such that for each parity, the number of times it appears in the first L moves is ≤ initial count of that parity. Since we can always arrange deletions (just pick any remaining element of required parity), the existence of such a sequence is guaranteed as long as those inequalities hold. And we can achieve L equal to the maximum possible? But must we necessarily continue until we cannot? The rules: "If after some move Polycarp cannot make a move, the game ends." This implies that after each move, we check if a next move is possible. If it is possible, we must make a move? The problem statement: "On the first move he chooses any element and deletes it; ... For each of the next moves he chooses any element with the only restriction: ... Polycarp stops if he can't make a move." This suggests that he continues as long as he can make a move. He cannot decide to stop earlier if there is a legal move, because the game ends only when he can't make a move. So he is forced to keep deleting until no move possible. So the length of the sequence is the maximum possible length that can be achieved from the starting parity? But is it always the maximum? Could there be choices that lead to earlier stoppage? Suppose we have both parities available at a step; we must delete some element of the required parity. After that deletion, the counts change. Could it be that by choice we might exhaust a parity earlier? But we always delete one element of the required parity, so the count of that parity decreases by 1. The other parity count stays same. So the state after each move is deterministic in terms of counts (given the sequence of required parities). The only way to stop earlier is if after some move, the required parity for the next move is not available. But if we have both parities available, after deleting an element of the required parity, the next required parity is the opposite. Could it become unavailable? Only if the opposite parity count is zero. But if both were available before deletion, after deletion of the required parity, the opposite parity count remains unchanged (still >0), so the next move is possible. Thus, if at step i, we have required parity p_i, and there is at least one element of parity p_i, we delete one. After that, for step i+1, required parity is opposite. Now, the availability of opposite parity depends solely on its count, which hasn't changed. So if opposite count >0, we can continue. Therefore, once we start, the process will continue until we attempt to use a parity that has count 0 at that moment. So the only way we stop is when we need a parity that is exhausted.
Now, given initial counts e and o, and starting parity p0, what is the length of the forced sequence? Let's simulate:
Case start with even (E). Move1: delete an even, provided e>=1. After move1, e' = e-1, o' = o.
Move2: need odd; if o>=1, we delete an odd: o' = o-1, e' = e-1.
Move3: need even; if e-1 >=1? Actually even count now e-1; need even. So as long as e-1 >=1 => e>=2, we can delete even: e' = e-2, o' = o-1.
Move4: need odd; if o-1 >=1 => o>=2, we delete odd: o' = o-2, e' = e-2.
And so on.
Thus the process continues alternating. It will stop when the required parity count is zero. This will happen when we have used up all of one parity. Which parity gets exhausted first? It depends on counts.
If start with even:
- The number of evens used after k moves: ceil(k/2)
- The number of odds used after k moves: floor(k/2)
But these uses cannot exceed initial counts. The process stops at the smallest k such that for the next move (k+1) the required parity's count minus usage so far is 0. Equivalently, we can find maximum L such that for all steps i ≤ L, the usage constraints hold, and then the game actually goes for L moves, after which step L+1 impossible. But wait, the process continues as long as step i is possible. So the actual number of moves performed is the largest integer L for which all steps 1..L are possible. So L = max number of moves we can do. That is determined by the condition that after L moves, we have not exhausted any parity for the next required parity? Actually we need that for each step i from 1 to L, at the beginning of step i, the required parity count >0. So after L moves, we may have exhausted a parity? After L moves, the next step (L+1) might be impossible. So the process stops after L moves.
Now what is L given starting parity? For start even, we require:
- For each even step (odd index): move number i odd => required even. So after i-1 moves, evens used = floor((i-1)/2)? Actually careful: For step i (1-indexed), parity needed = even if i odd, odd if i even. At start of step i, we have used evens = number of odd indices < i = ceil((i-1)/2) ? Let's derive condition: For step i (i odd), we need an even. The number of evens used before step i is number of odd indices < i = (i-1)/2. So we need e - (i-1)/2 > 0 => (i-1)/2 < e => i <= 2e. For i even, need odd; odds used before step i = i/2 - 1? Actually for step i even, odds used before = number of even indices < i = i/2 - 1? Let's compute: i even, i=2k. Before step i, we have made i-1 moves. Among them, evens used: k? Actually for moves 1..i-1: odd moves (1,3,...,i-1 if i-1 odd?) maybe easier: use iterative approach.
Alternative: The maximum possible L is the largest integer L such that:
If start even:
- Number of evens used in first L moves ≤ e
- Number of odds used in first L moves ≤ o
And also we must be able to perform step L+1? Actually to perform L moves, we only need those inequalities for moves 1..L. After L moves, we stop if step L+1 not possible; we don't need step L+1 to be possible. So L can be as high as the largest number where both usages are ≤ respective counts. However, is it always achievable to reach that L? Possibly there is also a condition on the order: Since moves alternate, the pattern is fixed. The usage counts for a sequence of length L are:
evens_used = ceil(L/2) if start even? Actually for start even, moves: 1 (even),2(odd),3(even),4(odd),... So number of evens = number of odd indices ≤ L = ceil(L/2). Number of odds = floor(L/2). So we need ceil(L/2) ≤ e and floor(L/2) ≤ o. The maximum L is the largest integer satisfying both.
But note that after we have performed L moves, we might have remaining elements. Could we stop earlier than max L? Suppose we choose to delete certain elements, but the process is forced: at each step, if a move is possible, we must make a move. So as long as the conditions for step i+1 hold (i.e., the required parity count >0), we are forced to perform step i+1. So we cannot arbitrarily stop earlier. However, is it possible that by our choice of which element to delete at a step, we could affect whether a future step becomes impossible? Since the only factor for continuation is the count of each parity, not which specific elements remain. Deleting any element reduces the count of that parity by 1. So the counts evolve deterministically: after each move, the count of the parity we just deleted decreases by 1. The counts of the other parity remain unchanged. So regardless of which element we delete, the counts will be exactly determined by how many moves of each parity we've made. So after k moves, evens left = e - (#even moves), odds left = o - (#odd moves). Since the numbers of each parity moves are fixed by k and starting parity, the counts left are deterministic. So the process can continue as long as both future required moves have the needed parity count > 0. But after k moves, the next required parity is known. So whether step k+1 is possible depends on whether the count of that parity after k moves is >0. That count is deterministic: if next parity is even, count = e - (#even moves so far) which is e - ceil(k/2) if start even. For step k+1 to be possible, we need that count > 0. So the maximum L such that all steps 1..L are possible is exactly the maximum integer L for which for each step i from 1 to L, the required parity count before that step >0. But given that the counts only decrease, the limiting factor is when we attempt a move and find count zero. This happens at the first i where the required parity count is zero. That i is the smallest i such that either:
- if i is odd (even move) and e - ceil((i-1)/2) == 0? Actually before step i, evens used = number of even moves among first i-1 moves. For start even, even moves occur on odd indices. So number of even moves among first i-1 = floor((i)/2)? Let's compute formula:
Let i be the step number (1-indexed). Before step i, we have performed i-1 moves. Among those:
- If start even:
- number of even moves = number of odd numbers in [1, i-1] = ceil((i-1)/2)
- number of odd moves = floor((i-1)/2)
Now step i requires:
- if i odd: even => need e - ceil((i-1)/2) > 0
- if i even: odd => need o - floor((i-1)/2) > 0
The process will stop at the smallest i for which the needed condition fails. That i is the first step we cannot perform; so the number of moves actually performed is i-1. So we need to compute the largest L such that for all i ≤ L, condition holds. But that is equivalent to saying we can perform moves as long as both: for all steps up to some number, the required counts are positive. This basically means the process continues until we try to make a move that requires a parity that has been exhausted. Which parity gets exhausted first? That depends on the counts.
Let's determine L given e and o and start parity p0.
Simplify: For start even, we alternate: E,O,E,O,... The process will continue until we run out of either evens when an even move is required, or odds when an odd move is required. Since we start with even, the first move uses an even. After that, we need odd, then even, etc. So after k moves, evens used = ceil(k/2), odds used = floor(k/2). The next move required parity is: if k is even (so just completed an odd move), next is even; if k is odd (just completed an even move), next is odd. So we can think in terms of after k moves, can we make the (k+1)-th move? Condition: if k is even, need even count > 0: e - ceil(k/2) > 0. If k is odd, need odd count > 0: o - floor(k/2) > 0. The process will continue until condition fails.
Now, since e and o are integers, we can find the maximum k such that both inequalities hold for all moves up to k. Actually, we need to check the condition for each step, but it might fail at some k where either e - ceil(k/2) becomes 0 before an even move, or o - floor(k/2) becomes 0 before an odd move.
We can think of the process as: while true, try to perform next move. It will eventually stop when either we need an even and no evens left, or need an odd and no odds left.
Consider start even. The sequence of required parities: move1: even; move2: odd; move3: even; move4: odd; ... So the evens are used on moves 1,3,5,... and odds on moves 2,4,6,... The process will continue until one of these moves cannot be made because that parity is exhausted. Since evens are used on odd moves, the condition for move (2t+1) (t>=0) is that e > t. Because before move (2t+1), number of evens used = t (since previous even moves: moves 1,3,...,2t-1? Actually move (2t+1) is the (t+1)-th even move. So condition: e - t > 0 => e >= t+1. So the last possible even move is when t = e-1, i.e., move number 2(e-1)+1 = 2e-1. After that, if we attempt another even move (move 2e+1?), we would need e >= e+1 which fails. So the maximum number of even moves we can make is e. Similarly, odds are used on even moves: move 2,4,...,2t. For move 2t (t>=1), condition: o >= t (since before move 2t, odds used = t-1? Let's check: before move 2t (even), we have performed 2t-1 moves. Number of odd moves among those = t-1 (since odds occur on moves 2,4,...,2t-2). So odds remaining = o - (t-1). Need >0 => o >= t. So the condition for move 2t is o >= t. So the last possible odd move is when t = o, i.e., move number 2o. After that, if we attempt another odd move (move 2(o+1)?), condition fails.
Now, the process will stop at the first move that cannot be made. So the actual number of moves L is determined by whichever parity gets exhausted first in the alternating schedule. Since we alternate, we will stop when we need to make a move that requires a parity that has been exhausted. That will happen at the move number equal to min(2e+1? Actually need to compute based on comparison of e and o.
Let's think: We start with move1 (even). We can do move1 if e>=1. Then move2 (odd) if o>=1. Then move3 (even) if e>=2. Then move4 (odd) if o>=2, etc.
So we can continue as long as both: for even moves up to the k-th even move, e>=k; for odd moves up to the l-th odd move, o>=l. But since they interleave, the process will go on until we attempt to make an even move that requires e>=k+1 but e = k, or an odd move that requires o>=l+1 but o = l. The point of failure depends on which parity is limiting.
If e > o, then odds are fewer. After using o odds, we cannot make the next odd move. The last odd move that can be made is move 2o (if that is within the sequence). After that, we will attempt move 2o+1? Actually after move 2o (which is odd? Wait move numbers: odd moves: 1,3,5,... even moves:2,4,6,... So move 2o is an even move? Let's recalc: move number 2t is even and requires odd? Actually move 2t requires odd? Wait earlier: For start even, move1: even, move2: odd, move3: even, move4: odd,... So odd-indexed moves are even, even-indexed moves are odd. That seems swapped: I'm mixing. Let's set: Start even => move1: even, move2: odd, move3: even, move4: odd,... So indeed, odd-numbered moves require even parity, even-numbered moves require odd parity. So move number 2t (even) requires odd. So the t-th odd move corresponds to move number 2t. So the condition for odd move number 2t is o >= t. So the last possible odd move is when t = o, i.e., move number 2o. After that, the next move would be move number 2o+1, which requires even. At that point, we have used o odds (all). And we have used how many evens? Up to move 2o, number of evens used = number of odd-numbered moves ≤ 2o? The odd-numbered moves up to 2o are 1,3,5,...,2o-1: that's o moves. So evens used = o. So after move 2o, evens left = e - o. Since e > o, e - o >= 1, so we still have evens. So move 2o+1 (requires even) is possible because e - o >= 1? Actually we need even count > 0. Evens remaining = e - o (since we used o evens so far). Since e > o, e - o >= 1, so yes we can perform move 2o+1. Then after move 2o+1, evens used becomes o+1, evens left = e - o -1. Now move 2o+2 would require odd, but odds are zero (since we used all o odds). So we cannot make move 2o+2. So the process stops after move 2o+1. So total moves L = 2o+1. That uses o odds and o+1 evens. So total deletions = 2o+1. Remaining elements: evens left = e - (o+1), odds left = 0. So if e > o, starting with even yields deletions = 2o+1, leaving e - o -1 evens.
What if we start with odd? Then symmetrical: If e > o, starting with odd would lead to using evens until odds run out? Let's analyze start odd with e > o. Start odd: move1: odd, move2: even, move3: odd, move4: even,... So odd-indexed moves are odd, even-indexed moves are even. Then number of odds used after k moves: ceil(k/2); evens used: floor(k/2). Conditions: odd moves require o >= ceil(k/2) at step k if k odd? Better to think in terms of t-th moves. For start odd:
- odd moves (moves 1,3,5,...): t-th odd move is move number 2t-1, requires o >= t.
- even moves (moves 2,4,6,...): t-th even move is move number 2t, requires e >= t.
Now if e > o, the limiting parity might be odds because o < e? Actually odds are fewer? Wait e > o, so odds are fewer than evens. So odds will be exhausted first. The last odd move possible is when t = o, i.e., move number 2o-1. After move 2o-1, odds used = o, odds left =0. Evens used so far: up to move 2o-1, number of even moves = floor((2o-1)/2) = o-1. So evens left = e - (o-1). Now next move is move 2o (requires even). Since evens left >0 (e - (o-1) >= 2 because e > o implies e >= o+1, so e - (o-1) >= 2), we can make move 2o. After move 2o, evens used become o, evens left = e - o. Then move 2o+1 requires odd, but odds are zero, so stop. So total moves L = 2o. Deletions = 2o, using all o odds and o evens? Actually used odds = o, used evens = o. Remaining: evens left = e - o, odds left = 0. So starting with odd yields deletions = 2o (all odds and o evens). Compare with starting even: deletions = 2o+1 (all odds and o+1 evens). So starting even yields one more deletion (an extra even) if e > o. But note: we are forced to continue until cannot. So if we start with even, we will end up deleting one more element (an extra even) than if we start with odd, assuming e > o. But is it always forced? Let's test with e=5, o=2. Starting even: sequence: E, O, E, O, E? Let's simulate: moves: E1 (e:4, o:2); O1 (e:4, o:1); E2 (e:3, o:1); O2 (e:3, o:0); now next required parity? Last deleted was O2, so need even. Evens left =3 >0, so we can do E3 (e:2, o:0). After that, need odd but o=0, stop. So deletions: 5? Actually we did 5 moves: E1,O1,E2,O2,E3 = 5. Deleted 3 evens and 2 odds. Remaining: 5-3=2 evens. So deletions=2o+1=5? Wait o=2, 2o+1=5, yes. Starting odd: start O: O1 (e:5, o:1); then E1 (e:4, o:1); O2 (e:4, o:0); then need even: E2 (e:3, o:0); then need odd but none, stop. Deletions: O1, E1, O2, E2 = 4 moves? Actually that's 4. But after O2, we had o=0, then need even, so we did E2, that's move4. Then need odd, stop. So deletions=4, which is 2o=4. Remaining: evens left = 5-2=3? Wait we used evens: E1 and E2 =2 evens, so left e=5-2=3. So remaining sum = sum of 3 evens left.
Thus starting even yields more deletions (one extra even) than starting odd when e > o. So to minimize sum remaining, we would like to delete as many elements as possible, i.e., maximize deletions. But also we can choose which specific elements to delete among each parity, so we would delete the largest values to minimize remaining sum. Since the total number of deletions of each parity is fixed by the starting parity choice and the counts, the optimal strategy is: given we will delete exactly E_del evens and O_del odds, we should delete the largest E_del even numbers and the largest O_del odd numbers to minimize the sum of remaining (or maximize sum deleted). So the problem reduces to: for each possible starting parity (which gives a certain number of deletions of each parity), compute the sum of the largest elements of each parity that we can delete, subtract from total sum to get remaining sum, and then take the minimum over the two choices (if both are possible). Also, note that if e and o are such that starting with a particular parity might not be feasible? Actually starting parity must have at least one element of that parity to make the first move. So if we choose to start with even but e=0, that's not allowed because we cannot make first move. Similarly for odd. So we must consider only starting parities that have at least one element. Also, the process will be forced as described; but we must verify that the forced sequence length is indeed determined solely by counts, regardless of which specific elements we delete. Because as argued, counts after k moves are deterministic: number of evens deleted = number of times even parity was required up to move k, which is determined by starting parity and k. And since we must continue as long as a move is possible, the game will always achieve the maximum possible k? Or could there be a scenario where we might get stuck earlier because we choose to delete elements in such a way that we exhaust a parity prematurely? But exhaustion depends only on counts, not which specific elements we delete. The only way to exhaust a parity earlier is if we use more of that parity than required by the pattern, but the pattern forces which parity to delete at each step. So we cannot delete an even when an odd is required. Therefore, the counts of deletions per parity are exactly the number of times that parity appears in the sequence up to the point where we stop. And the stopping condition is when the required parity count is zero. So the number of moves is indeed the maximum possible length of an alternating sequence given the starting parity, i.e., the largest L such that we never attempt to delete a parity that is already exhausted. This L is uniquely determined by e and o and starting parity. Let's derive formulas:
Let e = count of evens, o = count of odds.
Case 1: Start with even.
- We can make moves as long as:
- For even moves (move numbers 1,3,5,...): after t-1 even moves have been performed (i.e., before the t-th even move), we need at least one even: t ≤ e.
- For odd moves (2,4,6,...): before the t-th odd move, we need at least one odd: t ≤ o.
Since moves interleave, the sequence goes: E1, O1, E2, O2, E3, O3, ... until one of these conditions fails. The maximum number of moves is determined by whichever parity runs out first. If e > o, then odd runs out first: we can do o odd moves, and after the o-th odd move, we have done o odd moves and o even moves? Actually after o odd moves, we have done o odd moves and also we would have done either o or o+1 even moves depending on order. Let's derive:
Sequence: move1: E, move2: O, move3: E, move4: O, ..., the pattern pairs (E,O). If we have o odd moves, the last odd move is at move number 2o. Before that, even moves performed = o (moves 1,3,...,2o-1). After move 2o, we have used o evens and o odds. Then we attempt move 2o+1 (even). This is possible if e > o (i.e., e >= o+1). If e >= o+1, we can do move 2o+1 (even). After that, we have used o+1 evens and o odds. Then move 2o+2 would be odd, but o odds are exhausted, so stop. So total moves L = 2o+1 if e > o.
If e == o, then after 2o moves (o evens, o odds), we have used all evens and odds. Then move 2o+1 would be even, but e = o so no evens left, so cannot. So L = 2o = e+o = n. So all elements deleted.
If e < o, then evens are fewer. Let's analyze: start even, e < o. Evens will run out first. The last even move we can do is the e-th even move, which occurs at move number 2e-1? Actually even moves are at odd indices: 1,3,5,... The e-th even move is at move number 2e-1. After that, evens used = e, evens left = 0. Before that move, odds used = e-1 (since up to move 2e-2, there are e-1 odd moves). After move 2e-1, we have used e evens and e-1 odds. Next move (2e) requires odd. Since o >= e? Actually o > e, so odds remaining = o - (e-1) = o - e +1 >= 2? Actually since o > e, o - e >=1, so odds remaining >=2? Wait o > e implies o - e >= 1, so o - (e-1) >= 2? Let's compute: o - (e-1) = o - e + 1. Since o > e, o - e >=1, so o - e +1 >= 2. So there is at least one odd. So move 2e is possible (odd). After move 2e, odds used become e, odds left = o - e. Then move 2e+1 requires even, but evens are 0, so stop. So total moves L = 2e if e < o. That uses all evens and e odds. So deletions = 2e, remaining odds = o - e.
But wait, we need to check if we could have done move 2e+1 if evens were available? Not here. So L = 2e.
But also what if e == o-1? Then o = e+1. Then L = 2e? Let's check: e=2, o=3. Start even: moves: E1 (e1 left1, o3), O1 (e1, o2), E2 (e0, o2), O2? After E2, evens=0, next move requires odd. odds left=2, so move O2 (e0, o1). After O2, next requires even, but evens=0, stop. So moves: E1, O1, E2, O2 = 4 moves = 2e =4. Deleted evens all 2, odds 2, left odds=1. So L=2e. Good.
Thus for start even:
- If e > o: L = 2o+1 (deleting all odds and o+1 evens)
- If e == o: L = 2o = n (delete all)
- If e < o: L = 2e (delete all evens and e odds)
Similarly, start odd (by symmetry):
- If o > e: L = 2e+1 (delete all evens and e+1 odds)
- If o == e: L = 2e = n
- If o < e: L = 2o (delete all odds and o evens)
We can also compute directly: starting parity p:
Let count_start = count of starting parity = c_s, count_other = c_o.
If c_s > c_o: then L = 2c_o + 1 (since we can do all other parity moves plus one extra of starting parity)
If c_s == c_o: L = 2c_s = n
If c_s < c_o: L = 2*c_s
Check: for start even, c_s = e, c_o = o.
- If e > o: L = 2*o + 1
- If e == o: L = 2*e = n
- If e < o: L = 2*e
Matches.
For start odd, c_s = o, c_o = e:
- If o > e: L = 2*e + 1
- If o == e: L = 2*e = n
- If o < e: L = 2*o
Now, the numbers of each parity deleted are:
When start parity = P, with counts c_s and c_o.
Case A: c_s > c_o: deletions: from starting parity: c_o + 1; from other parity: c_o. So we delete all of the other parity and all but (c_s - (c_o+1)) of starting parity remain? Actually c_s - (c_o+1) starting parity elements remain.
Case B: c_s == c_o: delete all: all elements.
Case C: c_s < c_o: deletions: from starting parity: c_s; from other parity: c_s. So we delete all of starting parity and c_s of the other parity, leaving c_o - c_s of the other parity.
Thus we can compute for each starting parity (if possible, i.e., c_s >= 1 for starting move), the set of elements we will delete: we will delete either:
- all elements of the other parity plus some number (maybe all or some) of the starting parity. Specifically, we delete min(c_s, c_o+1) or something? Let's derive the counts:
If c_s > c_o: del_start = c_o + 1, del_other = c_o.
If c_s == c_o: del_start = c_s, del_other = c_o (all).
If c_s < c_o: del_start = c_s, del_other = c_s.
Thus we will delete certain numbers from each parity. Since we can choose which elements to delete, to maximize sum deleted (and minimize sum remaining), we should delete the largest elements of each parity up to those counts. So we need to compute: total sum - (sum of largest del_start evens + sum of largest del_other odds) if starting with even, or similar for starting odd.
But careful: When starting parity is even, del_start refers to evens, del_other to odds. When starting odd, del_start refers to odds, del_other to evens.
So we need to sort evens and odds separately descending. Compute prefix sums to quickly get sum of top k elements.
Then for each possible starting parity (provided there is at least one element of that parity), compute the sum of remaining = total_sum - (sum of top del_start of that parity + sum of top del_other of the other parity). Then answer is the minimum of these two values (or just one if only one parity exists? Actually if e=0, we cannot start with even; similarly if o=0, cannot start with odd. But we might still have possible game? If e=0 and o>0, then the only possible start is odd (since first move can be any element, but if no evens, we must start with odd). So start odd is forced. Similarly if o=0, start even forced. If both e=0 and o=0? n>=1, so at least one.
Now, need to verify if the forced move count analysis always holds regardless of the values? There is one nuance: The process is forced, but we must also consider that we can choose which specific element to delete at each step. Does that affect the possibility of continuing? As argued, it only affects which elements remain, but not the counts. However, could there be a scenario where we deliberately delete a large element early, causing later we have smaller elements left, but that doesn't affect the count. So the number of deletions of each parity is fixed. So the maximum sum of deleted elements is achieved by deleting the largest elements of each parity, subject to the fixed counts. So the minimal sum of remaining is total sum minus that maximum.
But is it always possible to achieve the deletion of exactly the largest elements of each parity? Since we can choose which element to delete at each step, we can always choose to delete the largest available element of the required parity at each move. That would result in deleting the largest elements of each parity in some order. However, we need to delete exactly del_start elements from starting parity and del_other from other parity. If we always delete the largest available of the required parity, will we end up deleting the top del_start from starting parity and top del_other from other parity? Possibly yes, because the deletions from each parity are independent; we can always pick the largest remaining of that parity when we need to delete that parity. However, there might be an ordering constraint: we might be forced to delete a particular parity at certain steps, but we can choose any element of that parity. So we can always ensure that the deleted elements from that parity are exactly the largest ones, because we can delete them in descending order. Since deletions from a parity occur at distinct times, we can simply delete the largest remaining each time. This will result in the set of deleted elements from that parity being the top k (where k is the number of times we delete from that parity). So yes, we can achieve that.
Thus the optimal strategy for minimizing remaining sum is to choose the starting parity that yields the smallest remaining sum, and within that, delete the largest elements from each parity.
Edge cases: When |e - o| <= 1, we can delete all, so answer 0.
Otherwise, we compute both possibilities and take min. But also need to consider that if e=0 and o>0, starting with even is impossible; similarly if o=0, starting odd impossible. So only compute possible starts.
Now, let's test with examples:
Example1: 5 elements: [1,5,7,8,2]. Evens: 8,2 -> e=2; odds:1,5,7 -> o=3. Here |e-o|=1? Actually 3-2=1, so we can delete all? Wait e=2, o=3, |e-o|=1, indeed we can delete all: starting with odd (since o > e) yields sequence: O, E, O, E, O = all 5? Check: start odd, delete O, then E, O, E, O. That uses 3 odds and 2 evens, which matches counts. So answer 0. Output 0. Good.
Example2: 6 elements: [5,1,2,4,6,3]. Count odds: 5,1,3? Actually list: 5 (odd),1 (odd),2 (even),4 (even),6 (even),3 (odd) => odds: 5,1,3 => o=3; evens: 2,4,6 => e=3. |e-o|=0, can delete all, answer 0.
Example3: 2 elements: [1000000, 1000000] both even, so e=2, o=0. |e-o|=2 >1. Only possible start is even (since o=0 cannot start odd). Start even: e=2>o=0? Actually o=0, so c_s = e=2 > c_o=0. According to formula for start even when c_s > c_o: L = 2*c_o + 1 = 1? Wait c_o=0, so L=1? But can we really make only 1 move? Let's simulate: start even: delete one even (any). After that, evens left = 1, odds = 0. Next move requires odd (since last deleted even), but no odds, so stop. So only 1 deletion. So del_even = 1, del_odd = 0. So we delete only one element. Then remaining sum = one 1000000. Answer 1000000 matches. Our formula: for start even, c_s > c_o, del_start = c_o + 1 = 0+1=1, del_other = c_o =0. So remaining evens = 2-1=1. So sum = 1000000. Good.
Now what about start odd? Not possible because o=0. So answer is 1000000.
Thus solution seems straightforward: compute e, o, separate evens and odds, sort descending. Compute prefix sums for each. For each starting parity that is possible (if count of that parity >=1), compute:
- If start even:
if e > o: del_e = o + 1; del_o = o.
elif e == o: del_e = e; del_o = o (all).
elif e < o: del_e = e; del_o = e.
- If start odd:
similarly, using o and e:
if o > e: del_o = e + 1; del_e = e.
elif o == e: del_o = o; del_e = e (all).
elif o < e: del_o = o; del_e = o.
But note that the case e < o for start even yields del_e = e, del_o = e, leaving o - e odds. That's consistent.
But also need to consider edge case where e > o but start even, we use del_e = o+1, del_o = o. However, is it always possible to delete exactly o+1 evens? That requires that e >= o+1, which holds if e > o. Similarly start odd when o > e requires o >= e+1.
Now compute remaining sum = total_sum - (sum_largest_even(del_e) + sum_largest_odd(del_o)). Then answer = min over possible starts.
But is it always optimal to delete the largest? Since we minimize remaining sum, yes, we want to keep the smallest elements. So we should delete the largest ones. So the remaining sum will be sum of all elements minus sum of deleted largest ones.
Now, the above reasoning seems correct. But let's test with some random cases to be sure.
Consider n=4, evens: [10,2] (e=2), odds: [1] (o=1). So array: 10,2,1. Wait n=3? Actually e=2, o=1, n=3. But n=4? Let's create a case: e=3, o=1. Example: evens: [100, 10, 1]? Actually need parity: evens: 100, 10, 2? Let's do evens: 100, 10, 2 (e=3); odds: 1 (o=1). Total sum = 113. |e-o|=2 >1. Two possible starts:
- Start even (e>=1): e=3 > o=1 => del_e = o+1 = 2, del_o = o =1. So delete top 2 evens and the odd. Largest evens: 100,10; odd:1. Deleted sum = 111, remaining sum = 2 (the smallest even 2). So remaining sum = 2.
- Start odd (o>=1): o=1 < e=3? Actually o < e, so start odd case c_s = o =1, c_o = e=3. Since c_s < c_o, del_o = o =1, del_e = o =1. So delete top 1 odd (only odd =1) and top 1 even (largest even =100). Deleted sum = 101, remaining sum = 12 (evens: 10,2). So remaining sum = 12. Minimum is 2. So starting even yields better result.
Now simulate: Starting even: first move delete even (choose 100). Then need odd: delete odd (1). Then need even: delete even (10). Then need odd: but no odds left, stop. So deleted 100,1,10. Left: 2. Sum=2. Yes. Starting odd: first delete odd (1). Then need even: delete even (maybe we could choose 2 instead of 100? But we want to minimize remaining sum, we can choose to delete the largest even at that step, 100). Then need odd: no odds left, stop. So deleted 1 and 100, left 10 and 2 sum=12. So indeed we get remaining sum 12 if we start odd. So the minimum is 2, as computed.
Now consider e=1, o=3: evens: [2], odds: [9,5,1]. Total sum=17. Possibilities:
- Start even (possible, e=1): since e < o (1<3), case c_s < c_o => del_e = e =1, del_o = e =1. So delete top even (2) and top odd (9). Deleted sum = 11, remaining sum = 6 (odds:5,1). So remaining = 6.
- Start odd (possible): o=3 > e=1 => case c_s > c_o? Actually start odd: c_s=o=3, c_o=e=1. Since o > e, del_o = e+1 = 2, del_e = e =1. Delete top 2 odds (9 and 5) and top even (2). Deleted sum = 16, remaining sum = 1 (odd 1). So remaining = 1. That's lower.
Thus answer should be 1.
Simulate: Start odd: delete odd (9). Then need even: delete even (2). Then need odd: delete odd (5). Then need even: no evens left, stop. So deleted 9,2,5, left 1. Sum=1. Starting even: delete even (2). Then need odd: delete odd (9). Then need even: no evens left, stop.