PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000117. Sắp xếp để có nhiều tổng tiền tố nguyên tố nhất (Prefix Sum Primes)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sắp xếp để có nhiều tổng tiền tố nguyên tố nhất (Prefix Sum Primes)

Prefix Sum Primes

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn tiles, each containing either 11 or 22. Rearrange all tiles into a sequence b1,b2,…,bnb_1,b_2,\ldots,b_n. For each position ii, consider the prefix sum si=b1+⋯+bis_i=b_1+\cdots+b_i.

Maximize the number of indices ii for which sis_i is prime. Output any optimal ordering, preserving the counts of 11 and 22 in the input.

Input

The first line contains integer nn. The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, each equal to 11 or 22.

Output

Print exactly nn integers forming a permutation of the input that maximizes the number of prime prefix sums.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; ai∈{1,2}a_i\in\{1,2\} for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; ai∈{1,2}a_i\in\{1,2\} for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; ai∈{1,2}a_i\in\{1,2\} for every 1≤i≤n1\le i\le n.

Examples

Input

5
1 2 1 2 1

Output

2 1 2 1 1

Explanation

The ordering [2,1,2,1,1][2,1,2,1,1] contains the same two copies of 22 and three copies of 11 as the input. Its prefix sums are 22, 33, 55, 66, 77. Four are prime: 2,3,5,72,3,5,7. As the total is 77, and these are all primes at most 77, no ordering can have more than four prime prefix sums.