eolymp
bolt
Try our new interface for solving problems
Problems

Bicolored Horses

Bicolored Horses

Every day, farmer Ion (this is a Romanian name) takes out all his horses, so they may run and play. When they are done, farmer Ion has to take all the horses back to the stables. In order to do this, he places them in a straight line and they follow him to the stables. Because they are very tired, farmer Ion decides that he doesn't want to make the horses move more than they should. So he develops this algorithm: he places the \textbf{1}st \textbf{P_1} horses in the first stable, the next \textbf{P_2} in the \textbf{2}nd stable and so on. Moreover, he doesn't want any of the \textbf{K} stables he owns to be empty, and no horse must be left outside. Now you should know that farmer Ion only has black or white horses, which don't really get along too well. If there are \textbf{i }black horses and \textbf{j} white horses in one stable, then the coefficient of unhappiness of that stable is\textbf{ i*j}. The total coefficient of unhappiness is the sum of the coefficients of unhappiness of every of the \textbf{K} stables. Determine a way to place the \textbf{N} horses into the \textbf{K} stables, so that the total coefficient of unhappiness is minimized. \InputFile On the \textbf{1}st line there are \textbf{2} numbers: \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{500}) and \textbf{K} (\textbf{1} ≤ \textbf{K} ≤ \textbf{N}). On the next \textbf{N} lines there are \textbf{N} numbers. The \textbf{i}-th of these lines contains the color of the \textbf{i}-th horse in the sequence: \textbf{1} means that the horse is black, \textbf{0} means that the horse is white. \OutputFile You should only output a single number, which is the minimum possible value for the total coefficient of unhappiness.
Time limit 1 second
Memory limit 16 MiB
Input example #1
6 3
1
1
0
1
0
1
Output example #1
2
Author Mugurel Ionut Andreica
Source Romanian Open Contest, December 2001