It can be proven that the best number to create the array with, is the median (the middle element when the array is sorted) of the initial array. So, sort the array. Now, the median is the middle element $$$(med = a[n/2])$$$. To get the answer iterate over the array and add $$$abs(a_i - med)$$$ to it.
Proof why the median is the best choice We denote the median as $$$m$$$. We have the sorted array $$$a$$$ where some elements are smaller, some are equal and some are larger than $$$m$$$ in this order. Let's consider the other possible choices which are, choosing a smaller value than $$$m$$$ and a larger value than $$$m$$$. If we choose a greater value than $$$m$$$ then the difference between the elements smaller and equal to $$$m$$$ will increase by $$$x - m$$$. And the difference between the elements greater than $$$m$$$ will increase by $$$x - m$$$. But, because there are at least as many elements equal or smaller to $$$m$$$ as elements greater than $$$m$$$ we can see it is not optimal to choose this number, because in the best case, the difference wouldn't change and in the worst case, it would change by $$$x$$$. The same technique can be used for numbers smaller than $$$m$$$. This leaving $$$m$$$ as the best choice.