cswork / OA打开练习工作台

AmazonOA #158

Find Maximum Calories

估计中等可在线判题

考点(AI 标注)贪心调度原地数组

The ground has height 0. You must jump off from the ground and visit every stone exactly once in any order, and you cannot return to the ground. Each jump contributes the square of the height difference between its start and end, and the total contribution should be maximized. Different stones with the same height must still each be visited once.

输入格式

The first line contains n, and the second line contains the stone heights. 1≤n≤100000, 1≤height≤46340.

输出格式

Output a single integer answer.

样例 1

输入:

3
1 2 3

输出:

14

样例 2

输入:

1
5

输出:

25

样例 3

输入:

3
2 2 2

输出:

4

样例说明

Example 1: the order 0→3→1→2 gives calories 9+4+1=14. Example 2: the only jump 0→5 contributes 25. Example 3: first jump from 0 to 2 for 4; the remaining jumps between equal heights give 0, for a total of 4.