Readability
InterviewTime limit2sMemory limit256 MB
Reorder n integers so odd and even values alternate, minimize the total moves |i-j|, and output the lexicographically smallest such order.
- Level
Medium6 of 10
- Topics
- Sorting, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
The Martian alphabet consists of integers from to . Odd integers are vowels, and even integers are consonants. A word is a sequence of such integers.
Changing the order of the integers in a word does not change its meaning, but it does change its pronunciation. If each pair of adjacent integers in a word contains one vowel and one consonant, the word is called readable.
Vasya Pupkin wants to open a restaurant of Earthern cuisine on Mars. He bought wooden integers for its name and placed them in a row on pedestals, numbered from to . The integer on pedestal is .
Moving an integer from pedestal to pedestal costs Martian rubles. During reordering, two or more integers may be on the same pedestal, but in the end each pedestal must have exactly one integer.
Find a readable word formed by reordering the integers that costs as few rubles as possible. If several such words exist, output the lexicographically smallest one.
A sequence is lexicographically smaller than a sequence of the same length if there exists an index such that for all , and .
Input
The first line contains an integer , the number of Martian letters in the word ().
The second line contains space-separated integers , the letters themselves ().
It is guaranteed that a solution exists.
Output
Output one line with space-separated integers: the required readable word.