Chef and Sequence
All submissions for this problem are available.
Chef loves to play with sequence. One day a friend of chef gave him a sequence of integers a1, a2, ..., an. and told him to find the maximum of strength of the sequence. Defining the strength of the sequence to be |a1 - a2| + |a2 - a3| + ... + |an-1 - an| + |an - a1|. Chef wants to make his sequence stronger, so he reorders his sequence into a new sequenceb1, b2, ..., bn. He wants this new sequence to be as strong as possible. Help chef to find the maximum strength of the resulting sequence
The first line contains a single integer N denoting the number of elements in the sequence. The next line contains the sequence of N space seperated integers.
Print the maximum possible strength of the sequence.
- 1 ≤ N ≤ 105
- 10-9 ≤ A[i] ≤ 109
Input: 4 1 2 4 8 Output: 18 Explanation: We can reorder the given sequence to form 1 8 2 4, which yields the strength of |1-8| + |8-2| + |2-4| + |4-1| = 18.
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, CLOJ, COB, FS|
Fetching successful submissions