Easy Strong Permutation
Practice
3.7 (322 votes)
Ad Hoc
Easy
Greedy algorithms
Problem
44% Success 4204 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Kevin has a sequence of integers a1, a2, ..., an. Define the strength of the sequence to be

|a1 - a2| + |a2 - a3| + ... + |an-1 - an| + |an - a1|.

Kevin wants to make his sequence stronger, so he reorders his sequence into a new sequence b1, b2, ..., bn. He wants this new sequence to be as strong as possible. What is the largest possible strength of the resulting sequence?

Input

The input consists of 2 lines. The first line has a positive integer n. The second line contains the n integers a1, a2, ..., an.

Output

Output a single integer: the maximum possible strength.

Constraints

1 <= n <= 105.

|ai| <= 109. Note that ai can be negative.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
36 votes
Tags:
Greedy algorithmAlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:20
105 votes
Tags:
ApprovedBasic ProgrammingBrute-force searchEasyReady
Points:20
15 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms