Water deposits

There are n water deposits in a line. They are so huge that they can be
considered to have infinite capacity. Initially, each deposit i has
ℓ_(i) liters in it. You have a pump that you can use to transfer water
from any deposit i to any adjacent deposit (i − 1 or i + 1). Each use of
the pump to transfer water between two deposits has cost p + ℓ, where p
is a constant cost to connect two adjacent deposits, and ℓ 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 n and p, followed by
ℓ₁, …, ℓ_(n). You can assume 1 ≤ n ≤ 10⁵, 0 ≤ p ≤ 10⁹, 0 ≤ ℓ_(i) ≤ 10⁹,
and that the sum of all ℓ_(i)’s is a multiple of n.

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
