eolymp
bolt
Try our new interface for solving problems
Məsələlər

Estimation

Estimation

"\textit{There are too many numbers here!}" your boss bellows. "\textit{How am I supposed to make sense of all of this? Pare it down! Estimate!}" You are disappointed. It took a lot of work to generate those numbers. But, you’ll do what your boss asks. You decide to estimate in the following way: You have an array \textbf{A} of numbers. You will partition it into \textbf{k} contiguous sections, which won’t necessarily be of the same size. Then, you’ll use a single number to estimate an entire section. In other words, for your array \textbf{A} of size \textbf{n}, you want to create another array \textbf{B} of size \textbf{n}, which has k contiguous sections. If \textbf{i} and \textbf{j} are in the same section, then \textbf{B\[i\]=B\[j\]}. You want to minimize the error, expressed as the sum of the absolute values of the differences (\textbf{Σ|A\[i\]-B\[i\]|}). \InputFile There will be several test cases in the input. Each test case will begin with two integers on a line, \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{2000}) and\textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{25}, \textbf{k} ≤ \textbf{n}), where \textbf{n} is the size of the array, and \textbf{k} is the number of contiguous sections to use in estimation. The array \textbf{A} will be on the next \textbf{n} lines, one integer per line. Each integer element of \textbf{A} will be in the range from \textbf{-10000}to \textbf{10000}, inclusive. The input will end with a line with two \textbf{0}s. \OutputFile For each test case, output a single integer on its own line, which is the minimum error you can achieve. Output no extra spaces, and do not separate answers with blank lines. All possible inputs yield answers which will fit in a signed\textbf{64}-bit integer.
Zaman məhdudiyyəti 15 saniyə
Yaddaşı istafadə məhdudiyyəti 128 MiB
Giriş verilənləri #1
7 2
6
5
4
3
2
1
7
0 0
Çıxış verilənləri #1
9
Mənbə The University of Chicago Invitational Programming Contest 2012 15 April 2012