Problem list
Log in
#usaco0123 · USACO 2013-2014 January Bronze

Ski Course Design

Silver V 2.0 s 256 MB

Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer elevation in the range 0 .. 100. In the winter, since there is abundant snow on these hills, FJ routinely operates a ski training camp.

Unfortunately, FJ has just found out about a new tax that will be assessed on farms used as ski training camps. Upon careful reading of the law, however, he discovers that the official definition of a ski camp requires the difference between the highest and lowest hill on his property to be strictly larger than 17. Therefore, if he shortens his tallest hills and adds mass to increase the height of his shorter hills, FJ can avoid paying the tax as long as the new difference between the highest and lowest hill is at most 17.

If it costs x^2 units of money to change the height of a hill by x units, what is the minimum amount of money FJ will need to pay? FJ is only willing to change the height of each hill by an integer amount.

Input

  • Line 1: The integer N.
  • Lines 2..1+N: Each line contains the elevation of a single hill.

Output

The minimum amount FJ needs to pay to modify the elevations of his hills so the difference between largest and smallest is at most 17 units.

Example 1

Input
5
20
4
1
24
21
Output
18

Output details: FJ keeps the hills of heights 4, 20, and 21. He raises the hill of height 1 to 4 (cost 9) and shortens the hill of height 24 to 21 (cost 9).

Source
USACO 2013-2014 January Bronze
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code