#P12167. Maximizing the Minimum Water Level

    ID: 14269 Type: Default 1000ms 256MiB

Maximizing the Minimum Water Level

Maximizing the Minimum Water Level

Little Blue has nn bottles arranged from left to right. The ii-th bottle initially contains aia_i units of water. For aesthetic reasons, Little Blue dyes the water in a cyclic fashion using kk different colors; that is, the water in the ii-th bottle and the (i+k)(i+k)-th bottle have the same color.

Little Blue noticed that some bottles have too little water. Hence, he permits the following operation: if two bottles ii and jj (i<ji<j) have the same color, then an arbitrary integer amount of water may be poured from bottle ii to bottle jj.

The task is to determine the maximum possible value of X=min{ai}X=\min\{a_i\} that can be achieved after performing an arbitrary number of such operations.

It can be observed that the operations only allow water to be moved forward (from left to right) among bottles of the same color. Consider one color group consisting of bottles with indices i1,i2,,imi_1, i_2, \dots, i_m (with i1<i2<<imi_1 < i_2 < \dots < i_m). Let Sr=j=1raijS_r=\sum_{j=1}^{r}a_{i_j} be the prefix sum in that group. In order to achieve that every bottle in this group contains at least XX units of water, it is necessary and sufficient that for all r{1,2,,m}r\in\{1,2,\dots,m\}, the condition

$$S_r \ge rX$$

holds. Therefore, the maximum XX attainable for this group is

$$X = \min_{1\le r\le m} \left\lfloor \frac{S_r}{r} \right\rfloor.$$

The answer to the problem is the minimum of such values over all kk color groups.

inputFormat

The input is given in the following format:

n kn\ k
a1 a2  ana_1\ a_2\ \dots\ a_n

where nn and kk are positive integers, and aia_i represents the initial units of water in the ii-th bottle.

outputFormat

Output a single integer --- the maximum possible value of min{ai}\min\{a_i\} over all bottles that can be achieved after any number of operations.

sample

5 2
5 3 7 6 2
3

</p>