FIICode 2025 Qualifying Round - Studenți | Maximize Grandi's Function

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 256MB Input: Output:

Task

We define the function Grandi(v,n)Grandi(v, n) (where vv is an array and nn is the number of elements of vv) as follows:

Grandi(v,n)=i=1nvi(1)i+1=v1v2+v3v4+v5±vnGrandi(v, n) = \displaystyle \sum_{i=1}^{n}{v_i \cdot (-1)^{i+1}}=v_1-v_2+v_3-v_4+v_5 - \ldots \pm v_n.

For example: Grandi([1,2,3,4,5],5)=12+34+5=3Grandi([1, 2, 3, 4, 5], 5) = 1 - 2 + 3 - 4 + 5 = 3.

You are given nn and an array v1,v2,,vnv_1,v_2,\ldots,v_n. Rearrange the elements of the array vv in such a way that Grandi(v,n)Grandi(v, n) is maximized.

Input data

The first line of input contains a single integer nn (1n1051 \le n \le 10^5) - the length of array vv.

The second line contains nn space separated integers v1,v2,,vnv_1,v_2,\ldots,v_n (109vi109-10^9 \le v_i \le 10^9) - the elements of array vv.

Output data

Print a single number, representing the maximum possible value of Grandi(v,n)Grandi(v, n) after optimally rearranging the elements of vv.

Example 1

stdin

4
5 9 10 2

stdout

12

Explanation

One of the optimal rearrangements of vv is [9,5,10,2][9,5,10,2], as Grandi([9,5,10,2],4)=95+102=12Grandi([9, 5, 10, 2], 4) = 9 - 5 + 10 - 2 = 12.

Example 2

stdin

5
3 2 1 4 5

stdout

9

Explanation

One of the optimal rearrangements of vv is [4,1,5,2,3][4,1,5,2,3], as Grandi([4,1,5,2,3],5)=41+52+3=9Grandi([4, 1, 5, 2, 3], 5) = 4 - 1 + 5 - 2 + 3 = 9.

Log in or sign up to be able to send submissions!