#SP0000117. Sắp xếp để có nhiều tổng tiền tố nguyên tố nhất (Prefix Sum Primes)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 tiles, each containing either or . Rearrange all tiles into a sequence . For each position , consider the prefix sum .
Maximize the number of indices for which is prime. Output any optimal ordering, preserving the counts of and in the input.
Input
The first line contains integer . The second line contains integers , each equal to or .
Output
Print exactly integers forming a permutation of the input that maximizes the number of prime prefix sums.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
5
1 2 1 2 1
Output
2 1 2 1 1
Explanation
The ordering contains the same two copies of and three copies of as the input. Its prefix sums are , , , , . Four are prime: . As the total is , and these are all primes at most , no ordering can have more than four prime prefix sums.