We're giving away nice huge bags containing number tiles! A bag we want to present to…
- User
We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2.
However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get.
Can you win the prize? Hurry up, the bags are waiting! Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ {1, 2}) — the values written on the tiles.
Output
Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any.
Examples
Input
5 1 2 1 2 1
Output
1 1 1 2 2
Input
9 1 1 2 1 1 1 2 1 1
Output
1 1 1 2 1 1 1 2 1
Note
The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
Assistant
import sys def sieve(max_limit): if max_limit < 2: return [False] * (max_limit + 1) sieve_list = [True] * (max_limit + 1) sieve_list[0] = sieve_list[1] = False for i in range(2, int(max_limit ** 0.5) + 1): if sieve_list[i]: sieve_list[i*i : max_limit + 1 : i] = [False] * len(sieve_list[i*i : max_limit + 1 : i]) return sieve_list n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) c1 = a.count(1) c2 = a.count(2) if c2 == 0: print(' '.join(map(str, [1] * c1))) elif c1 == 0: print(' '.join(map(str, [2] * c2))) else: max_sum = c1 + 2 * c2 sieve_max = max(max_sum, 2 + c1 + 2 * (c2 - 1)) sieve_list = sieve(sieve_max) # Calculate total_A primes_A_1s = sum(sieve_list[i] for i in range(2, c1 + 1)) if c1 >= 2 else 0 primes_A_2s = 0 if c1 % 2 == 1: start = c1 + 2 end = c1 + 2 * c2 if start <= end: primes_A_2s = sum(sieve_list[i] for i in range(start, end + 1, 2)) total_A = primes_A_1s + primes_A_2s # Calculate total_B primes_B_1s_part = sum(sieve_list[i] for i in range(3, 2 + c1 + 1)) if (2 + c1) >= 3 else 0 primes_B_2s_part = 0 if (2 + c1) % 2 == 1: start = 2 + c1 + 2 end = 2 + c1 + 2 * (c2 - 1) if start <= end: primes_B_2s_part = sum(sieve_list[i] for i in range(start, end + 1, 2)) total_B = 1 + primes_B_1s_part + primes_B_2s_part if total_A >= total_B: output = [1] * c1 + [2] * c2 else: output = [2] + [1] * c1 + [2] * (c2 - 1) print(' '.join(map(str, output)))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.