fork download
  1. n = int(input())
  2. x = list(map(int, input().split()))
  3. x.sort()
  4.  
  5. dp = [0] * (n + 1)
  6. for i in range(2, n + 1):
  7. dp[i] = dp[i - 2] + (x[i - 1] - x[i - 2])
  8.  
  9. if n % 2 == 1:
  10. res = min(dp[n - 1], dp[n])
  11. else:
  12. res = dp[n]
  13.  
  14. print(res)
Success #stdin #stdout 0.07s 13968KB
stdin
5
4 10 0 12 2
stdout
4