写法一
import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] f = new int[N];
for(int i = 0; i < N; ++i){
f[i] = sc.nextInt();
}
Arrays.sort(f);
long res = 0L;
for(int i = 0; i < N; ++i){
res += Math.abs(f[i] - f[i/2]);
}
System.out.println(res);
}
}
写法二
import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] f = new int[N];
for(int i = 0; i < N; ++i){
f[i] = sc.nextInt();
}
Arrays.sort(f);
long res = 0L;
for(int i = 0; i < N; ++i){
res += Math.abs(f[i] - f[N/2]);
}
System.out.println(res);
}
}