No.21 Difference of Averages
Problem Statement
You are given \(N\) numbers. Divide them into \(K(\geq 3)\) groups.
Each group must contain at least one number.
ex) For example, if the given numbers are \(\{10,3,23,91,5\}\) and \(K=3\), then
a division such as \(\{\{3,91\},\{23,5\},\{10\}\}\) is valid, while
a division such as \(\{\{\},\{3,5,10\},\{23,91\}\}\) is not allowed.
Compute the average of each group, then from these compute (largest average) - (smallest average),
and finally round the fractional part up; the resulting value is called the "difference of averages".
Find the difference of averages when the grouping is chosen so that the difference of averages is maximized.
Input
\(N\) \(K\) \(n_1\) \(n_2\) \(\dots\) \(n_N\)
The \(1\)st line contains \(N\ (3 \leq N \leq 9)\).
The \(2\)nd line contains \(K\ (3 \leq K \leq N)\).
Lines \(3\) through \(N+2\) contain the numbers \((1 \leq n_i \leq 1000, 1 \leq i \leq N)\).
Output
Output the numerical answer as a string.
Print a newline at the end.
Sample
Sample 1
Input
5 3 555 20 432 301 21
Output
535
For example, if we group them as \(\{\{555\}, \{21,20\}, \{433,301\}\}\),
the averages are \(\{555/1, (21+20)/2, (432+301)/2\} = \{555, 20.5, 366.5\}\), so
(largest average) - (smallest average) is \(555-20.5 = 534.5\).
Finally, rounding the fractional part up gives \(535\).
Sample 2
Input
8 4 329 980 656 738 739 542 873 501
Output
651
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。