atcoder#ARC146A. [ARC146A] Three Cards

[ARC146A] Three Cards

Score : 300300 points

Problem Statement

There are NN cards, numbered 11 to NN.

Card ii has a positive integer AiA_i written on it.

You can choose three of these cards and concatenate the integers written on them in any order you like to make a new integer. For example, if you choose cards with 11, 2323, and 44 written on them, you can make integers such as 12341234 and 42314231.

Find the maximum integer you can make.

Constraints

  • 3N2×1053 \le N \le 2 \times 10^5
  • 1Ai<1061 \le A_i < 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer.

5
1 4 3 5 8
854

If you choose cards with 44, 55, and 88 written on them, you can make 458458, 485485, 548548, 584584, 845845, or 854854.

You can make nothing greater than 854854, so the answer is 854854.

8
813 921 481 282 120 900 555 409
921900813