Covered Walkway
Time limit10sMemory limit128 MB
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 to a position , the contractor charges , where is a constant. It is allowed that , in which case the charge is simply .
Given the points along the walkway and the constant , 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 and (, ), where is the number of points that must be covered and is the contractor's constant. Each of the following 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 to , 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.