AmazonOA #158
Find Maximum Calories
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
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.
Input
The first line contains n, and the second line contains the stone heights. 1≤n≤100000, 1≤height≤46340.
Output
Output a single integer answer.
Example 1
Input:
3
1 2 3
Output:
14
Example 2
Input:
1
5
Output:
25
Example 3
Input:
3
2 2 2
Output:
4
Example notes
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.
Editorial
Loading solution…