Addon
Time limit1sMemory limit128 MB
Given the stable heights, find the largest chamber height admitting a safe and complete set of rod lengths, and output a smallest such set.
- Level
Hard8 of 10
- Topics
- Number theory, Combinatorics, Greedy
- Solved
- No attempts yet
Statement
Scientists discovered a new radioactive element called addon. It turned out to be the most efficient nuclear fuel ever known, so it was decided to build an addon reactor. According to the design, a fuel chamber is a vertical pipe. Fuel rods, that is addon cylinders, are placed in the chamber one above another. Fuel rods can have various lengths.
A reactor cycle begins with putting fuel rods into the chamber and then igniting them. Unfortunately, the height of a column of fuel cannot be arbitrary: only certain heights keep the reaction safe. Such heights are called stable heights.
The designers have two things to decide: the height of the fuel chamber and the set of lengths in which addon rods will be produced. A set of lengths is safe (for a given chamber) if for every column built from rods whose lengths belong to this set and that is not taller than the chamber, the height of the column is stable. A set of lengths is complete (for a given chamber) if every column of a stable height that is not taller than the chamber can be built from rods whose lengths are taken from this set.
Write a program that:
- reads the set of stable heights from standard input,
- computes the maximal height of a chamber for which a safe and complete set of lengths exists,
- finds, for such a chamber, a safe and complete set of lengths with the smallest possible number of elements,
- writes the result to standard output.
Input
The first line of standard input contains a natural number (), the number of given stable heights. Each of the next lines contains one positive integer no greater than . These numbers are the stable heights, given in increasing order.
Output
The first line of standard output should contain one integer: the maximal height of a chamber. The following lines should contain the computed set of lengths in increasing order, one number per line.