#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int arr2[100005];
int main()
{
int n;
scanf("%d", &n);
for(int i=0;i<n;i++){
scanf("%d", &arr[i]);
}
sort(arr, arr+n);
long total=0;
for(int lo=0,hi=n-1,k=0;lo<=hi;++lo,--hi){
arr2[k]=arr[lo];
k++;
arr2[k]=arr[hi];
k++;
}
for(int i=0;i<n;i++){
total+=(long)abs(arr2[i]-arr2[(i+1) % n]);
}
printf("%ld\n", total);
return 0;
}
- Problem Description
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 <= 10^5.
|ai| <= 10^9.
Note that ai can be negative.
- Test Case 1
Input (stdin)4
1 2 4 8
Expected Output18
- Test Case 2
Input (stdin)5
1 2 4 8 10
Expected Output30
No comments:
Post a Comment