Hide

Problem A
Töflur

Languages en is

You really like playing games with your friend. You are currently playing a game where each player is given n tiles with numbers on them. Each player then places the tiles down such that they form a sequence. Let aj denote the number on the j-th tile in the sequence, for j=1,2,,n. The score of the sequence is then computed by adding up the squares of differences of adjacent tiles, that is

j=1n1(ajaj+1)2.

The player with the lowest score wins.

Input

The first line of the input is an integer n, the number of tiles you have, where 1n106. The next line consists of n integers each being at least one, but not larger than 106.

Output

The only line of the output should contain one integer, the lowest score you can achieve by arranging your tiles optimally.

Scoring

Groups

Points

Constraints

1

15

n=3

2

42

n18

3

43

No further constraints

Sample Input 1 Sample Output 1
9
1 2 3 1 1 2 2 3 3
2
Sample Input 2 Sample Output 2
7
4 8 7 25 95 97 6
5199
Hide

Please log in to submit a solution to this problem

Log in