#P7996. Coin Stack Variance Minimization

    ID: 21180 Type: Default 1000ms 256MiB

Coin Stack Variance Minimization

Coin Stack Variance Minimization

You are given n stacks of coins arranged in a row. In the i-th stack, all coins have a face value of ai. Each stack contains the same number of coins, denoted by a positive integer x. Consequently, the total amount for the i-th stack is ai \times x.

The variance of the total amounts of the stacks is defined as follows:

\( s^2 = \frac{(a_1\times x - \overline{(a\times x)})^2 + (a_2\times x - \overline{(a\times x)})^2 + \cdots + (a_n\times x - \overline{(a\times x)})^2}{n} \)

Since every stack is multiplied by the same x, the mean of the totals is \(\overline{(a\times x)} = x\times \overline{a}\), where \(\overline{a} = \frac{a_1+a_2+\cdots+a_n}{n}\). Thus, the variance simplifies to:

\( s^2 = x^2 \times \left( \frac{(a_1-\overline{a})^2+(a_2-\overline{a})^2+\cdots+(a_n-\overline{a})^2}{n} \right) \)

You are also given a positive integer k. Your task is to choose a positive integer x such that the absolute difference |s2 - k| is minimized. If there are multiple candidates, choose the smallest x.

inputFormat

The input consists of two lines:

  • The first line contains two positive integers n and k, where n is the number of coin stacks and k is the target positive integer for the variance.
  • The second line contains n integers: a1, a2, ..., an, representing the face value of coins in each stack.

outputFormat

Output a single integer x which is the number of coins per stack that makes the total amount variance s2 (computed as explained above) as close as possible to k in terms of absolute difference. In case of a tie, output the smaller x.

sample

3 10
1 2 3
4