Telephone Line

Time limit1sMemory limit128 MB

Summary
Raise each pole to height at least its original, paying squared increase plus C times adjacent height gaps, and minimize the total.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Math
Solved
No attempts yet

Problem

Jaehyeon wants to install a telephone line through a village.

The village has NN utility poles standing in a row; the initial height of the ii-th pole is HiH_i. Jaehyeon may first raise each pole by any amount (heights can never be lowered), and then runs the telephone line through poles 1,2,…,N1, 2, \dots, N in order. Let hih_i (hi≥Hih_i \ge H_i) be the final height of pole ii after raising.

  • Raising cost: raising a pole by XX costs X2X^2. So pole ii contributes (hi−Hi)2(h_i - H_i)^2.
  • Line cost: connecting two adjacent poles ii and i+1i+1 costs C×∣hi−hi+1∣C \times |h_i - h_{i+1}|.

Find the minimum total cost to raise the poles and connect the whole line. The total cost is the sum of all raising costs plus the sum of all line costs.

Input

The first line contains the number of poles NN and the cost coefficient CC, separated by a space. (1≤N≤100,0001 \le N \le 100{,}000, 1≤C≤1001 \le C \le 100)

Each of the next NN lines contains the initial height HiH_i of one pole. (1≤Hi≤1001 \le H_i \le 100)

Output

Print the minimum total cost to connect the entire telephone line on a single line.

Hint

Consider 55 poles with C=2C = 2 and initial heights 2,3,5,1,42, 3, 5, 1, 4. Raising them to [3,3,5,3,4][3, 3, 5, 3, 4] yields a total cost of 1515, and no smaller cost is possible.

Examples3

  1. Example 1

    Input
    5 2
    2
    3
    5
    1
    4
    
    Expected output
    15
    
  2. Example 2

    Input
    1 100
    50
    
    Expected output
    0
    
  3. Example 3

    Input
    3 100
    5
    5
    5
    
    Expected output
    0