luogu#P4767. [IOI 2000] 邮局 加强版

    ID: 3762 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学2000IOI枚举区间 DP四边形不等式

[IOI 2000] 邮局 加强版

Problem Description

There are some villages along a highway. The highway is represented as the integer number line, and the position of each village is given by a single integer coordinate. No two villages are at 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, villages. To build the post offices, you should choose their locations so that the sum of distances from each village to its nearest post office is minimized.

You need to write a program that, given the positions of the villages and the number of post offices, computes the minimum possible total sum of distances from each village to its nearest post office.

Input Format

The first line contains two integers: the first is the number of villages VV, and the second is the number of post offices PP.

The second line contains VV integers. These integers are the positions of the villages.

Output Format

The first line contains one integer SS, which is the total sum of distances from each village to its nearest post office.

10 5 
1 2 3 6 7 9 11 22 44 50
9

Hint

For 40%40\% of the testdata, V300V \leq 300.

For 100%100\% of the testdata, 1P3001 \leq P \leq 300, PV3000P \leq V \leq 3000, and 11 \leq village positions 10000\leq 10000.

Translated by ChatGPT 5