#P6140. Lexicographically Smallest Cow Line

    ID: 19360 Type: Default 1000ms 256MiB

Lexicographically Smallest Cow Line

Lexicographically Smallest Cow Line

Farmer John plans to enter \(N\) (\(1 \le N \le 2000\)) cows into the annual "All-America Farm Owners Competition". In this contest, each contestant must line up his cows and then have them walk past the judges in order.

This year, the contest committee has introduced a new registration rule: For each cow, take the first letter of its name and, in the order of the original line, form a string. All teams will have their strings recorded and then sorted in lexicographical (dictionary) order to determine the order in which they perform.

Due to his busy schedule, Farmer John wants to appear as early as possible. Therefore, he decides to rearrange the line.

The rearrangement method is as follows: Repeatedly, choose either the cow at the beginning or the cow at the end of the current line, and append that cow to the end of a new line. Repeat until all cows have been moved into the new line. Your task is to determine the lexicographically smallest possible string (formed by the first letters of the cows' names in the new line) that can be achieved using this procedure.

Note: If there is a tie when comparing the two letters at the ends of the line, consider the subsequent letters in order until a difference is found.

inputFormat

The first line contains a single integer \(N\), the number of cows. The following \(N\) lines each contain a single uppercase letter, representing the first letter of a cow's name in the initial order.

outputFormat

Output a single line containing the lexicographically smallest string that can be obtained by the described process.

sample

4
A
C
D
B
ABCD