Bricks
Time limit3sMemory limit256 MB
Arrange every brick so neighbors differ and the ends are p and q, printing the lexicographically smallest valid line or 0.
Problem
Bitie and his friends spent all of yesterday playing with colored bricks at the kindergarten. They built models first, got bored with them, and then decided to lay the bricks out in one long line. To keep the line from looking dull they never put two bricks of the same color next to each other, and after a long while every brick was in place. The day care closed and the children went home.
Bitie came back early this morning and was glad to see the line still standing. Then he tripped and fell right onto it, and the bricks scattered into a pile. He sorted them by color and started thinking about how to rebuild the line quickly. He still remembers the colors of the two bricks that were at the ends.
You are given how many bricks Bitie has of each color and the two colors he remembers. Build a line in which neighboring bricks always have different colors, the first brick has color , and the last brick has color . Bitie may have remembered the colors wrong, or some bricks may have stayed lost after the fall, so the line cannot always be rebuilt.
Input
The first line contains the number of brick colors , the color of the first brick , and the color of the last brick , separated by single spaces. (, )
The second line contains integers separated by single spaces. Bitie has exactly bricks of color . ()
The total number of bricks is at most .
Output
Print the colors of the bricks in order on one line, separated by single spaces. The first color is , the last color is , and neighboring bricks have different colors.
When more than one line satisfies the conditions, print the lexicographically smallest one. When no line satisfies them, print the single integer 0.
Notes
To compare two lines and of the same length, look at the first position where their colors differ. The line with the smaller color number at that position comes first in lexicographic order.