#P3195. Toy Compression Optimization

    ID: 16452 Type: Default 1000ms 256MiB

Toy Compression Optimization

Toy Compression Optimization

Professor P is heading to the Olympics in Beijing, but he cannot bear to leave his treasured toys behind. Instead, he decides to compress and transport all his toys using his special compressor, which can reduce any item to a 1-dimensional piece that can be placed in a unique container.

There are n toys numbered from 1 to n. After compression, the i-th toy has a length of Ci.

To ease the organization, Professor P requires that:

  • The toys in any one container must be consecutive in their numbering.
  • If a container holds more than one toy, a padding of unit length is inserted between every two adjacent toys. Formally, if toys numbered i to j (with i ≤ j) are placed together in one container, then the container's total length is given by \[ x = (j - i) + \sum_{k=i}^{j} C_k. \]

The cost to manufacture a container depends on its length x; specifically, if its length is x, the cost is \[ (x - L)^2, \] where L is a given constant. Professor P does not mind using any number of containers (even containers longer than L), but he wants the total manufacturing cost to be minimized.

Your task is to help him choose how to partition the toys into containers to minimize the overall cost.

inputFormat

The first line contains two integers n and L (1 ≤ n ≤ 105, L is an integer constant), representing the number of toys and the constant L respectively.

The second line contains n integers: C1, C2, ..., Cn, where Ci represents the compressed length of the i-th toy.

outputFormat

Output a single integer - the minimum total cost to manufacture the containers.

sample

3 5
1 2 3
5