Covered Walkway

Time limit10sMemory limit128 MB

Summary
Cover all required points on a line with segments, where covering from x to y costs c plus (x - y) squared, minimizing total cost.
Level

Medium7 of 10

Topics
Dynamic programming, Divide and conquer, Prefix sum
Solved
No attempts yet

Problem

A university wants to build a new walkway, and at least part of it must be covered. There are certain points along the walkway that must be covered; whether any other points are covered or not does not matter.

The building contractor uses the following pricing scheme. To cover the walkway from a position xx to a position yy, the contractor charges c+(x−y)2c + (x - y)^2, where cc is a constant. It is allowed that x=yx = y, in which case the charge is simply cc.

Given the points along the walkway and the constant cc, find the minimum cost to cover every point that must be covered.

Input

The input contains several test cases. Each test case begins with a line containing two integers nn and cc (1≤n≤1,000,0001 \le n \le 1{,}000{,}000, 1≤c≤1091 \le c \le 10^9), where nn is the number of points that must be covered and cc is the contractor's constant. Each of the following nn lines contains a single integer, a point along the walkway that must be covered. The points are given in order from smallest to largest. Every point is in the range from 11 to 10910^9, inclusive. The input ends with a line containing two zeros (0 0).

Output

For each test case, output a single integer: the minimum cost to cover all of the specified points. Print each integer on its own line, with no spaces, and do not print any blank lines between answers. For every possible input, the answer fits in a signed 64-bit integer.

Examples4

  1. Example 1

    Input
    10 5000
    1
    23
    45
    67
    101
    124
    560
    789
    990
    1019
    0 0
    
    Expected output
    30726
    
  2. Example 2

    Input
    1 1
    5
    0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    2 10
    7
    7
    0 0
    
    Expected output
    10
    
  4. Example 4

    Input
    2 5
    1
    100
    0 0
    
    Expected output
    10