JPMorgan ChaseOA #14
Find Total Cost
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsGreedy
Each time, remove the current minimum and maximum elements of the array (they must be at two different positions) and insert their sum; the cost of this step is ceil((min+max)/(max−min+1)). Repeat until only one element remains, and find the total cost. Negative values are also rounded up in the mathematical sense.
Input
The first line contains n, followed by n integers. The original has no numeric bounds; this site adds 1≤n≤200000, with elements in −1000000000..1000000000.
Output
Output a single integer answer.
Example 1
Input:
5
2 3 4 5 7
Output:
8
Example 2
Input:
2
-2 -2
Output:
-4
Example 3
Input:
3
-4 1 2
Output:
0
Example notes
The outputs of the three public examples are, in order: 8; -4; 0. These answers have been checked by independent enumeration or direct simulation.
Editorial
Loading solution…