This page is still under construction.

Parts of this page are still being built. What you see may change.

Map

Time limit3sMemory limit128 MB

Summary
Partition n populations into m groups to minimize the sum over each group of |value - group median|, where the median can be any value meeting the half-half condition.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Greedy, Prefix sum
Solved
No attempts yet

Problem

After redrawing the administrative divisions of Byteland, the cartographic office is preparing a new demographic map of the country. For technical reasons only a limited number of colors is available. The map must be colored so that regions with the same or similar population (number of inhabitants) receive the same color.

For a given color kk, let A(k)A(k) be a value such that:

  • at least half of the regions colored kk have population not greater than A(k)A(k), and
  • at least half of the regions colored kk have population not less than A(k)A(k).

In other words, A(k)A(k) is a median of the populations of the regions colored kk.

The coloring error of a region colored kk is ∣A(k)−p∣|A(k) - p|, where pp is that region's population. The cumulative error is the sum of the coloring errors of all regions. We are looking for an optimal coloring, that is, one with the minimum cumulative error.

Write a program that reads the populations of the regions of Byteland, computes the minimum cumulative error, and writes the result to standard output.

Input

The first line contains an integer nn, the number of regions of Byteland, with 10<n<300010 < n < 3000.

The second line contains an integer mm, the number of colors used to color the map, with 2≤m≤102 \le m \le 10.

Each of the next nn lines contains one non-negative integer, the population of one region. No population exceeds 2302^{30}.

Output

Output a single line containing one integer: the minimum cumulative error that can be achieved by an optimal coloring.

Examples1

  1. Example 1

    Input
    11
    3
    21
    14
    6
    18
    10
    2
    15
    12
    3
    2
    2
    
    Expected output
    15