Anneal
Glass is produced molten in melting furnaces, where very high temperatures prevail, and gradually cooled uniformly and in a controlled manner, in order to avoid breakage or cracks, and to maintain high product quality. A glass production company has a series of cooling chambers and cools the product by passing it successively through all of them. The chambers are located in a straight line and are numbered along it, starting from the one closest to the melting furnace. Before the production process begins, the temperature prevailing in each cooling chamber is known. In order for the cooling process to proceed without problems, care is taken so that as the glass passes from one chamber to the next, there is no increase in temperature. This is achieved either by reducing the temperature of a chamber from to , which requires energy equal to the temperature difference , or by bypassing chamber , which requires energy equal to twice the temperature . The existing infrastructure of the company does not allow either an increase in the temperature of a chamber or any rearrangement of the chambers in the sequence through which the glass passes through them (meaning the glass cannot pass through another chamber before passing through chamber ).
The minimum required amount of energy to have a sequence of chambers along which the temperature does not increase is requested.
Input Files
The first line of input will contain exactly one integer: the number of chambers . The second line will contain positive integers , separated by spaces: the initial temperatures of the chambers.
Output Files
The output should consist of one line containing exactly one integer: the minimum required amount of energy to have a sequence of chambers along which the temperature does not increase.
Constraints
Execution time limit: 1 sec.
Example of Input - Output Files:
STDIN (anneal.in)
8
55 10 80 50 20 40 70 60
STDOUT (anneal.out)
135
Explanation of the Example
We bypass the nd and the th chambers, and we reduce the temperature of the rd chamber to , and of the th and th chambers to . The resulting sequence of temperatures is [, , , , , , , ], where "" denotes the chambers that have been bypassed. The energy cost is: for reducing the temperature in chambers , , and respectively, and for chambers and , respectively, that we bypassed.
Comments