Polish version    English version  
  History of OI -> VI OI 1998/1999 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
Stats
V OI 1997/1998
IV OI 1996/1997
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
VI Olimpiada Informatyczna 1998/1999

Task: MAP
Author:
Map

III stage contest  

After a new administrative division of Byteland cartographic office works on a new demographic map of the country. Because of technical reasons only a few colors can be used. The map should be colored so that regions with the same or similar population (number of inhabitants) have the same color. For a given color k let A(k) be the number, such that: 

  • at least half of regions with color k has population not greater than A(k)
  • at least half of regions with color k has population not less than A(k

A coloring error of a region is an absolute value of the difference between A(k) and the region's population. A cumulative error is a sum of coloring errors of all regions. We are looking for an optimal coloring of the map (the one with the minimal cumulative error).

Task

Write a program which:

  • reads the population of regions in Byteland from the input file MAP.IN,
  • computes the minimal cumulative error,
  • writes the result in the text file MAP.OUT.

Input

In the first line of the input file MAP.IN an integer n is written, which is the number of regions in Byteland, 10< n <3000. In the second line the number m denoting the number of colors used to color the map is written, 2 <= m <= 10. In each of the following n lines there is one non-negative integer - a population of one of the regions of Byteland. No population exceeds 2^30

Output

Your program should write in the only line of the output file MAP.OUT one integer number equal to a minimal cumulative error, which can be achieved while the map is colored (optimally).

Example

For the input file MAP.IN:

11
3
21
14
6
18
10
2
15
12
3
2
2
the correct answer is the output file MAP.OUT
15



Print friendly version