No.972 Score of a Selection
Problem Statement
When you choose $k$ elements ( $k \ge 1$ ) from a sequence of positive integers $a_1,...,a_N$ consisting of $N$ elements and call them $b_1,...,b_k$,
the score $S$ you obtain is expressed as follows, using the median $m$ (described below) of the $k$ elements.
\[
S = \sum_{i=1}^{k} (b_i - m)
\]
Find the maximum value of $S$.
It can be shown that the answer is always an integer. Output it as an integer.
Note: The median $m$ of $k$ numbers is expressed as follows, where $x_1,...,x_k$ is the sequence obtained by sorting them in ascending order.
\[
m =
\begin{cases}
\ x_{\frac{k+1}{2}} \ (kが奇数) \\
\ \frac{1}{2} \times (x_{\frac{k}{2}} + x_{\frac{k}{2} +1 }) \ (kが偶数)
\end{cases}
\]
Input
$N$ $a_1\ a_2\ ...\ a_N$
- $1 \le N \le 2\times 10^5$
- $1 \le a_i \le 10^9 $
- All inputs are integers.
Output
Output the answer.
Print a newline at the end.
Sample
Sample 1
Input
5 1 2 3 4 5
Output
2
When $b=\{1,2,5\}$, the score is $(1-2)+(2-2)+(5-2)=2$. This is the maximum value.
Sample 2
Input
4 7 7 7 9
Output
2
The maximum value can be achieved by choosing all the elements.
Sample 3
Input
10 3 8 9 7 2 2 10 10 35 5
Output
37
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
沙耶花
shibh308