Water deposits

There are nn water deposits in a line. They are so huge that they can be considered to have infinite capacity. Initially, each deposit ii has i\ell_i liters in it. You have a pump that you can use to transfer water from any deposit ii to any adjacent deposit (i1i - 1 or i+1i + 1). Each use of the pump to transfer water between two deposits has cost p+p + \ell, where pp is a constant cost to connect two adjacent deposits, and \ell is the number of liters transferred. Your goal is to minimize the cost to equally distribute the water among all the deposits.

Input

Input consists of several cases, each with nn and pp, followed by 1,,n\ell_1, \dots, \ell_n. You can assume 1n1051 \le n \le 10^5, 0p1090 \le p \le 10^9, 0i1090 \le \ell_i \le 10^9, and that the sum of all i\ell_i’s is a multiple of nn.

Output

For each case, print the minimum cost to equally distribute the water among all the deposits.

Problem information

Author: Jordi Cortadella

Generation: 2026-01-25T11:07:01.207Z

© Jutge.org, 2006–2026.
https://jutge.org