编程介的小学生 2017-10-27 01:41 采纳率: 20.5%
浏览 721
已采纳

K-Monotonic

Description

A sequence of integer numbers is called strictly monotonically increasing if every term of the sequence is strictly greater than the one preceding it. Similarly, a sequence is called strictly monotonically decreasing if every term is strictly less than the one preceding it. A strictly monotonic sequence is a sequence that is either strictly monotonically increasing or decreasing. A sequence of integers is called k-monotonic if it can be decomposed into k disjoint contiguous subsequences that are strictly monotonic.

For example a strictly monotonically increasing sequence is 1-monotonic — in fact it is k-monotonic for every k between 1 and the number of elements it contains. The sequence { 1, 2, 3, 2, 1 } is 2-monotonic since it can be decomposed into { 1, 2, 3 } and { 2, 1 }.

If a sequence is not k-monotonic, you can transform it into a k-monotonic sequence by performing the following operation one or more times: select any term in the sequence and either increase it or decrease it by one. You are allowed to perform any number of these operations on any of the terms. Given a sequence of numbers A1, A2, …, An and an integer k, you are to calculate the minimum number of operations required to transform the given sequence into a k-monotonic sequence.

Input

The input contains multiple test cases.

Each test case contains consists of two lines. The first line gives the integers n (1 ≤ n ≤ 1000) and k (1 ≤ k ≤ min{ n, 10 }). The second line gives the integers A1, A2, …, An (−100 000 ≤ Ai ≤ 100 000).

A pair of zeroes indicates the end of the input and should not be processed.

Output

Output the answer of each test case on a separate line.

Sample Input

4 1
1 1 1 1
4 2
1 1 1 1
4 4
1 1 1 1
6 1
1 2 3 3 2 1
0 0
Sample Output

4
2
0
9

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-11-12 03:52
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题