Insulator
Memory limit: 32 MB
The company Insumax produces multilayer thermal insulators. Each of the
layers, , of such an insulator
is characterized by positive insulation coefficient . The layers
are numbered according to the direction the heat leaks.
heat
The insulation coefficient of the whole insulator, , is described by
the sum of insulation coefficients of its layers. Moreover, the coefficient
raises if a layer with a smaller insulation coefficient is followed
by a layer with a larger coefficient, according to the formula:
.
For example, the insulation coefficient of the insulator of the form
is .
Task
Write a program which, for given insulation coefficients of layers determines such an ordering of the layers that the
insulation coefficient of the whole insulator is maximised.
Input
In the first line of the standard input there is the number of layers
, . In the successive
lines there are coefficients , one per line. Those coefficients are integers satisfying the
inequalities .
Output
In the first and only line of the standard output your program should write
one integer equal to the largest possible value of the insulation coefficient
of the insulator built of the layers of the given coefficients, put
in a particular order.
Example
For the input data:
4
5
4
1
7
the correct result is:
24
Task author: Zbigniew Czech.