Insulator
InterviewTime limit1sMemory limit128 MB
Given n positive coefficients, reorder them so that the sum plus the total of positive rises between adjacent layers is as large as possible, and print that maximum.
Problem
The company Insumax produces multi-layer thermal insulators. Each insulator consists of layers, and the -th layer () is described by a positive integer insulation coefficient . The layers are numbered along the direction in which heat leaks out.
heat -> || a1 | a2 | ... | ai | ai+1 | ... | an || ->
The insulation coefficient of the whole insulator is the sum of the coefficients of its layers. In addition, whenever a layer is followed by a layer with a larger coefficient, grows by that difference. Formally,
For example, arranging the layers in the order gives .
Given the coefficients of the layers, arrange them so that the total insulation coefficient is maximized, and output that maximum value.
Input
The first line contains the number of layers (). Each of the next lines contains one coefficient . Every coefficient is an integer satisfying .
Output
Output a single integer: the largest possible value of the total insulation coefficient over all orderings of the layers.