PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000038. Các con số trên bảng (Töflur)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các con số trên bảng (Töflur)

Töflur

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn integers, arrange them in a sequence minimizing the sum of squared differences between adjacent elements.

Input

The first line contains nn. The second line contains nn integers.

Output

Print the minimum possible sum.

Constraints

  • 1≤n≤2⋅1051\le n\le 2\cdot10^5.
  • −109≤ai≤109-10^9\le a_i\le 10^9 for 1≤i≤n1\le i\le n.

Subtasks

  • Subtask 1 (20 points): n≤8n\le 8.
  • Subtask 2 (30 points): n≤30n\le 30.
  • Subtask 3 (50 points): Full constraints.

Examples

Input

4
1 4 2 8

Output

21

Explanation

The sample follows the ordering rule described in the Vietnamese explanation and produces the shown optimum.