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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Các con số trên bảng (Töflur)
Töflur
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers, arrange them in a sequence minimizing the sum of squared differences between adjacent elements.
Input
The first line contains . The second line contains integers.
Output
Print the minimum possible sum.
Constraints
- .
- for .
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- 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.