n = int(input()) a = list(map(int, input().split())) a.sort() prefix_sum_left = [0] * (n + 1) for i in range(n): prefix_sum_left[i+1] = prefix_sum_left[i] + a[i] back_sum = [0] * (n + 1) for i in range(n-1, -1, -1): back_sum[i] = back_sum[i+1] + a[i] max_total = -float('inf') for i in range(n): current = a[i] s_left = i # Number of elements on the left that can be chosen s_right = n - 1 - i # Elements on the right max_s = min(s_left, s_right) if max_s == 0: sum_left = 0 sum_right = 0 else: # sum_left is sum of max_s elements in the left part (i-1, i-2, ..., i-max_s) start_left = i - max_s sum_left = prefix_sum_left[i] - prefix_sum_left[start_left] # sum_right is sum of max_s elements in the right part (largest max_s elements) start_right = n - max_s sum_right = back_sum[start_right] - back_sum[n] total = sum_left + sum_right - 2 * max_s * current max_total = max(max_total, total) print(max_total)