We need to produce a permutation of given 1s and 2s that maximizes number of prefix sums that are prime. We have n up to 200k, values only 1 or 2.
We need to reorder the sequence. Since each tile is either 1 or 2, the total sum S = (# of 1s) + 2*(# of 2s). Let ones = count of 1's, twos = count of 2's. Then S = ones + 2*twos.
Prefix sums: p_k = sum_{i=1..k} b_i. We want many p_k prime.
We can choose any ordering. What ordering maximizes number of prime prefix sums? This is a combinatorial optimization problem.
Observation: Since we can reorder arbitrarily, we basically decide how many 1's and 2's we place at each step. The prefix sum increments by 1 or 2 each step. We need to decide a sequence of increments (each 1 or 2) using given counts to maximize the number of steps where total sum is prime.
We can treat it as: we have initial sum 0 (not counted). At each step, we add either 1 or 2, and we want the new sum to be prime as often as possible.
This is like a DP? But n up to 200k, primes up to S max ~ n*2 = 400k, so we can precompute primes up to 400k using sieve.
We need to find ordering maximizing count of prime prefix sums. Since only increments of 1 or 2, the prefix sums are a path from 0 to S. At each step we can choose either +1 or +2 if we have remaining ones/twos. The number of primes in the path (excluding 0) is to be maximized.
This resembles a DP over steps and remaining counts? But that would be O(n^2) which is too much.
But maybe we can find a greedy or constructive solution.
Let's think: Since we have only 1s and 2s, the sequence of prefix sums is monotonic increasing, step sizes 1 or 2. So the primes we can hit depend on the parity and pattern.
Observation: The number of primes among prefix sums is at most the number of primes in [1, S] that are reachable given the constraints? But we can potentially skip some primes? Since we can choose increments, we can try to hit as many primes as possible. But there may be constraints because of the counts of ones and twos.
Consider we want to have prefix sum equal to a prime p. To achieve that, we need to have some combination of ones and twos summing to p. But the order matters: we might need to hit intermediate primes too.
But maybe the maximum achievable is simply the maximum number of primes we can hit such that the gaps between consecutive primes can be bridged by 1 and 2 steps using available counts.
Given a sequence of primes p1 < p2 < ... < pk within [1,S], we want to find if there exists a path from 0 to S that hits exactly these primes (and possibly other sums) using given numbers of 1 and 2 steps. Actually we don't need to hit exactly these primes; we might also hit some other primes (or extra primes) but we want to maximize count. But the path must use exactly ones ones and twos twos.
So we need to decide a path (sequence of increments) that yields a set of prime prefix sums, and maximize size of that set.
We can treat it as DP over the current sum? But sum up to 400k, and we have counts constraints. Perhaps we can do DP where state is current sum and number of ones used? But number of ones used determines twos used (since steps taken = ones_used+twos_used). But we can also track steps taken. Actually we can define DP[pos][?] but that's too big.
But maybe the problem reduces to: the best ordering is to put all 2's first or all 1's first? Let's test with examples.
Example 1: n=5, values: 1,2,1,2,1 => ones=3, twos=2. S = 3+4=7. The output: 1 1 1 2 2 gave prefix sums: 1,2,3,5,7 -> primes at steps 1-4? Actually 1 is not prime, but they counted? They said four primes: 2,3,5,7. So they counted primes at positions 2,3,4,5. So they achieved hitting primes at sums 2,3,5,7. That's all primes up to 7 except maybe 1 is not prime, 4 is not, 6 is not. So they hit all primes in [1,7]. That seems optimal.
What about alternative ordering: 2 2 1 1 1 gives sums:2,4,5,6,7 -> primes:2,5,7 (only three). 2,1,2,1,1: sums:2,3,5,6,7 -> primes:2,3,5,7 (four). But that uses a 2 at start. Actually ordering 2,1,2,1,1 also yields four primes? Let's compute: start 2 (prime), then +1 ->3 (prime), +2->5 (prime), +1->6 (not), +1->7 (prime). So also four primes. So multiple orderings yield four. The example gave 1,1,1,2,2 which yields primes at sums:1 (not),2 (prime),3 (prime),5 (prime),7 (prime) -> four primes. So indeed maximum is 4.
Example 2: ones=7? Actually input: 9 numbers: 1 1 2 1 1 1 2 1 1 -> ones count: Let's count: values: there are two 2's? Actually list: 1,1,2,1,1,1,2,1,1 -> that's 1 appears 7 times? Let's count: positions: 1:1,2:1,3:2,4:1,5:1,6:1,7:2,8:1,9:1 -> yes ones=7, twos=2, total S=7+4=11. Output: 1 1 1 2 1 1 1 2 1 gives prefix sums: 1,2,3,5,6,7,8,10,11 -> primes: 2,3,5,7,11 -> five primes. Could we hit all primes from 1 to 11? Primes: 2,3,5,7,11. That's 5 primes. 11 is total sum. So they hit all. Could we also hit prime 13? no. So maximum is 5. Alternative: all 1s first then 2s? 1 1 1 1 1 1 1 2 2 gives sums:1,2,3,4,5,6,7,9,11 -> primes:2,3,5,7,11 -> also 5. So works.
But maybe there are cases where we cannot hit all primes. Let's try a simple case: n=1, tile = 1 => sum=1, prefix sum is 1 not prime, so max=0. tile=2 => sum=2 prime, max=1. That is trivial.
Case: n=2, both 1's: ones=2, twos=0, S=2. Order must be 1,1 sums:1,2 -> primes:2 => max=1. Could we get both sums prime? 1 not prime, so only one. So max=1.
Case: n=2, one 1 and one 2: ones=1, twos=1, S=3. Order possibilities: 1 then 2 sums:1,3 -> primes:3 (only 1 prime). 2 then 1 sums:2,3 -> primes:2,3 (two primes). So max=2. So we can hit both primes 2 and 3. That's all primes in [1,3] (2,3). So max=2.
Case: n=2, both 2: ones=0, twos=2, S=4. Orders: 2,2 sums:2,4 -> primes:2 => max=1. So max=1.
Case: n=3, ones=0, twos=3 => S=6. Only increments of 2. Path: 2,4,6. Primes:2 only? 2 is prime; 4 not; 6 not. So max=1.
Case: n=3, ones=1, twos=2 => S=5. Can we hit all primes? Primes up to 5: 2,3,5. Let's see if we can get prefix sums at 2,3,5. Order: 2,1,2? sums:2,3,5 -> yes all three primes. That's possible: 2 (prime), +1 ->3 (prime), +2 ->5 (prime). So max=3.
Case: n=3, ones=2, twos=1 => S=4. Primes up to 4: 2,3. Can we hit both? Let's try order: 1,1,2 sums:1,2,4 -> primes:2 only. 1,2,1 sums:1,3,4 -> primes:3 only. 2,1,1 sums:2,3,4 -> primes:2,3 both. So max=2.
Case: n=4, ones=0, twos=4 => S=8, path:2,4,6,8 primes:2 only => max=1.
Case: n=4, ones=1, twos=3 => S=7. Primes up to 7: 2,3,5,7. Can we hit all? Let's try to construct: We need sums sequence that hits as many primes. Since total sum is 7, final is prime. We have 3 twos and 1 one. Total steps=4. Need increments sequence of length4. Sum=7. Could we hit primes at each step? At step1: if we put 2 -> sum2 prime; step2: add 1 -> sum3 prime; step3: add 2 -> sum5 prime; step4: add 2? But sum after step3=5, adding 2 gives 7 prime. But we need exactly 3 twos total: step1:2, step3:2, step4:2 would be three twos? Actually we used twos at step1 and step3, but we have three twos, so need another two. If step4 is 2, then after step3=5, adding 2 gives 7. But step2 we used 1. That's sequence: 2,1,2,2 sums:2,3,5,7 all primes. That uses ones=1, twos=3? Actually twos=3: we used two at steps1,3,4 -> that's three twos. So yes, max=4. So we can hit all primes.
Case: n=4, ones=4, twos=0 => S=4. Primes:2,3. Can we hit both? With all ones, sums:1,2,3,4. So primes at 2 and 3 => both, max=2.
Seems in many cases we can achieve hitting all primes up to S? But is it always possible? Consider ones=0, twos large: S=2*twos, which is even >2. The only primes we can hit are those that are even, i.e., only 2 (since all other primes are odd). So the maximum number of primes is: if S>=2 then 1 else 0. But maybe we could hit other primes? With increments of 2, sum is always even after first step if start with 2? Actually if we start with 2, sum=2 (prime). Then subsequent sums increase by 2, so all sums are even >2, none are prime. So only at most one prime (the first step) if S>=2. So max = 1 if there is at least one 2 (since we can place it first) and S>=2? Wait, if we have only 2's, we can only get one prime: the first 2. But what if we have multiple 2's but also some 1's? That could increase possibilities.
Now, what about ones=1, twos large? For example, n=5, ones=1, twos=4 => S=1+8=9. Primes up to 9: 2,3,5,7. Can we hit all? Let's try to construct sequence of 5 numbers: one 1, four 2's. We want prefix sums to include these primes. We need to start with something. If start with 2: sum=2 (prime). Next maybe 1: sum=3 (prime). Next 2: sum=5 (prime). Next 2: sum=7 (prime). Next 2: sum=9 (not prime). So we hit primes at steps1-4: 2,3,5,7 -> that's all four primes. So max=4. Good.
What about ones=1, twos=3 gave max=4 as earlier. So with at least one 1, we may be able to hit many primes.
Now consider ones=2, twos=2, S=6. Primes up to 6: 2,3,5. Can we hit all three? Let's try: we have two 1's, two 2's. Sequence length 4. Desired primes: 2,3,5. Final sum is 6 not prime. We need to have prefix sums 2,3,5 at some steps. Try to design: step1:2 ->2 prime; step2:1 ->3 prime; step3:2 ->5 prime; step4:1 ->6 not prime. That uses ones: step2, step4; twos: step1, step3. That's two ones and two twos. So yes, we hit all three primes. So max=3.
What about ones=2, twos=1, S=4. Primes:2,3. We can hit both as shown earlier. So max=2.
What about ones=3, twos=1, S=5. Primes:2,3,5. Can we hit all? Sequence length 4. Let's try: step1:2 ->2 prime; step2:1 ->3 prime; step3:1 ->4 not; step4:1 ->5 prime. That's ones used: steps2,3,4; twos step1. So primes at steps1,2,4: 2,3,5. That's three primes. Could we get also prime at step3? 4 is not prime. So max=3, which is all primes. Another ordering: 1,2,1,1 sums:1,3,4,5 -> primes:3,5 (two). So max=3.
Seems that when we have at least one 1 and at least one 2, we can usually achieve hitting all primes up to S? But not always? Consider S large but with limited ones. Let's try a more extreme case: ones=1, twos=100000 => S=200001, which is odd. Primes up to S: many primes. Can we hit all primes? We have only one 1, many 2's. With a single 1, the increments are mostly 2's. The prefix sums will have parity that changes only once when we use the 1. If we start with 2's, sums are even; after adding the 1, sums become odd; thereafter adding 2's toggles parity? Actually if we add a 1, the parity flips. After that, adding 2 preserves parity. So the parity of sums: initially 0 even. If we use some 2's first, sums remain even. At the point we insert the 1, sum becomes odd. Then all subsequent additions of 2's keep it odd (since adding 2 doesn't change parity). So the sequence of sums has at most two parity segments: first some even numbers, then after the 1, all odd numbers. So we can achieve at most one transition from even to odd.
Now, primes greater than 2 are odd. So to hit many odd primes, we need to have the odd segment. The even segment can only contain the prime 2. So maximum primes possible: we can get at most one even prime (2) from the even segment, plus as many odd primes as we can hit in the odd segment. In the odd segment, we have increments of 2 (since after the 1 we only have 2's left). So the odd segment sums form an arithmetic progression with difference 2 starting from some odd number (the sum after inserting the 1). Let's denote we have t twos before the 1, then the 1, then the remaining twos: let x = number of twos placed before the 1, and y = number of twos after the 1, with x+y = twos (total). Then the prefix sums:
After first x twos: sums: 2,4,6,...,2x. Among these, the only prime that can appear is 2 (if x>=1) and maybe others like? But even numbers >2 are not prime. So only prime is possibly at step1 if we placed a 2 first (sum=2). But could we get prime 2 at later even step? Only if we have a 2 at some point as sum: 2 is only achievable if we have a prefix sum exactly 2. That requires that the first element is 2 (since starting from 0, to get sum 2, either two 1's or one 2. But we only have one 1? Actually in this scenario we have one 1. So to get sum 2 early, we could either start with 2 (immediate sum=2) or start with 1 then 1? But only one 1, so not possible. So the only way to have sum=2 as a prefix is to have a 2 as the first element (since if we start with 1, sum=1, then next 2 gives sum=3, bypassing 2). So to get the prime 2, we must place a 2 first. So we likely want to do that.
Now after placing the 1, the sum becomes S_odd = 2x + 1. Then we add y twos, each step sum increases by 2. So the odd sums are: S_odd, S_odd+2, S_odd+4, ..., S_odd+2y = 2x+1+2y = 2(x+y)+1 = 2twos+1 = S? Wait total S = ones + 2twos = 1 + 2*twos = odd. So final sum is odd.
So the odd segment consists of consecutive odd numbers spaced by 2. Among these, some may be prime. We can control the starting odd number by choosing x (the number of twos before the 1). Since we want to maximize the number of primes in this arithmetic progression of length y+1 terms (including the point after the 1, before any further twos). Actually after the 1, we have y+1 prefix sums: after the 1 (that sum), and then after each subsequent 2. So there are (y+1) odd sums.
We want to choose x (0 ≤ x ≤ twos) to maximize number of primes in the set {2x+1, 2x+3, ..., 2x+1+2y}.
Also we may get an extra prime from the even segment if we set x>=1 and we placed a 2 first to get sum=2. But note that if we set x=0, then we start with the 1, then all twos after; then even segment is empty; we never get sum=2. So we might miss the prime 2. But perhaps we can get more odd primes by adjusting x to align the progression to cover more primes. Could we potentially get more total primes by skipping the prime 2 and focusing on odd primes? Since the total count of primes among the odd progression might be maximized for some x.
Thus, the problem reduces to: given counts ones and twos, find ordering (i.e., arrangement of 1's and 2's) maximizing number of prefix sums that are prime. Since we have many twos and few ones, the best might be to place one 1 somewhere to create an odd progression and maybe a leading 2 to get prime 2.
But more generally, if we have both types, we can intersperse them arbitrarily. However, the general structure: the sequence of prefix sums is determined by the order of 1s and 2s. Since the increments are small, the path can be thought of as a walk from 0 to S with steps of 1 or 2. The prime counts depend on which sums are visited.
This is reminiscent of a DP over sum, but perhaps we can use a greedy: we can try to hit all primes in [1, S] except possibly some that are unreachable due to parity or step constraints.
Is it always possible to achieve that all primes ≤ S are visited when we have at least one 1 and at least one 2? Let's test a counterexample. Suppose ones=1, twos=1, S=3 we got all primes (2,3) yes.
ones=1, twos=2, S=5 we got all primes (2,3,5) yes.
ones=1, twos=3, S=7 we got all primes (2,3,5,7) yes.
ones=1, twos=4, S=9 we got all primes (2,3,5,7) and 9 is not prime; we hit all primes up to S (2,3,5,7). That's all primes ≤9. Yes.
ones=1, twos=5, S=11. Primes up to 11: 2,3,5,7,11. Can we hit all? Let's try to construct: we have one 1 and five 2's. Want to hit 2,3,5,7,11. The odd progression after the 1: after some x twos then 1 then y twos, with x+y=5. The odd sums are 2x+1, 2x+3, ..., 2x+1+2y. That's y+1 terms. For x=2, y=3: odd sums: 5,7,9,11 -> includes 5,7,11 (and 9 not prime). We also have even segment: if we start with 2's first, we get sums: 2,4,...? x=2 gives two twos first: sums:2,4. So we get prime 2. Now we have prime 2, then after 1 sum=5 (prime), then after next 2 sum=7 (prime), then after next 2 sum=9 (no), then after next 2 sum=11 (prime). Also we missed prime 3. Could we get prime 3? To get sum 3, we need either (2 then 1) or (1 then 2). But if we start with two twos, we never have sum 3. If we start with 1 first, then we lose sum 2. Let's try x=1, y=4: even segment: first one 2 gives sum=2 (prime). Then 1 gives sum=3 (prime). Then odd sums: 3? Actually after 1 sum=3, then y=4 twos: sums 5,7,9,11 -> primes 5,7,11. That gives primes:2,3,5,7,11. Perfect! So all primes. So works.
So for ones=1, it seems always possible to hit all primes up to S? Let's test ones=1, twos=6, S=13. Primes up to 13: 2,3,5,7,11,13. Can we hit all? Need to find x (0≤x≤6) such that we get 2 (if x≥1), 3 (maybe if we arrange 2 then 1 or 1 then 2), and odd progression covers 5,7,11,13? And also maybe 13 final? Let's try x=1: even:2 (prime). Then 1 -> sum=3 (prime). Then odd progression: after 1 sum=3, then add 2's: 5,7,9,11,13 (since y=5). That's odd sums: 3,5,7,9,11,13. Among those primes: 3,5,7,11,13. But we already counted 3 from after 1 step, but careful: the prefix after the 1 is sum=3, which we count as prime. Then subsequent steps: 5,7,11,13 are primes (9 not). So primes:2,3,5,7,11,13 = all 6 primes. So works. So for ones=1, we can achieve all primes ≤ S? Possibly always if we set x=1 (i.e., place one 2 first, then the 1, then all remaining 2's). This yields prefix sums: 2 (prime), 3 (prime), then odd numbers from 5 onward stepping by 2 up to S (odd). But will that always include all odd primes up to S? Not necessarily; the progression may skip some odd primes if they are not in the arithmetic progression. For example, if we start the odd progression at 3, then we get all odd numbers from 3 to S with step 2. That includes all odd numbers between 3 and S. So it includes all odd primes in that range. Because the progression is every odd number. Indeed, if we have sequence: start with 2, then 1, then all remaining 2's, then the odd sums after the 1 are: after 1, sum = 3; then each addition of 2 increments by 2, so we get 3,5,7,9,11,13,... up to S (which is odd). So we will visit every odd number from 3 to S inclusive. Therefore we will capture every odd prime in that range. And we also capture prime 2 at the first step. Thus we capture all primes ≤ S: 2 and all odd primes up to S. This works provided we have at least one 2 to place first (so that we get sum=2) and at least one 1 to place immediately after that (to get sum=3). But what if we have no 2? Then we cannot get sum=2, but we might still get all odd primes? If we have only ones (no twos), then the prefix sums are 1,2,3,...,S. That includes all numbers, so we get all primes ≤ S. So for ones only, we get all primes. If we have only twos (no ones), then prefix sums are even numbers: 2,4,6,...,S. The only prime possible is 2 (if S≥2). So we get at most 1 prime.
Thus, the only problematic case is when we have at least one 1 but zero 2? That's fine. If we have at least one 2 but zero 1, then only one prime possible (2). So what about mixed cases where we have both 1 and 2 but maybe not enough to get the pattern 2,1, then all 2's? That pattern requires at least one 2 and at least one 1. It yields visiting all odd numbers from 3 to S. But does it always work regardless of counts? Let's test: ones=1, twos=1 gives sequence: 2,1 yields sums:2,3 -> primes:2,3. That's all primes up to 3 (2,3). Good.
ones=1, twos=0? Not mixed.
ones=2, twos=1: using pattern 2,1, then remaining? But we have two ones total. If we follow pattern: place a 2 first, then a 1, then the remaining one? Actually we have twos=1, ones=2. After placing 2 and 1, we have one 1 left. So sequence: 2,1,1 gives sums:2,3,4 -> primes:2,3. But we missed prime 5? S=2+1+1=4, so primes up to 4 are 2,3 only. So we hit both. So that's fine.
What about ones=2, twos=2? S=6, primes:2,3,5. Pattern 2,1, then remaining? We have ones left=1, twos left=1? Actually after 2 and 1, remaining: ones=1, twos=1. If we then place the remaining 2 and then the remaining 1? That would be: 2,1,2,1 => sums:2,3,5,6 -> primes:2,3,5. That's all. Or we could do 2,1,1,2 => sums:2,3,4,6 -> primes:2,3 only. So we need to ensure after the initial 2,1, we place the remaining 2 before the remaining 1 to get sum=5 (prime) before 6. But we have control. So maybe we can always arrange to visit all primes. But is there any case where we cannot visit all primes? Consider ones=2, twos=0, we can visit all primes (all numbers). That's fine.
Consider ones=0, twos>0: only one prime.
Now consider ones=1, twos>0: we argued we can get all primes by 2,1, then all 2's. That seems to always work. But wait: after 2,1, we have placed the only 1. Then all remaining are 2's. So the sums after the 1 will be odd numbers: 3,5,7,..., S (since we add 2 each time). This progression includes all odd numbers from 3 to S. So yes, we capture every odd prime. And we also captured 2 at step1. So that yields all primes ≤ S.
What about ones>=2, twos>=1? Can we always capture all primes? Let's try to construct a general method.
We want to produce prefix sums that include all primes up to S. Since primes are dense but not contiguous, we need to ensure that for each prime p ≤ S, there is some prefix sum equal to p. We have ability to produce any sum between 0 and S, but not all sums may appear in a single sequence because the increments are 1 or 2; we can produce any integer? Actually with steps of 1 or 2, you can produce any integer as a prefix sum because you can always choose a sequence of 1s and 2s that sums to any value up to S, but you need to produce the entire path from 0 to S that includes all those primes as intermediate sums. Is it possible to have a path that visits a given set of numbers? This is similar to covering all primes by a walk that uses given numbers of steps of size 1 and 2.
Given we have ones count O and twos count T, we can think of constructing a sequence that goes through all numbers from 1 to S? But that would require all 1's, because to increase by 1 each time, you need all steps to be 1. With 2 steps, you skip numbers. So we cannot guarantee to visit every integer. However, perhaps we can visit all primes even with some skips. For each prime p, we need to ensure that p is achievable as a prefix sum given the order.
We want to maximize the count, so the upper bound is the total number of primes ≤ S. Could it always be achievable when O >= 1 and T >= 1? Or when O >= 2? Let's test borderline cases:
Case: O=1, T=1 we already saw all primes (2,3) yes.
Case: O=1, T=2: all primes (2,3,5) yes.
Case: O=2, T=1: all primes up to S=4 (2,3) yes.
Case: O=2, T=2: all primes up to 6 (2,3,5) yes.
Case: O=2, T=3: S=2+6=8? Actually O=2, T=3 => S=2+6=8. Primes ≤8: 2,3,5,7. Can we get all four? Let's try to construct. We have two 1's, three 2's. Want to hit 2,3,5,7. Sequence length 5. Try pattern: 2,1,2,2,1? Let's compute: 2 (2 prime), +1 =>3 (prime), +2 =>5 (prime), +2 =>7 (prime), +1 =>8 not prime. That uses ones: at steps2 and step5; twos: steps1,3,4. That's two ones and three twos. Works! So all primes.
What about O=2, T=4: S=2+8=10. Primes ≤10: 2,3,5,7. Could we also get 11? no. So primes:2,3,5,7. Can we hit all? Try pattern: 2,1,2,2,2,1? But length is 6 (O=2,T=4 => n=6). Sequence: 2,1,2,2,2,1? Let's compute sums:2 (prime),3 (prime),5 (prime),7 (prime),9 (no),10 (no). That gives primes 2,3,5,7. Works! So all.
What about O=3, T=2: S=3+4=7 primes:2,3,5,7. Already done.
What about O=3, T=3: S=3+6=9 primes:2,3,5,7. Can we hit all? n=6. Try pattern: 2,1,2,1,2,1? That's O=3,T=3? Actually 2,1,2,1,2,1 uses three twos and three ones. Sums:2,3,5,6,8,9 -> primes:2,3,5 only, missing 7. Try 2,1,2,2,1,1? That's twos: steps1,3,4 = three twos; ones: steps2,5,6 = three ones. Sums:2,3,5,7,8,9 -> primes:2,3,5,7. Yes. So works.
Seems plausible that if we have at least one 1 and at least one 2, we can achieve all primes ≤ S. But is there a counterexample where S is prime but we cannot cover all primes? Let's try a more complex scenario: suppose O=1, T=1 we did. O=1, T=0 trivial. O=0, T>0 trivial. So maybe the only case where we cannot achieve all primes is when we have no 1's (only 2's), because then we can only get 2 as prime. Also maybe when we have no 2's? That's all ones, we can get all primes because we generate all integers.
But wait, consider O=1, T large as we argued we can get all primes by 2,1, then all 2's. That yields all odd numbers from 3 to S, and prime 2. That includes all odd primes. So yes.
Now consider O=2, T=0: all ones, we get all numbers.
Consider O=0, T>0: only one prime.
So far it seems maximum is π(S) (number of primes ≤ S) except when there are no ones? Actually even when there are no ones, we might get 2 if S≥2, else none. But is it always possible to achieve π(S) when ones ≥ 1? Let's test some random larger example with O=1, T=1000, S=2001. The pattern 2,1, then 999 twos gives odd numbers from 3 to 2001 step 2. That's all odd numbers. So we capture all odd primes. Also 2 is captured. So total primes = 1 (for 2) + number of odd primes up to 2001. That's exactly π(2001) because 2 is the only even prime. So yes.
Now what about O=2, T=1000? S=2+2000=2002. Primes ≤2002. Can we capture all primes? We need to ensure we hit primes like 2,3,5,7,... including even prime 2 and odd primes. But with two ones, we have more flexibility. The pattern 2,1, then some arrangement to cover all odd primes up to 2002? However, note that S is even (2002). The final sum is even, not prime. The largest prime ≤2002 might be 1999 etc. We need to hit all odd primes up to 2002, but we cannot necessarily hit every odd number because we have limited ones. But perhaps we can arrange to hit all odd primes by using the ones strategically to adjust parity at the right times.
General idea: If we have at least one 1 and at least one 2, can we always construct a sequence that visits every prime ≤ S? Let's attempt to prove or find a counterexample.
We need to produce a sequence b_1...b_n of 1s and 2s (with given counts) such that for every prime p ≤ S, there exists k with sum_{i=1..k} b_i = p.
We have control over order. One approach: try to design a sequence that generates all integers from 1 to S? That would require all steps to be 1, which is only possible if we have all ones. But we have twos, so we skip some numbers. However, we don't need all integers, only primes. Could we cover all primes by a single path? It seems plausible if we have at least one 1, we can achieve a path that visits all numbers of a certain parity, and maybe we can adjust using extra 1's to also visit some numbers of the other parity that are primes? But primes >2 are odd, so we only need to visit odd primes. So we need to ensure that our path includes every odd prime ≤ S. If we can make the set of visited odd numbers be all odd numbers from some starting point to S, then we cover all odd primes. For that, we need the path to eventually step by 2 after a point, covering consecutive odd numbers.
Observation: If we have at least one 1, we can switch parity. After using an odd number of 1's? Actually each 1 toggles parity, while 2 preserves parity. So the parity of the prefix sum depends on the number of 1's used modulo 2. Suppose we have used u ones so far. Then parity = (sum of twos is even) so parity = u mod 2. So the parity of the prefix sum is the parity of the number of 1's used. So to have an odd prefix sum, we need to have used an odd number of 1's at that point.
Thus, if we have at least one 1, we can achieve odd sums at some positions. But to have all odd numbers, we need to be able to increment by 2 while maintaining odd parity, i.e., we need to have used an odd number of 1's and then add only 2's (which keep parity odd). So if at some point we have used an odd number of 1's and we have only 2's remaining, then subsequent sums will be odd and increase by 2, covering a set of consecutive odd numbers (starting from that odd sum, then +2, +2,... until we possibly add more 1's which would flip parity to even). So to cover all odd primes, we could aim to have a segment where we have used an odd number of 1's and then we have a long run of 2's that covers the rest of the numbers up to S (or until we maybe add another 1). But if we have more than one 1, we might have to place them carefully not to break the coverage.
Let's think: Suppose we have O ones and T twos. We want to design an ordering that yields prefix sums including all odd primes ≤ S.
A natural candidate: Place a 2 first (to get prime 2). Then place a 1 (to get 3, and parity becomes odd). Then place all remaining twos. Then place the remaining ones? But if we place remaining ones after the twos, then after we start adding 1's, parity flips to even, and we may miss some odd primes that lie after that point. However, if we place all remaining ones after all twos, then after the long run of twos we have odd sums covering all odd numbers from some start to some maximum (the sum before adding ones). Then when we add a 1, the sum becomes even (non-prime except possibly 2 but we already have 2). Then subsequent ones will toggle parity, possibly giving odd numbers again, but those odd numbers would be spaced by 1? Actually adding 1 flips parity; adding another 1 flips again. But if we have multiple ones at the end, they will produce alternating parity sums, maybe hitting some odd primes if they align. But could we cover all odd primes up to S by this? The highest odd numbers will be reached before adding the final ones, because after adding the last 2, sum is S - (sum of ones at end). If we put all ones at the end, then the odd segment from after the first 1 to before the first of the trailing ones will be a run of consecutive odd numbers. The ones at the end will produce some even and odd numbers, but the odd numbers produced after the trailing ones will be limited and might not cover the full range up to S. In particular, the final sum S might be even or odd. If we put all ones at the end, the final sum might be even if number of trailing ones is odd? Actually after the twos, we have some sum (which is odd if we had an odd number of ones used before? In the pattern: 2 (even), then 1 (odd), then all twos (odd+even = odd?), wait: after 2, sum=2 (even). After 1, sum=3 (odd). Then adding twos: each 2 preserves parity, so all subsequent sums remain odd. So after adding all twos, sum = 3 + 2*T_remaining = odd. Then if we add a 1, sum becomes even; then another 1, sum becomes odd again; etc. So trailing ones produce alternating parity. So the odd numbers we get after adding some ones might be the odd numbers that are S - (some even offset). But will they cover all odd numbers from the start of the odd run up to S? Possibly not if we have many ones at the end, we might skip some odd numbers.
For example, O=3, T=2. S=7. If we do 2,1, then all twos? But T=2, we used one two at start, so one two remains. Actually pattern: 2,1, then the remaining twos? That would be: 2,1,2, then remaining ones? We have O=3, we used one 1, so two ones left. Sequence: 2,1,2,1,1 gives sums:2,3,5,6,7. Odd primes: 3,5,7 are covered; we have 7 prime, but also we might miss 7? Actually 7 is included. But we used trailing ones. So it's fine. But if we had many ones at the end, could we miss some odd primes? Let's try O=2, T=5, S=2+10=12. Primes ≤12: 2,3,5,7,11. Can we get all? Let's try pattern: 2,1, then all 5 twos? But we have T=5, we used one two at start, so remaining twos=4? Actually T=5 total, if we place 2 first, then 1, then we have 4 twos left? Wait total twos=5. Pattern: first element 2 (uses one two), second element 1 (uses one one). Then we have 4 twos left and O-1 ones left. If we then place all 4 twos, then the remaining ones (O-1=1) at the end. Sequence: 2,1,2,2,2,2,1. Sums:2,3,5,7,9,11,12. Primes:2,3,5,7,11. That's all primes. Good.
What if O=2, T=5 but we try to put all ones at the end? Actually we already did that: we placed one 1 early and one at the end; that's fine.
What about O=3, T=5, S=3+10=13. Primes ≤13: 2,3,5,7,11,13. Can we do 2,1, then all remaining twos? After 2,1, we have twos left=4? Actually total twos=5, used one, so 4 left. Ones left=2. Sequence: 2,1, then 4 twos, then 2 ones. Let's compute sums: after 2,1:3. Then adding 4 twos: 5,7,9,11. Then adding two ones: 12,13. So primes encountered:2,3,5,7,11,13. That's all. Good.
What about O=4, T=5, S=4+10=14, primes ≤14: 2,3,5,7,11,13. Can we do 2,1, then all twos? After 2,1, twos left=4, ones left=3. Sequence: 2,1,2,2,2,2,1,1,1? Actually we need to place 4 twos and 3 ones after. Let's try: after the initial 2,1, we have sum=3. Then we could place all 4 twos: sums become 5,7,9,11. Then place 3 ones: 12,13,14. Primes:2,3,5,7,11,13. That's all primes. But note we missed prime? 2,3,5,7,11,13 all covered. 13 is included at the second last step? Actually after 4 twos, sum=11. Then first 1 ->12, second 1 ->13 (prime), third 1 ->14. So yes.
So far it's working.
Now consider a scenario where ones are many but we might be forced to have multiple parity flips that cause gaps in odd numbers, but we can arrange the order to keep a long stretch of consecutive odd numbers covering all odd primes. The key is to have a contiguous block of 2's after we have used an odd number of ones. That block will generate a sequence of odd numbers with step 2. The length of that block determines how many odd numbers we cover. To cover all odd primes up to S, we need that the block covers from some starting odd number up to the maximum odd number that is ≤ S, and that the starting odd number is ≤ the smallest odd prime not otherwise covered? Actually we need to ensure that every odd prime appears somewhere in the path. The block of 2's after an odd number of ones will produce a set of odd numbers that are an arithmetic progression. If that progression covers all odd numbers from some a to b (consecutive odds), then all odd primes in that interval are covered. However, the path may also produce other odd numbers outside that block via ones placed before or after. So if we can arrange that the union of odd sums from all segments covers all odd numbers from 3 up to S (if S odd) or up to S-1 (if S even). But do we need to cover all odd numbers? No, only the primes. But if there is a prime that falls into a gap not covered, we miss it. So we need to guarantee coverage of each odd prime.
It might be simpler: For any configuration with at least one 1 and at least one 2, we can achieve the upper bound of π(S). Let's attempt to construct a general algorithm that always yields this maximum. Then the answer is simply to output any permutation that achieves all primes. We need to also handle the case where there are no 1's or no 2's.
But is it always possible? Let's try to find a counterexample by brute force for small n to see if maximum is always π(S) except when only 2's. We can test small n manually or by reasoning.
Consider O=1, T=1 gave π(3)=2? π(3)=2 (primes 2,3) achieved yes.
O=1, T=2 gave π(5)=3 achieved.
O=1, T=3 gave π(7)=4 achieved.
O=1, T=4 gave π(9)=4 (primes 2,3,5,7) achieved.
O=1, T=5 gave π(11)=5 achieved.
So for O=1, it's fine.
Now consider O=2, T=1 gave π(4)=2 (2,3) achieved.
O=2, T=2 gave π(6)=3 (2,3,5) achieved.
O=2, T=3 gave π(8)=4 (2,3,5,7) achieved.
O=2, T=4 gave π(10)=4 (2,3,5,7) achieved.
O=2, T=5 gave π(12)=5? π(12)=5 (2,3,5,7,11) achieved above.
O=2, T=6: S=2+12=14, π(14)=6? primes ≤14: 2,3,5,7,11,13 -> 6. Can we achieve all? Let's try to construct. O=2, T=6. We need a sequence of length 8. Idea: 2,1, then 5 twos? Actually T=6, after initial 2, we have 5 twos left, and O left=1. But we have total O=2, used one, so one left. Sequence: 2,1, then 5 twos, then 1? That would be: 2 (prime), 1 ->3 (prime), then twos: 5,7,9,11,13 (primes:5,7,11,13), then final 1 ->14. So primes:2,3,5,7,11,13 -> all six. Works.
O=2, T=7: S=2+14=16, π(16)=6? primes ≤16: 2,3,5,7,11,13 -> 6. Can we? 2,1, then 6 twos, then 1? After 2,1 ->3, then twos: 5,7,9,11,13,15, then 1 ->16. Primes:2,3,5,7,11,13 -> all. Yes.
So O=2 seems fine.
Now O=3, T=1: S=3+2=5, π(5)=3 (2,3,5) achieved? Let's see: O=3,T=1. Sequence: maybe 2,1,1,1? But that uses 1 two and 3 ones. Sums:2,3,4,5 -> primes:2,3,5. Works.
O=3,T=2: S=3+4=7, π(7)=4 (2,3,5,7) we did.
O=3,T=3: S=3+6=9, π(9)=4 (2,3,5,7) we did.
O=3,T=4: S=3+8=11, π(11)=5 (2,3,5,7,11). Let's try: 2,1, then 3 twos? Actually T=4, after initial 2, have 3 twos left, O left=2. Sequence: 2,1, then 3 twos, then 2 ones? Sums:2,3, then twos:5,7,9, then ones:10,11. That gives primes:2,3,5,7,11. Yes.
O=3,T=5: S=13, π(13)=6 (2,3,5,7,11,13). Try: 2,1, then 4 twos? T=5, initial 2 uses one, so 4 left, O left=2. Sequence:2,1, then 4 twos:5,7,9,11, then two ones:12,13. Primes:2,3,5,7,11,13. Works.
O=3,T=6: S=15, π(15)=6? primes ≤15: 2,3,5,7,11,13 -> 6. Try: 2,1, then 5 twos? Actually T=6, after initial 2, have 5 twos, O left=2. Sequence:2,1, then 5 twos:5,7,9,11,13,15, then two ones:16,17? Wait total sum would be? Actually S=3+12=15. After initial 2,1 sum=3. Then adding 5 twos gives +10, sum=13. Then adding two ones gives +2, sum=15. So the sums after twos: 5,7,9,11,13. Then after first one:14, after second one:15. Primes:2,3,5,7,11,13. That's all six. Good.
O=4, T=1: S=4+2=6, π(6)=3 (2,3,5). Sequence: 2,1,1,1,1? That uses 1 two and 4 ones: sums:2,3,4,5,6 -> primes:2,3,5. Works.
O=4,T=2: S=4+4=8, π(8)=4 (2,3,5,7). Try: 2,1, then 1 two? Actually T=2, after 2,1 we have 1 two left, O left=3. Sequence:2,1,2,1,1,1? Sums:2,3,5,6,7,8 -> primes:2,3,5,7. Works.
O=4,T=3: S=4+6=10, π(10)=4 (2,3,5,7). Try:2,1, then 2 twos? T=3, after initial 2, have 2 twos, O left=3. Sequence:2,1,2,2,1,1,1? Wait length: O=4,T=3 total 7. Order:2,1,2,2, then three ones? Let's compute: 2->2; 1->3; 2->5; 2->7; then ones:8,9,10. Primes:2,3,5,7. Works.
O=4,T=4: S=4+8=12, π(12)=5 (2,3,5,7,11). Try:2,1, then 3 twos? T=4, after initial 2, have 3 twos, O left=3. Sequence:2,1,2,2,2,1,1,1? Actually we need total: O=4, used 1 one, so left 3 ones; T=4, used 1 two, left 3 twos. So length 1+1+3+3=8. Order:2,1, then 3 twos, then 3 ones. Sums:2,3, then after twos:5,7,9, then ones:10,11,12. Primes:2,3,5,7,11. Works.
O=4,T=5: S=4+10=14, π(14)=6 (2,3,5,7,11,13). Try:2,1, then 4 twos? T=5, after initial 2, have 4 twos, O left=3. Sequence:2,1, then 4 twos:5,7,9,11, then 3 ones:12,13,14. Primes:2,3,5,7,11,13. Works.
So far all good.
Now consider O=5,T=1: S=5+2=7, π(7)=4, we can probably do.
It seems very plausible that whenever there is at least one 1 and at least one 2, we can achieve all primes ≤ S. But let's think about a potential subtle case: S is even and the largest prime is S-1 (if S-1 is prime). In our construction with initial 2,1, then all remaining twos, then the remaining ones at the end, we might not cover S-1 if the trailing ones cause the last odd number before S to be less than S-1. For example, suppose we have many ones at the end. The odd numbers covered by the block of twos go up to some odd value, say M (the sum before we start adding trailing ones). Then after adding trailing ones, we may get additional odd numbers if we add an odd number of ones? Actually adding a 1 toggles parity. So if M is odd, then after adding one 1, we get even; after adding two ones, we get odd again, specifically M+2; after three ones, M+3? Wait careful: If we have sum M (odd), then add 1 -> M+1 (even). Add another 1 -> M+2 (odd). Add another 1 -> M+3 (even). So the odd numbers we get after trailing ones are M+2, M+4, M+6,... for each pair of ones? Actually if we add two ones consecutively, the sum goes from M (odd) to M+1 (even) to M+2 (odd). So the odd numbers after trailing ones are M+2k for k≥1, where we have added 2k ones. So we can achieve odd numbers that are M plus an even increment. So the odd numbers we can achieve after the block are spaced by 2, starting from M+2. Meanwhile, the block of twos gave us odd numbers from some start A to M with step 2. So overall, we have odd numbers covering a range from A to possibly up to S if we add enough trailing ones. But are there gaps? Between M (odd) and M+2 (odd) we have M+1 even, which is fine. So the odd numbers after the block are M+2, M+4, M+6, ... until we run out of ones. So if we have enough ones, we can extend the odd coverage further. However, note that we cannot get M+1 (even) as prime (except 2). So the odd coverage may be contiguous in the sense of every odd number from A up to some maximum? Let's see: The block of twos gives odds: A, A+2, A+4, ..., M (since we add twos one by one). That's a complete set of odds from A to M inclusive with step 2. Then after that, if we have at least 2 ones, we get M+2 (odd). But what about M+2? Is it necessarily the next odd after M? Yes, the next odd after M is M+2. So if we can get M+2, then the odds are covered continuously from A to M+2. However, to get M+2, we need at least 2 trailing ones. But if we have only one trailing one, we get M+1 (even) but not M+2. So we would miss the odd number M+2 if it is prime. So we need to ensure that for each odd prime that lies beyond M, we can reach it using trailing ones. But maybe we can reorder the trailing ones and some twos to fill gaps.
In our construction above, we placed all remaining twos after the initial 2,1, then all remaining ones at the end. That works when we have enough ones to cover the needed odd numbers after M. But let's test a scenario where we have only one 1 total (so O=1). In that case, we have no trailing ones; after the block of twos, we stop at S (odd). That gives all odds from 3 to S. So coverage is complete. So O=1 is fine.
Now consider O=2. In our construction, we placed one 1 early (right after the first 2) and the second 1 at the very end. That gives after the block of twos, we have sum M (odd), then adding the last 1 yields M+1 (even), and stop. So we do not get M+2. The largest odd we get is M, which is S-1 (since final sum is S = M+1, even). So we get odds from 3 up to S-1. That covers all odd numbers up to S-1. Since S is even, the largest odd ≤ S is S-1. So that's sufficient to cover all odd primes (all odd primes ≤ S-1). So with O=2 and S even, we cover all odds up to S-1, which is the maximum odd number. So fine.
If S is odd and O=2, then S is odd? Let's compute: O=2, T some, S = 2 + 2T = even? Actually O=2 gives S = 2 + 2T = even? 2 + 2T is even. So if O=2, S is even regardless of T. So S even. So we always cover up to S-1, which is odd, and that's the max odd <= S. So fine.
Now consider O=3. O=3 gives S = 3 + 2T = odd + even = odd? Actually 3 is odd, 2T even, so S is odd. With O=3, we have three ones. In our construction, we used one 1 early (after first 2), leaving two ones at the end. After the block of twos, we have sum M (odd). Then we add two ones: first one gives M+1 (even), second one gives M+2 (odd). So the final sum S = M+2, which is odd. So the odd numbers we get are: block odds up to M, then after two ones we get M+2. So we have odds from A to M and also M+2. But what about M+2? That's the next odd after M, so coverage is continuous? We have M (odd) then we skip M+2? Actually we have M, then after one one we have M+1 (even), then after second one we have M+2 (odd). So we have M and M+2, but we don't have M+2 directly after M without the even step. That's fine; we have the odd M+2. However, is there any odd number between M and M+2? No, only M+1 even. So the set of odds we cover includes all odds from A up to M, and also M+2. But what about M+4? That would require four trailing ones? Actually if we have only two trailing ones, we only get up to M+2. Since S = M+2, and S is odd, we stop at S. So we don't need M+4 because S is M+2. So coverage up to S is fine: we have all odds from A to S? But wait, the block of twos gave odds: after the initial 1, we start at 3? Actually after 2,1 we have sum=3. Then adding twos gives odds: 3,5,7,..., up to some value. Let's compute: After initial 2,1, sum=3. Then we add all remaining twos (say t twos). The sums after each two are: 3+2, 3+4, ..., 3+2t = 3+2t. So M = 3+2t. That's an odd number. Then we add two ones: after first one, sum = M+1 (even); after second one, sum = M+2 = 3+2t+2 = 2t+5? Actually S = 3+2t+2 = 2t+5 = 2(t+2)+1? Wait compute: ones total=3, we used one early, so left 2 ones. So S = 3 + 2t + 2 = 2t+5. That is odd. So the odd numbers we have are: 3,5,7,..., 2t+3? Actually 3+2t = 2t+3. That is M. Then we also have final S = 2t+5. So we have odds: 3,5,7,..., 2t+3, and also 2t+5. So the set of odds from 3 to 2t+5 is covered except maybe 2t+5? But we have it. However, is 2t+5 consecutive after 2t+3? Yes, 2t+5 = (2t+3)+2, so it's the next odd. So indeed we have all odds from 3 to S (since S is odd) because we have 3,5,7,...,2t+3 (which is S-2) and then S. That's every odd number from 3 to S, because the odds are 3,5,7,..., S-2, S. So indeed with O=3 and this construction, we get all odd numbers from 3 to S. Let's verify: if S = 2t+5, then odds: 3,5,7,...,2t+3,2t+5. Yes we have them all. Because the block of twos gives odds from 3 to 2t+3 step 2 (that's t+1 numbers? Actually from 3 to 2t+3 inclusive step 2 gives t+1 numbers: 3,5,...,2t+3). Then we also have the final odd S=2t+5, which is the next odd. So the set includes all odds. So with O=3, we can cover all odds from 3 to S. Thus all odd primes covered.
Similarly, O=4 gives S even? O=4 even, so S = 4+2T = even. In our construction, we used one 1 early, leaving three ones at the end. After block of twos, M odd. Then we add three ones: first -> M+1 even, second -> M+2 odd, third -> M+3 even. So we get odd M+2, but not M+4. Final sum S = M+3 (even). So the odds we have are: from A up to M (block), and also M+2. So the maximum odd we have is M+2 = S-1 (since S = M+3). So we cover all odds up to S-1. Since S even, the largest odd ≤ S is S-1, so we cover all odds up to S-1. So all odd primes covered.
In general, it seems that with at least one 1 and at least one 2, we can achieve a sequence that yields all odd numbers from 3 up to the largest odd ≤ S. Let's try to prove constructively.
We need to consider parity of number of ones used before the long block of twos. If we have O ones total. We can decide to use k ones before we start a long stretch of consecutive twos, and the remaining O-k ones after. The parity of k determines the parity during the twos block. To get odd numbers during the twos block, we need k odd. Because after using k ones, the parity of the sum is (k mod 2) (since all twos added so far are even). Actually we also may have some twos before the block, but adding twos doesn't change parity. So the parity at the start of the block is k mod 2. To have the block generate odd numbers, we need k odd. So we should place an odd number of ones before the block. Also, to have the block cover a contiguous range of odd numbers, we should have the block consist of all remaining twos (or at least enough). After the block, we may place the remaining ones.
Let's denote:
- O_total = number of ones.
- T_total = number of twos.
We need O_total ≥ 1, T_total ≥ 1.
Choose k = 1 if O_total ≥ 1. That gives odd parity. Then we will arrange the sequence as: start with a 2? Wait, we need to ensure we also capture the prime 2. To get sum=2, we need a 2 at the beginning (since 0+2=2). Could we get sum=2 via 1+1? That would require two ones at start, but then we might not have an odd number of ones before the block? Let's see: If we start with 1, then sum=1 (not prime). To get sum=2, we could have 1,1: sum=2, but that uses two ones, so k would be 2 (even). Then parity at block start would be even, so the block of twos would generate even numbers, not covering odd primes. So not good. So we likely want to start with a 2 to capture prime 2 and also to set up for the block. But is it necessary to capture prime 2? Possibly we could sacrifice prime 2 to get more odd primes? But the maximum possible primes is π(S), which includes 2. So to achieve π(S), we must also capture 2. So we need sum=2 at some point. The only ways to achieve sum=2 are: either a single 2 as first element, or two 1's as first two elements. Using two 1's uses two ones and results in even parity after using two ones (even number). That might still be okay if we can still cover all odd primes via some other mechanism? Let's explore: If we start with 1,1, sum=2 at step2, but then we have used two ones, parity even. Then we can add a 2, sum=4 (even). Not good. To get odd numbers, we need an odd number of ones before a block of twos. If we used two ones initially, we could later add another one to make total ones used odd before the twos block. For example, sequence: 1,1,1,...? That uses three ones, odd. After three ones, sum=3 (odd). Then adding twos would yield odd numbers. But we would have used three ones before the block, which is odd. That's okay. But then we need to ensure we still capture prime 2. We already captured it at step2. So maybe we can start with two 1's to get 2, then a 1 to get 3, then all twos, then remaining ones. Let's test: O=3, T=2, S=7. Sequence: 1,1,1,2,2? That sums:1,2,3,5,7 -> primes:2,3,5,7 (all). That's also works. So there are multiple patterns.
But we need a general constructive method that works for any O≥1, T≥1. Perhaps a simple pattern: Place one 2 at the beginning (to get prime 2). Then place one 1 (to get prime 3). Then place all remaining twos. Then place all remaining ones. This is what we used earlier. This seems to work for O>=1 and T>=1? Let's verify for O=1,T>=1: after 2,1 we have used the only 1, so no remaining ones, then we place all remaining twos. That yields odds from 3 to S (since S odd). Covers all odd primes and 2. Good.
For O=2,T>=1: after 2,1, we have one 1 left. Then we place all remaining twos, then the last 1. This yields: after twos block, we have odd numbers up to M (odd). Then adding the last 1 gives M+1 (even). Final S = M+1, even. The odd numbers covered are all odds from 3 to M. Since M = 3 + 2*(T-1) (if we used initial 2, then after initial 2,1 we have placed 1 two and 1 one, so remaining twos = T-1). Actually T total, we placed one 2 at start, so remaining twos = T-1. After the initial 2,1, sum=3. Then adding T-1 twos yields sums: 3+2, 3+4, ..., 3+2*(T-1) = 2T+1? Let's compute: 3 + 2*(T-1) = 2T+1. That is M. Then adding final 1 gives S = 2T+2 = 2(T+1) = O+2T? O=2, so S=2+2T, which matches. So odd numbers covered are 3,5,7,..., 2T+1. Since S = 2T+2, the largest odd ≤ S is 2T+1, which is M. So we have all odds up to S-1. That's all odd numbers from 3 to S-1. So we cover all odd primes ≤ S (since the largest odd prime could be ≤ S-1). So we have all primes. Good.
For O=3,T>=1: after 2,1, we have two ones left. Then we place all remaining twos, then the two ones. This yields after twos block: M = 3 + 2*(T-1) (since we used one two at start). Then add two ones: M+1 (even), M+2 (odd). Final S = M+2 = 2T+3? Actually compute: S = 2 (initial) + 1 + 2*(T-1) + 2 = 2 + 1 + 2T -2 + 2 = 2T+3? Let's do: initial two:2, then 1 ->3, then (T-1) twos: add 2(T-1) -> sum = 3+2(T-1)=2T+1, then add two ones: +2 -> 2T+3. So S = 2T+3, odd. The odd numbers covered: from twos block we have odds: 3,5,..., 2T+1. Then after two ones we get odd 2T+3 = S. So we have all odds from 3 to S inclusive (step 2). So all odd primes covered.
For O=4,T>=1: after 2,1, we have three ones left. Then all remaining twos, then three ones. After twos block: M = 2T+1 (as above). Then add three ones: M+1 (even), M+2 (odd), M+3 (even). Final S = M+3 = 2T+4, even. Odd numbers covered: from twos block: odds up to M = 2T+1; plus odd M+2 = 2T+3. So we have all odds from 3 to 2T+3, which is S-1. Since S even, max odd is S-1, covered. So all odd primes covered.
Thus the pattern works for any O>=1, T>=1. For O=1, we have no trailing ones, but that's fine. For O>=2, we have trailing ones (O-1) ones after the twos block. The parity of O-1 determines the final coverage. But as argued, we always get all odd numbers from 3 up to the largest odd ≤ S. Let's confirm for O=5,T>=1: after 2,1, we have four ones left. After twos block M=2T+1. Then add four ones: M+1 (even), M+2 (odd), M+3 (even), M+4 (even? Actually careful: start M odd.
Step1: +1 -> M+1 even.
Step2: +1 -> M+2 odd.
Step3: +1 -> M+3 even.
Step4: +1 -> M+4 even? Wait M odd, M+1 even, M+2 odd, M+3 even, M+4 odd? Let's compute parity: odd+1=even, even+1=odd, odd+1=even, even+1=odd. Actually parity toggles each addition of 1. So after 4 ones, parity = odd + 4 mod2 = odd (since 4 even) so M+4 is odd. So after adding 4 ones, we get M+4 odd. So we get odds at M+2 and M+4. But we also have M (odd) from block. So odds: ..., M, M+2, M+4. So we have all odds from 3 up to M+4? Since M is 2T+1, M+4 = 2T+5. Are there gaps? Between M and M+2 we have M+1 even, so no odd missing. Between M+2 and M+4, we have M+3 even, so no odd missing. So we have odds: 3,5,...,2T+1 (from block), and also 2T+3, 2T+5. That's all odds from 3 to 2T+5. Now what is S? O=5 => S = 5 + 2T. That equals 2T+5. So S = 2T+5, odd. So we have all odds from 3 to S. Good.
For O=6,T>=1: O even => S=6+2T = 2T+6, even. After 2,1, we have five ones left (since used one). After twos block M=2T+1. Add five ones: we get odd numbers at M+2, M+4 (since odd after adding 2,4, etc? Let's compute: after ones: positions where odd sums occur: after even number of ones added to M? Starting M odd, after adding 2k ones, sum = M+2k, parity = odd (since 2k even). So odd sums appear at M+2, M+4, M+6,... up to M+2floor((O-1)/2)? Actually if we add O-1 ones, the odd sums after the block are M+2, M+4, ..., up to M+2floor((O-1)/2) if O-1 is even? Let's derive: The set of odd sums after adding trailing ones is { M+2k | 1 ≤ k ≤ floor((O-1)/2) } because after adding 2k ones, we get odd. Also possibly if O-1 is odd, the final sum after all ones is M+(O-1). The parity of final sum: M + (O-1). Since M odd, parity = (O-1) mod 2. If O-1 is even, final sum odd; if O-1 odd, final sum even. In our case O=6 => O-1=5 odd, so final sum even. So we have odd sums: M+2, M+4 (since 2k max where 2k ≤ O-1 => k ≤ floor(5/2)=2 => k=1,2 => M+2, M+4). So odds covered: up to M+4 = 2T+5. The largest odd ≤ S? S = 2T+6, so largest odd = 2T+5. So covered. And we also have odds from block up to M=2T+1. So we have all odds from 3 to 2T+5 = S-1. So all odd primes covered.
Thus the construction works for all O≥1, T≥1.
Now what about edge cases: O=0 (no ones) or T=0 (no twos). If T=0 (all ones), we can simply output the sequence of all ones (any order, but it's all ones). That gives prefix sums 1,2,3,...,S. This includes all primes ≤ S. So max = π(S). Wait: does it include 2? Yes. So all primes. So maximum is π(S). However, is there any constraint? For T=0, the construction above (2,1,...) not applicable because we have no 2. But we can just output the sorted array (all ones). That works.
If O=0 (all twos), then we only have 2's. The prefix sums are 2,4,6,...,S. The only prime that can appear is 2 (if S≥2). So maximum is 1 if S≥2 else 0 (if n=0? n≥1, so if n=1 and tile=2, S=2 prime, count=1; if n=1 tile=1? but O=0 can't have 1). So for all twos, max = 1 if there is at least one tile? Actually if n=1 and tile=2, S=2 prime, count=1. If n≥2 and all twos, S≥4 even, only 2 is prime, but can we always get prefix sum 2? Yes, by placing a 2 first. So count = 1. However, is it possible to get 0? If we place a 2 first, we get 2 prime. So we can always achieve 1. So maximum = 1 for O=0 and T≥1. But could we ever get more than 1? If we have only twos, all sums are even and >2 after first step, none are prime. So max = 1.
Now, what about the case where O=0 and T=0? impossible.
Thus the maximum number of prime prefix sums is:
- If at least one 1 is present: π(S)
- Else (i.e., all tiles are 2): π(S) but π(S) for even S>2 is 1? Actually π(S) includes 2 and maybe other primes? For S even and >2, there are odd primes ≤ S, but we cannot get them because all sums are even. So maximum is not π(S) but 1. So we need to treat the all-twos case separately.
But wait, what if there is at least one 1 but no 2? Then we can get all primes π(S). So maximum = π(S) when ones > 0. And when ones = 0, maximum = 1 (if S ≥ 2) or 0 if S < 2? Since n≥1 and all twos, S = 2*n ≥2, so always ≥2, so max=1.
But is there any scenario where ones > 0 but we cannot achieve π(S)? Let's test some random small cases exhaustively for n up to maybe 10 to be sure. But from the construction, it seems always possible as long as there is at least one 2? Actually our construction requires at least one 2 to place at the beginning to get prime 2. But if ones > 0 but no 2s, then we cannot use that construction because we have no 2. But we can just output all ones; that yields all numbers from 1 to S, which includes all primes. So that's fine. So if T=0, output all ones, which gives π(S). So condition for achieving π(S) is ones > 0. Because if ones > 0, we can always arrange to get all primes? Let's verify case ones > 0 but T=0: we have all ones, we get all numbers, so yes π(S). Case ones > 0 and T>0: we have construction above.
Thus maximum = π(S) when ones > 0; else (ones = 0) maximum = 1 (since we can always get prime 2 by placing a 2 first; if n=0? not possible). But note: if n=1 and the only tile is 1, ones=1, S=1, π(1)=0 (no primes). So max=0, which is π(1)=0. So that fits the rule: if ones>0, max = π(S). Since if S=1, π(1)=0, and we can achieve 0 primes (only sum 1). So it's consistent.
If n=1 and tile=2, ones=0, S=2, π(2)=1, but our rule for ones=0 gives max=1, which equals π(2) as well. But for larger all-twos, π(S) may be >1, but we cannot achieve more than 1. So for ones=0, max = 1 (if S≥2). However, note π(S) for S=2 is 1, so it's same. For S=4, π(4)=2? Actually primes ≤4 are 2 and 3. π(4)=2. But with all twos, we can only get 2. So max=1, not 2. So the rule is: if ones=0, max = 1 (provided there is at least one tile). But what if n=0? not applicable.
Thus we need to output a permutation that achieves the maximum. So we need to construct such a permutation.
We have two main cases:
Case 1: count_ones == 0 (all twos). Then optimal is 1 prime (the first prefix sum if we start with a 2). Actually if we start with a 2, we get sum 2 (prime). Any order yields same count because all steps are 2: sums: 2,4,6,...,S. Only the first is prime. So any permutation works, but we can output e.g., all twos in any order (like original). So just output the input as is, or just a sequence of 2's.
Case 2: count_ones > 0. Then we want to achieve π(S). We need to output a permutation that yields all primes ≤ S. We can use a constructive method:
-
If count_twos == 0: output all ones (any order, simplest: print all ones). That yields sums 1,2,...,S and covers all primes.
-
Else (count_twos > 0 and count_ones > 0): Use the construction: first element = 2, second element = 1, then all remaining twos, then all remaining ones.
Let's verify this works for all counts as argued. We need to ensure that after we place the initial 2 and 1, we still have remaining twos? If count_twos == 1, then after placing the initial 2, there are no twos left. So the "all remaining twos" is empty. Then we place all remaining ones (which is count_ones - 1). That sequence is: [2,1] followed by (count_ones-1) ones. Let's test: count_ones=3, count_twos=1 => S=3+2=5. Sequence: 2,1,1,1 gives sums:2,3,4,5 -> primes:2,3,5? Actually 5 is prime. So primes:2,3,5 => π(5)=3 achieved. Good.
If count_ones=1, count_twos=1: sequence: 2,1 then no remaining twos, no remaining ones? Actually count_ones=1, after placing the 1, there are no ones left. So it's just [2,1]. That's sums:2,3 -> primes both, π(3)=2, good.
If count_ones=1, count_twos>1: sequence: 2,1, then (count_twos-1) twos, then no ones. Sums: after 2,1:3; then each two: 5,7,9,... up to S. This yields all odds from 3 to S and prime 2. So all primes. Good.
If count_ones>0, count_twos>0, the construction works.
But wait, what about the scenario where count_ones>0 but count_twos=0? Already handled.
Thus algorithm:
Input n, array a. Compute count_ones and count_twos.
Compute total sum S = count_ones + 2*count_twos.
If count_ones == 0:
output the array as is (or simply print "2 " repeated n) - but we need a permutation of input, which is all 2's, so just output the input.
Else:
If count_twos == 0:
output the array as is (all ones) - that's fine.
Else:
Construct sequence:
print a 2
print a 1
then print (count_twos-1) copies of 2
then print (count_ones-1) copies of 1
But careful: The input may have the values in any order, but we can output any permutation. So we can output this constructed sequence (list of numbers). However, we must ensure that the counts match exactly: we have exactly count_twos 2's and count_ones 1's. So the sequence should contain exactly count_twos 2's and count_ones 1's. Our constructed sequence does that: first we output one 2, then one 1, then (count_twos-1) 2's, then (count_ones-1) 1's. Total twos = 1 + (count_twos-1) = count_twos; total ones = 1 + (count_ones-1) = count_ones. So correct.
But we must also handle the case where count_twos-1 could be 0 (if count_twos=1). Then we output no extra 2's. Similarly, count_ones-1 could be 0 (if count_ones=1). So fine.
Thus the solution is straightforward O(n) to read and output.
But before finalizing, we need to double-check if there is any corner case where this construction fails to achieve all primes. Let's test some random values.
Take count_ones=5, count_twos=3. Then S=5+6=11, π(11)=5 (primes:2,3,5,7,11). Construct: 2,1, then twos: we have count_twos-1=2 twos, then ones: count_ones-1=4 ones. Sequence: 2,1,2,2,1,1,1,1. Compute sums: step1:2 (prime), step2:3 (prime), step3:5 (prime), step4:7 (prime), step5:8, step6:9, step7:10, step8:11 (prime). So primes:2,3,5,7,11. That's all.
Take count_ones=2, count_twos=2: S=2+4=6, π(6)=3 (2,3,5). Sequence:2,1, then twos-1=1 two, then ones-1=1 one: 2,1,2,1. Sums:2,3,5,6 -> primes:2,3,5. Works.
Take count_ones=2, count_twos=3: S=2+6=8, π(8)=4 (2,3,5,7). Sequence:2,1, then 2 twos (since 3-1=2), then 1 one: 2,1,2,2,1. Sums:2,3,5,7,8 -> primes:2,3,5,7. Works.
Take count_ones=3, count_twos=4: S=3+8=11, π=5. Sequence:2,1, then 3 twos, then 2 ones: 2,1,2,2,2,1,1. Sums:2,3,5,7,9,10,11 -> primes:2,3,5,7,11. Works.
Take count_ones=1, count_twos=5: S=1+10=11, π=5. Sequence:2,1, then 4 twos: 2,1,2,2,2,2. Sums:2,3,5,7,9,11 -> primes:2,3,5,7,11. Works.
Take count_ones=1, count_twos=1: 2,1 -> sums 2,3.
Take count_ones=1, count_twos=0: all ones -> 1, sums=1 (π=0). Sequence: just "1". That's fine.
Take count_ones=0, count_twos=3: all twos -> just output all twos, sums:2,4,6 -> prime only 2. So max=1. Could we do better with different order? No, all twos only produce even sums. So max=1. Our output is all twos, which is optimal.
Now, are there any cases where our construction might produce prefix sum 1 which is not prime but that doesn't matter? For count_ones>0 and count_twos>0, our construction never starts with 1, so first sum is 2 (prime). So we don't waste a prime opportunity at step1? Actually we want to maximize primes, and we achieve all primes, so it's optimal.
But is there any scenario where π(S) is not achievable because we might be forced to have a prime at sum=2 but we cannot due to ordering? In our construction we always start with 2, so that's fine. But if count_twos=0, we cannot start with 2; we start with 1. Then the first sum is 1 (non-prime). But we still achieve all primes because we will hit 2 at step2 (if count_ones>=2) or maybe not if count_ones=1? If count_ones=1 and count_twos=0, S=1, no primes. So that's fine. If count_ones=2 and count_twos=0, sequence all ones: sums 1,2 -> prime at step2. So we get prime 2. Also 3? S=2? Actually S=2, so only prime 2. So π(2)=1. So we get it. If count_ones=3, S=3: sums 1,2,3 -> primes 2,3. So we get both. So all ones works.
Thus the construction is optimal.
But wait, what about the scenario where count_ones > 0 but count_twos = 0? Our construction for that case is just output all ones, which we said is fine.
Now, is there any scenario where we might be able to get more primes than π(S)? No, because the maximum possible number of primes among prefix sums cannot exceed the number of primes ≤ S, since each prefix sum is a number between 1 and S inclusive. So π(S) is an upper bound. And we have shown constructively that we can achieve π(S) whenever there is at least one 1. The only exception is when there are no 1's, but then the only prime we can get is 2 (if S≥2). However, could there be a scenario with no 1's but we could get more than 1 prime? Possibly if S includes prime numbers that are even and >2? But no, the only even prime is 2. So indeed max=1. So the maximum is:
max = (count_ones > 0 ? π(S) : 1) (since if count_ones=0 then S>=2, so at least 1). But what if n=0? not allowed.
But consider count_ones=0 and n=1 (only one tile 2): S=2, π(2)=1, so max=1. Good.
So the answer is simply to output a permutation that achieves that maximum.
We just need to implement: read n, array, compute counts, then produce the described sequence.
But we must ensure that for the case count_ones>0 and count_twos>0, the construction indeed yields all primes. We've verified many cases. Let's also test a case where S is small and count_ones>0, count_twos>0 but maybe the construction yields a prefix sum that equals 1? No, because we start with 2. So first sum is 2.
But consider count_ones=1, count_twos=0 already handled.
Now, we must be careful about the order of remaining twos and ones. In our construction we placed all remaining twos immediately after the initial 2,1, then all remaining ones. This yields a specific pattern. But could there be a case where this fails to cover some odd prime? For example, when count_ones is even and S is even, we need to cover all odd numbers up to S-1. Let's verify with a specific number: Suppose count_ones=4, count_twos=2. Then S=4+4=8. Our construction: 2,1, then (2-1)=1 two, then (4-1)=3 ones. Sequence: 2,1,2,1,1,1. Sums:2,3,5,6,7,8. Primes:2,3,5,7. That's π(8)=4. All odd primes ≤8 are 3,5,7. Yes covered. Good.
Suppose count_ones=2, count_twos=1: S=2+2=4, π(4)=2 (2,3). Construction: 2,1, then no twos (since count_twos-1=0), then ones-1=1 one: 2,1,1. Sums:2,3,4 -> primes:2,3. Good.
Suppose count_ones=2, count_twos=0: S=2, π=1. All ones: 1,1 sums 1,2 -> prime 2. Good.
Suppose count_ones=3, count_twos=0: S=3, π=2. All ones: 1,1,1 sums 1,2,3 -> primes 2,3. Good.
Suppose count_ones=3, count_twos=1: S=5, π=3. Construction:2,1, then 0 twos, then 2 ones: 2,1,1,1 sums:2,3,4,5 -> primes 2,3,5. Good.
Suppose count_ones=5, count_twos=1: S=7, π=4 (2,3,5,7). Construction:2,1, then 0 twos, then 4 ones: 2,1,1,1,1,1 sums:2,3,4,5,6,7 -> primes:2,3,5,7. Good.
Suppose count_ones=6, count_twos=2: S=6+4=10, π(10)=4 (2,3,5,7). Construction:2,1, then 1 two, then 5 ones: 2,1,2,1,1,1,1,1? Wait count: twos total=2, ones=6. Sequence:2 (first),1 (second), then remaining twos=1, then remaining ones=5. So length=8. Sums:2,3,5,6,7,8,9,10? Let's compute precisely: after 2:2; after 1:3; after next 2:5; then ones: after first 1:6; second 1:7; third 1:8; fourth 1:9; fifth 1:10. So primes:2,3,5,7. That's all. Good.
Thus construction works.
Now, confirm that we are indeed outputting a permutation of the input sequence. Since the input contains exactly count_ones ones and count_twos twos, our output uses exactly those counts. So it's a valid permutation.
Now, about the prime counting: we don't actually need to compute π(S) for output, only to construct the sequence. So we just output the sequence as described.
But is there any situation where we might want to output a different permutation because our construction might fail for some specific counts? Let's stress test with a larger random: count_ones=7, count_twos=4 => S=7+8=15, π(15)=6 (2,3,5,7,11,13). Construction:2,1, then 3 twos, then 6 ones. Sums: after 2:2; after1:3; after first two:5; after second two:7; after third two:9; then ones:10,11,12,13,14,15. Primes:2,3,5,7,11,13. That's all. Good.
count_ones=8, count_twos=4 => S=8+8=16, π(16)=6 (2,3,5,7,11,13). Construction:2,1, then 3 twos, then 7 ones. Sums:2,3,5,7,9, then ones:10,11,12,13,14,15,16. Primes:2,3,5,7,11,13. All covered. Note 13 is prime and appears.
count_ones=9, count_twos=4 => S=9+8=17, π(17)=7 (2,3,5,7,11,13,17). Construction:2,1, then 3 twos, then 8 ones. Sums: after twos: 2,3,5,7,9; then ones:10,11,12,13,14,15,16,17. Primes:2,3,5,7,11,13,17. All covered. Note 17 is prime final.
Everything looks good.
Thus solution is simple.
However, we must consider the possibility that the construction might produce a prefix sum equal to 1 if we have no 2's? But that's only when count_twos=0, and we output all ones, which starts with 1. That's fine; we still achieve all primes because we will hit 2 later.
But is there any scenario where the construction fails to achieve all primes because we might inadvertently miss a prime that is 2? We always include 2 at start when twos>0. If twos=0, we still get 2 when we have at least 2 ones. So covered.
Now, what about the prime 2 when count_ones=1 and count_twos=0? That's S=1, no prime 2 because not reached. That's fine, π(1)=0.
Thus the solution seems complete.
But we need to be absolutely certain that for any combination with ones>0, the construction always yields all primes ≤ S. Let's attempt a formal proof:
Let O = number of ones, T = number of twos, with O ≥ 1, T ≥ 0. If T = 0, the sequence all ones yields prefix sums 1,2,...,O. This set is {1,...,O}. Since all primes ≤ O are included, we achieve π(O) = π(S).
If T ≥ 1, construct sequence: start with 2, then 1, then (T-1) twos, then (O-1) ones.
Let’s compute prefix sums:
- After first element: 2.
- After second: 3.
- After the next (T-1) twos: for i=1..(T-1), sum = 3 + 2i = 5,7,9,..., 3+2(T-1) = 2T+1.
- Then we add (O-1) ones. Let’s denote current sum before ones as M = 2T+1. After adding k ones (1 ≤ k ≤ O-1), sum = M + k.
Now, what are the primes we get?
- 2 is prime.
- 3 is prime.
- For each i from 1 to T-1, sum = 3+2i. These are all odd numbers from 5 to 2T+1 (step 2). So we hit all odd numbers in that range.
- Among the ones added later, we get sums M + k for k=1..O-1. These are consecutive integers from M+1 to M+O-1 = 2T+1 + (O-1) = 2T+O = S.
Now, consider the odd numbers in the range from M+1 to S. Since M is odd (2T+1), M+1 is even, M+2 odd, M+3 even, etc. So the odd numbers among these are M+2, M+4, M+6,... up to at most S if S odd, or S-1 if S even.
Specifically, the odd numbers in [M+1, S] are: if O-1 >= 2, we get M+2; if O-1 >= 4, we get M+4; etc. In general, we get odd numbers M+2j for j=1,2,..., floor((O-1)/2). Also, note that M+2 = 2T+3, M+4 = 2T+5, etc.
Now, the entire set of odd numbers covered by the sequence is:
- From the twos block: 3,5,7,..., 2T+1.
- From the ones block: odd numbers 2T+3, 2T+5, ..., up to either S (if S odd) or S-1 (if S even).
Thus, the covered odd numbers are exactly all odd numbers from 3 up to the largest odd ≤ S. Because the twos block covers all odds from 3 to 2T+1. Then if O-1 >= 2, we get 2T+3; if O-1 >= 4, we get 2T+5; and so on. Since S = 2T+O, the largest odd ≤ S is:
- If O is odd: S is odd, largest odd = S = 2T+O.
- If O is even: S is even, largest odd = S-1 = 2T+O-1.
We need to verify that our covered odds include all odds from 3 to that max. The twos block gives odds up to 2T+1. The next odd is 2T+3. We get 2T+3 if O-1 >= 2, i.e., O >= 3. If O=2, then O-1=1, so we don't get 2T+3. But for O=2, S = 2T+2, which is even. The largest odd is S-1 = 2T+1. And we already have that from the twos block. So no need for 2T+3. So covered odds are exactly 3,...,2T+1, which are all odds up to S-1.
If O=3, we get 2T+3 = S (since S=2T+3). So odds up to S.
If O=4, we get 2T+3 (since O-1=3 >=2, but we need O-1>=2 yes) but not 2T+5 because O-1=3 <4. The largest odd is S-1 = 2T+3. So we have odds: 3,...,2T+1, and also 2T+3. That's all odds up to 2T+3. So covered.
If O=5, we get 2T+3 and 2T+5 = S. So all odds up to S.
So indeed, for any O≥1, T≥1, the covered odds are exactly all odd numbers from 3 up to the largest odd ≤ S. Thus all odd primes in that range are covered. Additionally, 2 is covered at the first prefix. Therefore, all primes ≤ S are achieved. QED.
Hence the construction is correct.
Thus the problem reduces to a simple construction.
Now, implement.
We need to read n and array of n integers (1 or 2). We can just count ones and twos.
Edge Cases:
- n=1, a=[1] -> ones=1, twos=0 -> output "1". That yields prime count 0 (max).
- n=1, a=[2] -> ones=0, twos=1 -> output "2". That yields prime count 1 (max).
- n=2, a=[1,2] -> ones=1,twos=1 -> construction: 2,1 output "2 1". That yields primes 2 and 3. Good.
- n=2, a=[2,2] -> ones=0 -> output "2 2" (any order). Only first prefix is prime? Actually if we output "2 2", sums:2,4 -> prime at first. So count=1, which is optimal.
Now, confirm that the construction also works when count_twos>0 but count_ones=0? In that case we are in case count_ones==0, we don't use construction, just output twos. So fine.
Implementation Steps:
- Read n.
- Read list a.
- Count ones = a.count(1), twos = n - ones.
- If ones == 0:
- output the list as is (or simply print "2 " * n, but we need to print the permutation; we can just print the original list because it's all twos; any order works, but to be safe we can just print the list as given since it's a permutation of itself.)
Actually the problem asks to output a permutation of the input sequence. So we can output the input itself if it's already a permutation. For all twos, any order is same, so output the input. So we can just print the original array. But we must ensure format: space-separated. So we can print the array as is.
- Else if twos == 0:
- output the list as is (all ones). That works.
- Else:
- Construct list ans = [2, 1] + [2](twos-1) + [1](ones-1)
- Print ans.
Make sure to handle cases where twos-1 could be 0 or ones-1 could be 0; Python list multiplication with 0 yields empty list, fine.
Complexity O(n).
Now, test with provided examples:
Example 1:
n=5, a: 1 2 1 2 1 -> ones=3, twos=2.
Construction: ans = [2,1] + [2](1) + [1](2) = [2,1,2,1,1] -> That yields "2 1 2 1 1". But the sample output is "1 1 1 2 2". Both are valid permutations achieving max 4 primes. So okay. Our output differs but any optimal permutation is acceptable.
Check that our output "2 1 2 1 1" indeed gives prefix sums: 2,3,5,6,7? Let's compute: 2 (prime), +1=3 (prime), +2=5 (prime), +1=6, +1=7 (prime). So primes:2,3,5,7 -> 4 primes. Yes.
Example 2:
n=9, a: 1 1 2 1 1 1 2 1 1 -> ones=7, twos=2.
Construction: ans = [2,1] + [2](1) + [1](6) = [2,1,2,1,1,1,1,1,1] -> That yields "2 1 2 1 1 1 1 1 1". Let's compute prefix sums:2,3,5,6,7,8,9,10,11? Wait need to compute: after 2:2; +1:3; +2:5; +1:6; +1:7; +1:8; +1:9; +1:10; +1:11. Primes:2,3,5,7,11 -> that's 5 primes. Sample output is "1 1 1 2 1 1 1 2 1" which also yields 5 primes. So both fine.
Thus solution matches.
Now, verify some other cases:
Case: n=3, a: 2 2 2 -> ones=0 -> output "2 2 2". Sums:2,4,6 -> only first prime. Max=1. Could we do better by reordering? No.
Case: n=4, a: 2 2 2 2 -> output "2 2 2 2". Sums:2,4,6,8 -> prime only 2. Max=1.
Case: n=4, a: 1 1 1 1 -> ones=4, twos=0 -> output "1 1 1 1". Sums:1,2,3,4 -> primes:2,3 -> 2 primes, which is π(4)=2? Actually π(4)=2 (2,3). Yes.
Case: n=4, a: 1 1 1 2 -> ones=3, twos=1 -> construction: [2,1] + []? twos-1=0, ones-1=2 -> [2,1,1,1]. Sums:2,3,4,5 -> primes:2,3,5 -> 3 primes. π(5)=3. Good.
Case: n=4, a: 2 2 1 1 -> ones=2, twos=2 -> construction: [2,1] + [2] (since twos-1=1) + [1] (ones-1=1) -> [2,1,2,1]. Sums:2,3,5,6 -> primes:2,3,5 -> π(6)=3. Good.
All good.
Now, we should also consider if there is any scenario where our construction might produce a prefix sum that is 1? No, because we start with 2.
But what about the case ones>0, twos>0, but we might have count_ones=1, count_twos=1 -> output [2,1] -> no 1 at start.
Thus done.
Now, we need to implement.
Implementation details:
-
Input reading: n, then list of ints.
-
Count ones = sum(1 for x in a if x == 1)
-
Count twos = n - ones
-
If ones == 0:
ans = a (original list) (or any permutation, but original is fine)
-
elif twos == 0:
ans = a
-
else:
ans = [2] + [1] + [2](twos-1) + [1](ones-1)
But note: The input list a may not be sorted; but we are constructing a new list, which is a permutation because we use correct counts.
Now, printing: print the ans list with spaces.
Potential pitfalls: For large n (200k), constructing list with multiplication is fine.
Now, does this always produce the maximum? Yes.
But wait, we assumed that the maximum is π(S) when ones>0. However, is it always possible to achieve π(S) if ones>0 but the total sum S is 1? That would require ones=1,twos=0 -> π(1)=0, which we achieve. So ok.
What about ones>0 but S=2? That could be ones=2,twos=0 -> π(2)=1, achieved via all ones: sums 1,2 -> prime at 2. Could there be a scenario with ones>0, twos>0 and S=2? That would require ones=0? Actually S>=? For ones>=1 and twos>=1, minimal S = 1+2=3. So not relevant.
Thus fine.
Now, consider if there is a case where ones>0 but we might not be able to achieve π(S) because we need to have prefix sum 2, but we cannot place a 2 at the beginning if we have no twos? But that's the case twos=0, and we can still get 2 by having two ones. So we can achieve 2. For ones=1,twos=0, S=1, no 2 needed. For ones=2,twos=0, we can get 2 at second prefix. So it's fine.
Thus, maximum is π(S) for any bag that contains at least one 1. And for bags with no 1, maximum is 1 (since we can get 2 at first prefix if we have at least one 2; if n=0? not). So indeed our construction works for all cases.
But is there any nuance: maybe when ones>0 but we have no 2, our construction (all ones) yields all primes, which is fine.
One more nuance: The problem statement: "Output a permutation ... maximizing the number of the prefix sums being prime numbers." So we need to output a permutation. In case all twos, any permutation works. We can output the input as is. That's valid.
Now, I'll produce final solution in Python.
Complexity O(n). Memory O(n).
Now, I'd like to double-check the claim that max = π(S) when ones>0, by considering a potential counterexample: suppose S is 2 (ones=2, twos=0) we get π(2)=1, we achieve it. Suppose S is 3 (ones=3 or ones=1,twos=1) π(3)=2, we achieve. Suppose S=4: possibilities: ones=4 gives π(4)=2 (2,3) and we achieve; ones=2,twos=1 gives S=4? Actually 2+2=4? ones=2,twos=1 gives S=2+2? 2*1=2, plus 2 ones = 4? Wait ones=2,twos=1 -> S=2+2=4, yes. Can we achieve π(4)=2? Construction yields [2,1,1] -> sums:2,3,4 -> primes 2,3. Good. Ones=1,twos=1? S=3 not 4. So fine.
Suppose S=10, ones=1,twos=4? That's 1+8=9, not 10. Actually S=10 could be ones=2,twos=4: 2+8=10, or ones=4,twos=3:4+6=10, or ones=6,twos=2:6+4=10, or ones=8,twos=1:8+2=10, or ones=10,twos=0. All have ones>0, so we claim π(10)=4 (2,3,5,7). We have constructions for each. So seems consistent.
Now, is there any situation where the bag contains at least one 1 but we cannot achieve π(S) because the primes include 2 but we might not be able to hit 2? For twos>0, we start with 2; for twos=0, we need at least two ones to get sum 2. If ones=1 and twos=0, S=1, so no need for 2. If ones=1 and twos>0, we have a 2 to start. If ones=2 and twos=0, we can get 2 at step2. So always possible.
Thus final.
Now, implement.
Edge Cases:
Now, I think solution is done.
But I recall that in competitive programming sometimes there is a twist: maybe the maximum is not π(S) when ones>0? Let's try to find a case where S contains a prime p that we cannot hit because of the step constraints. For example, suppose we have ones=1, twos=2, S=5. We can hit 2,3,5. Good. Suppose we have ones=1, twos=3, S=7, we hit 2,3,5,7. Suppose we have ones=1, twos=4, S=9, we hit 2,3,5,7. That's π(9)=4? Actually π(9)=4 (2,3,5,7). Yes.
Suppose we have ones=2, twos=2, S=6, we hit 2,3,5. π(6)=3. Good.
Suppose we have ones=2, twos=3, S=8, we hit 2,3,5,7. π(8)=4. Good.
Suppose we have ones=2, twos=4, S=10, we hit 2,3,5,7. π(10)=4. Good.
Suppose we have ones=3, twos=1, S=5, we hit 2,3,5. π(5)=3. Good.
Suppose we have ones=3, twos=2, S=7, we hit 2,3,5,7. π(7)=4. Good.
Suppose we have ones=3, twos=3, S=9, we hit 2,3,5,7. π(9)=4. Good.
Suppose we have ones=4, twos=1, S=6, we hit 2,3,5? Actually construction [2,1,1,1,1] sums:2,3,4,5,6 -> primes 2,3,5. π(6)=3. Good.
Now, could there be a prime that is 2 but we cannot get because we have no 2 and only one 1? Then S=1, so 2 is not ≤S, so no need.
Thus seems robust.
Hence solution is straightforward.
Now, let's think about the possibility that the maximum might be greater than π(S) if we count prefix sum 0? No, they count only sums of prefixes, starting from first element. So 0 not considered.
Thus final answer.
Now, I'll produce final message with solution explanation. ```python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1+n]))
ones = a.count(1)
twos = n - ones
if ones == 0:
# all twos: any order works, output the input itself
ans = a
elif twos == 0:
# all ones: any order works, output the input itself
ans = a
else:
# construction: [2, 1] + remaining twos + remaining ones
ans = [2, 1] + [2] * (twos - 1) + [1] * (ones - 1)
print(' '.join(map(str, ans)))
if name == 'main':
main()