Water deposits P41867


Statement
 

pdf   zip

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.

Public test cases
  • Input

    4 42   5 5 5 5
    1 8    100
    7 100  10 30 14 6 50 15 15
    8 10   0 0 0 0 0 0 1000000000 1000000000
    

    Output

    0
    0
    551
    6000000070
    
  • Information
    Author
    Jordi Cortadella
    Language
    English
    Official solutions
    C++
    User solutions
    C++