CodeforcesJul 24, 2025

Lunar New Year and Number Division

Hazrat Ali

Codeforces
Lunar New Year and Number Division
 
 

Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem.

There are n positive integers a1,a2,,an on Bob's homework paper, where n is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least 2 numbers. Suppose the numbers are divided into m groups, and the sum of the numbers in the j-th group is sj. Bob's aim is to minimize the sum of the square of sj, that is

j=m.
 

Bob is puzzled by this hard problem. Could you please help him solve it?

Input

The first line contains an even integer n (2n310), denoting that there are n integers on Bob's homework paper.

The second line contains n integers a1,a2,,an (1ai10), describing the numbers you need to deal with.

Output

A single line containing one integer, denoting the minimum of the sum of the square of sj, which is

i=m
where m is the number of groups.

 

Examples
Input
4
8 5 2 3
Output
164
Input
6
1 1 1 2 2 2
Output
27

Solution
n = int(input())
a = list(map(int, input().split()))

a.sort()
ans = 0

for i in range(n // 2):
    pair_sum = a[i] + a[n - 1 - i]
    ans += pair_sum * pair_sum

print(ans)






Comments