Description

There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

Input

Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

Output

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

Sample Input

Sample Output

Source

已经快要wc了 然而我还在补基础qwq 菜死呢 当年基础学的不是很好的
虽然似乎很多大佬的blog的标签都给的是区间dp 然而我还是领悟不到精髓 还是喜欢套用leoly的dp方程 来搞首先预处理下dis[i][j]表示区间i~j只建一个邮局的最下花费是多少 可以类似dp转移dis[i][j]=dis[i][j-1]+a[j]-a[i+j>>1] 这样 为什么呢首先考虑我只有i~j的话 那么我一定是建在最中间最好 但为什么直接可以加 就是因为i~j-1那一块可以看成像一个滑块一样 往中间移动一下也不影响距离的总和
然后就是dp[i][k]表示前1~i个位置中我放了k个邮局的最小代价是多少 然后o(n^2*m)枚举转移下即可

 


elijahqi

辣鸡蒟蒻一枚qwq 欢迎加qq qwq 2922945330

发表评论

电子邮件地址不会被公开。 必填项已用*标注