Problem:
Max Score: 350 Points

Problem Statement

There are N buildings along the line. The i-th building from the left is colored in color i, and its height is currently a_i meters.

Chokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least K buildings from the left.

You can increase height of buildings, but it costs 1 yens to increase 1 meters. It means you cannot make building that height is not integer.

You cannot decrease height of buildings.

Calculate the minimum cost of satisfying Chokudai's objective.

Note: "Building i can see from the left" means there are no j exists that (height of building j) ≥ (height of building i) and j < i.

Input Format

N K
a_1 a_2 a_3 ... a_N

Output Format

Print the minimum cost in one line. In the end put a line break.

Constraints

1 ≤ K ≤ N ≤ 15

1 ≤ a_i ≤ 10^9

Scoring

Subtask 1 [120 points]

N = K

Subtask 2 [90 points]

N ≤ 5

a_i ≤ 7

Subtask 3 [140 points]

There are no additional constraints.

Sample Input 1

5 5
3949 3774 3598 3469 3424

Sample Output 1

1541

The optimal solution is (height of buildings from the left) = [3949, 3950, 3951, 3952, 3953].

Sample Input 2

5 3
7 4 2 6 4

Sample Output 2

7

The optimal solution is (height of buildings from the left) = [7, 8, 2, 9, 4].

Solution in PyPy3 (7.3.0) (judge verdict: Wrong Answer)

import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N, K = MI()
    if N == 1 or K == 1:
        print(0)
        sys.exit()

    myT = LI()
    result = 1000000000000
    dq = deque()
    dq.append([1,myT[0],0])
    for i in range(1, N):
        new_dq = deque()
        for num, height, cost in dq:
            num1 = num+1
            height1 = max(myT[i], height+1)
            cost1 = cost + height1 - myT[i]
            if cost < result and num + N - i > K:
                new_dq.append([num, height, cost])
            if num1 == K:
                result = min(result, cost1)
            elif cost1 < result and num1 + N - i > K:
                new_dq.append([num1, height1, cost1])
        if not new_dq:
            break
        else:
            dq = new_dq
    print(result)
        
if __name__ == "__main__":
    main()