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].



Sample input:
5 5
3949 3774 3598 3469 3424


Sample output:
1541

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) == K and b[-1] == 1:
        b.reverse()
        b_index = 0
        height = A[0]
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] <= height:
                cnt += height - A[b_index] + 1
                height += 1
            else:
                height = A[b_index]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
if N == K:
    res = 0

    height = A[0]
    for i in range(1, N):
        if A[i] <= height:
            res += height - A[i] + 1
            height = height + 1
        else:
            height = A[i]
    print(res)
    exit()

for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) == K and b[-1] == 1:
        b.reverse()
        b_index = 0
        height = A[0]
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] - height <= 0:
                sub += height - A[b_index] + 1
                cnt += sub
                height = A[b_index] + sub
            else:
                height = A[b_index]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) == K and b[-1] == 1:
        b.reverse()
        b_index = 0
        height = A[0]
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] - height <= 0:
                sub += height - A[b_index] + 1
                cnt += sub
                height = A[b_index] + sub
            else:
                height = A[b_index]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) >= K and b[-1] == 1:
        b.reverse()
        b_index = 0
        height = A[0]
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] - height <= 0:
                sub += height - A[b_index] + 1
                cnt += sub
                height = A[b_index] + sub
            else:
                height = A[b_index]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) >= K:
        b.reverse()
        b_index = -1
        height = 0
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] - height <= 0:
                sub += height - A[b_index] + 1
                cnt += sub
                height = A[b_index] + sub
            else:
                height = A[b_index]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) >= K:
        b_index = -1
        height = 0
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] - height <= 0:
                sub += height - A[b_index] + 1
                cnt += sub
                height = A[b_index] + sub
            else:
                height = A[b_index]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

res = 1000000000
for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) >= K:
        b_index = -1
        height = 0
        cnt = 0
        while True:
            sub = 0
            if 1 in b[b_index + 1:]:
                b_index = b[b_index + 1:].index(1) + b_index + 1
            else:
                break

            if A[b_index] - height <= 0:
                sub += height - A[b_index] + 1
                cnt += sub

            height = A[b_index] + sub

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int,input().split())
a = list(map(int,input().split()))
lis = [x for x in range(1,N)]
m = 10**15

for con in itertools.combinations(lis, K-1):
  mm = 0
  f = a[0]
  for i in range(K-1):
    if f+1 <= a[con[i]]:
      f = a[con[i]]
    else:
      mm += (f+1) - a[con[i]]
      f += 1
  m = min(m,mm)
  
print(m)

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int,input().split())
a = list(map(int,input().split()))
f = a[0]
lis = [x for x in range(1,N)]
m = 10**15

for con in itertools.combinations(lis, K-1):
  mm = 0
  for i in range(K-1):
    mm += (f+i+1) - a[con[i]]
  m = min(m,mm)
  
print(m)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
INF = int(1e18)
def main():
    n, k = map(int, input().split())
    a = [int(i) for i in input().split()]
    p = [i for i in range(1,n)]
    #必ず見えるようにする建物の組合せを決める
    #左端の建物は常に見えるので注意する
    ans = INF
    for i in combinations(p, k-1):
        s = set(i)
        cost = 0
        mx = a[0]
        for j in range(1, n):
            if j in s:
                if a[j] > mx:
                    continue
                mx += 1
                cost += mx-a[j]
            mx = max(mx, a[j])
        ans = min(cost, ans)                
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))

ans = []
for i in range(2**N):
    s = a.copy()
    cnt = 0
    tmp = 1
    henkou = [False for _ in range(N)]
    for j in range(N):
        if((i >> j)&1):
            henkou[j] = True
    bill = s[0]
    for k in range(1, N):
        if(henkou[k]):
            if(bill >= s[k]):
                cnt += bill-s[k]+1
                s[k] = bill+1
                bill = s[k]
                tmp += 1
        else:
            if(bill < s[k]):
                bill = s[k]
                tmp += 1
    if(tmp >= K):
        ans.append(cnt)
print(min(ans), end='\n')

Submission status: Wrong Answer

Solution code:
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))

ans = []
for i in range(2**N):
    s = a.copy()
    cnt = 0
    tmp = 1
    henkou = [False for _ in range(N)]
    for j in range(N):
        if((i >> j)&1):
            henkou[j] = True
    bill = s[0]
    for k in range(1, N):
        if(henkou[k]):
            if(bill >= s[k]):
                cnt += bill-s[k]+1
                s[k] = bill+1
                bill = s[k]
                tmp += 1
        else:
            if(bill < s[k]):
                bill = s[k]
                tmp += 1
    if(tmp >= K):
        ans.append(cnt)
print(min(ans))
print()

Submission status: Wrong Answer

Solution code:
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))

ans = []
for i in range(2**N):
    s = a.copy()
    cnt = 0
    tmp = 1
    henkou = [False for _ in range(N)]
    for j in range(N):
        if((i >> j)&1):
            henkou[j] = True
    bill = s[0]
    for k in range(1, N):
        if(henkou[k]):
            if(bill >= s[k]):
                cnt += bill-s[k]+1
                s[k] = bill+1
                bill = s[k]
                tmp += 1
        else:
            if(bill < s[k]):
                bill = s[k]
                tmp += 1
    if(tmp >= K):
        ans.append(cnt)
print(min(ans))

Submission status: Wrong Answer

Solution code:
from itertools import combinations
INF = 16*(10**9)
def main():
    n, k = map(int, input().split())
    a = [int(i) for i in input().split()]
    p = [i for i in range(1,n)]
    #見えるようにする建物の組合せを決める
    #左端の建物は常に見えるので注意する
    ans = INF
    for i in combinations(p, k-1):
        s = set(i)
        cost = 0
        mx = -1
        for j in range(n):
            mx = max(a[j], mx)
            if j in s:
                mx += 1
                cost += mx-a[j]
        ans = min(cost, ans)                
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
if n == 1:
  print(0)
  exit()
  
dp = [[[float('inf')]*2 for i in range(k+1)] for i in range(n)]
for i in range(n-1):
  dp[i][1][0] = a[0]
  dp[i][1][1] = 0
  for j in range(min(k,i+2)):
    if a[i+1] <= dp[i][j][0]:
      dp[i+1][j+1][1] = min(dp[i][j][1]+dp[i][j][0]-a[i+1]+1,dp[i][j+1][1])
      if dp[i+1][j+1][1] == dp[i][j][1]+dp[i][j][0]-a[i+1]+1:
        dp[i+1][j+1][0] = dp[i][j][0]+1
      else:
        dp[i+1][j+1][0] = dp[i][j+1][0]
    elif dp[i][j][0] < a[i+1]:
      dp[i+1][j+1][1] = dp[i][j][1]
      dp[i+1][j+1][0] = a[i+1]
      
print(dp[n-1][k][1])

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
if n == 1:
  print(0)
  exit()
  
dp = [[[float('inf')]*2 for i in range(k+1)] for i in range(n)]
dp[0][0][0] = a[0]
dp[0][1][0] = a[0]
dp[0][1][1] = 0
for i in range(n-1):
  for j in range(min(k,i+2)):
    if a[i+1] <= dp[i][j][0]:
      dp[i+1][j+1][1] = min(dp[i][j][1]+dp[i][j][0]-a[i+1]+1,dp[i][j+1][1])
      if dp[i+1][j+1][1] == dp[i][j][1]+dp[i][j][0]-a[i+1]+1:
        dp[i+1][j+1][0] = dp[i][j][0]+1
      else:
        dp[i+1][j+1][0] = dp[i][j+1][0]
    elif dp[i][j][0] < a[i+1]:
      dp[i+1][j+1][1] = dp[i][j][1]
      dp[i+1][j+1][0] = a[i+1]
      
print(dp[n-1][k][1])

Submission status: Wrong Answer

Solution code:
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))

ans = []
for i in range(2**N):
    s = a.copy()
    cnt = 0
    tmp = 0
    henkou = [False for _ in range(N)]
    for j in range(N):
        if((i >> j)&1):
            henkou[j] = True
    bill = s[0]
    for k in range(1, N):
        if(henkou[k]):
            if(bill >= s[k]):
                cnt += bill-s[k]+1
                s[k] = bill+1
                bill = s[k]
        else:
            if(bill >= s[k]):
                tmp += 1
            else:
                bill = s[k]
    if(N-tmp >= K):
        ans.append(cnt)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
ans = float('inf')

for i in range(2**n):
    height = a[0]
    count = 1
    cost = 0
    for j in range(1,n):
        if (i>>j)&1:
            cost += max(0,(height-a[j]+1))
            height +=1
            count +=1
        else:
            if a[j] > height:
                count +=1
                height = a[j]
    if count >= k:
        ans = min(ans, cost)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
# print(A)

MIN = 10**17

for select in range(1<<(N-1)):
    if bin(select).count("1") == K - 1:
        MAX = A[0] + 1
        cost = 0
        for i in range(0,N-1):
            if (1<<i)&select:
                if A[i+1] < MAX:
                    cost += MAX - A[i+1]
                    MAX += 1               
                elif A[i+1] >= MAX:
                    MAX = A[i+1]
                    MAX += 1
            # print(i,select,MAX,cost)
        if MIN > cost:
            MIN = cost

print(MIN)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
# print(A)

MIN = 10**15

for select in range(1<<(N-1)):
    if bin(select).count("1") == K - 1:
        MAX = A[0] + 1
        cost = 0
        for i in range(0,N-1):
            if (1<<i)&select:
                if A[i+1] < MAX:
                    cost += MAX - A[i+1]
                    MAX += 1               
                elif A[i+1] >= MAX:
                    MAX = A[i+1]
                    MAX += 1
            # print(i,select,MAX,cost)
        if MIN > cost:
            MIN = cost

print(MIN)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
# print(A)

MIN = 10**15

for select in range(1<<(N-1)):
    if bin(select).count("1") == K - 1:
        MAX = A[0] + 1
        cost = 0
        for i in range(0,N-1):
            if (1<<i)&select and A[i+1] < MAX:
                    cost += MAX - A[i+1]
                    MAX += 1               
            elif A[i+1] >= MAX:
                MAX = A[i+1]
                MAX += 1
                print(i,select,MAX,cost)
        if MIN > cost:
            MIN = cost

print(MIN)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
# print(A)

MIN = 10**15
for select in range(1<<(N-1)):
    if bin(select).count("1") == K - 1:
        MAX = A[0] + 1
        cost = 0
        for i in range(0,N-1):
            if (1<<i)&select:
                if A[i+1] >= MAX:
                    MAX = A[i+1]
                    MAX += 1
                elif A[i+1] < MAX:
                    cost += MAX - A[i+1]
                    MAX += 1
                # print(i,select,MAX,cost)
        if MIN > cost:
            MIN = cost

print(MIN)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
NONE = 10 ** 9

mx = 0
nowK = []
for i,j in enumerate(A):
    if mx < j:
        nowK.append([i,j])
        mx = j

# print(A)

MIN = 10**9
for select in range(1<<(N-1)):
    if bin(select).count("1") == K - 1:
        MAX = A[0] + 1
        cost = 0
        for i in range(0,N-1):
            if (1<<i)&select:
                if A[i+1] >= MAX:
                    MAX = A[i+1]
                    MAX += 1
                elif A[i+1] < MAX:
                    cost += MAX - A[i+1]
                    MAX += 1
                # print(i,select,MAX,cost)
        if MIN > cost:
            MIN = cost

print(MIN)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
NONE = 10 ** 9

mx = 0
nowK = []
for i,j in enumerate(A):
    if mx < j:
        nowK.append([i,j])
        mx = j



for select in range(1<<(N-1)):
    if bin(select).count("1") == K - 1:
        MAX = A[0] + 1
        cost = 0
        for i in range(0,N-1):
            if (1<<i)&select:
                if A[i+1] >= MAX:
                    MAX = A[i+1]
                elif A[i+1] < MAX:
                    cost += MAX - A[i+1]
                    MAX += 1

print(cost)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
import random

input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
_START_TIME = time.time()


def main():
    ans = solve()

    if ans is True or ans is False:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    N, K = iip()
    A = iip()


    ans = 10**30
    for i in range(2**N):
        cn = 0
        mh = 0
        cost = 0
        m = 1
        for j in range(N):
            if (i % (m*2)) // m == 1:
                #print(1)
                cn += 1
                mh = max(mh+1, A[j])
                cost += mh-A[j]
            else:
                mh = max(mh, A[j])
            m += 1
        if cn >= K:
            ans = min(cost, ans)

    print(ans)


#####################################################ライブラリ集ここから

def kiriage_warizan(a, b):
    return -(-a//b)


def iip(listed=True):  # 数字のinputをlistで受け取る
    d = input().rstrip("\n").split()
    try:
        ret = [int(i) for i in d]
    except:
        ret = [int(i) if i in _NUMINT_ALL else i for i in d]
        if len(ret) == 1 and listed is False:
            return ret[0]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iipt(l, listed=True, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed))
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m

def time_debug_10percent():
    a = random.randint(0, 9)
    if a == 9:
        sys.exit()

def time_debug(t):
    time.sleep(_START_TIME + t - time.time())
    sys.exit()


def make_graph_edge_flat(N):  # グラフ作成のための辺をリストで返す
    ret = []
    for i in range(N-1):
        a, b, c = iip()
        a -= 1
        b -= 1
        ret[a].append((b, c))
        ret[b].append((a, c))
    return ret


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


def nibutan_func(func, target, left, right, side="left"): # 関数を二分探索
    l = left
    r = right
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if func(x) == target:
            return x
        elif func(x) > target:
            r = x
        else:
            l = x

    if side == "left" or func(x) == target:
        return l
    else:
        return r


def nibutan_list(list_, target, side="left"):  # リストを二分探索
    if not isinstance(list_, list):
        list_ = list(list_)

    l = 0
    r = len(list_)
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if list_[x] == target:
            return x
        elif list_[x] > target:
            r = x
        else:
            l = x

    if side == "left" or list_[x] == target:
        return l
    else:
        return r


class UfTree():
    def __init__(self, maxnum):
        self.parent = list(range(maxnum))
        self._size = [1] * maxnum
        self.rank = [0] * maxnum

    def size(self, a):
        return self._size[self.root(a)]

    def root(self, a):
        rank = 0
        cur = a
        while True:
            if self.parent[cur] == cur:
                #for i in path: # 経路圧縮
                #    self.parent[i] = cur
                return cur
            else:
                self.parent[cur] = self.parent[self.parent[cur]]
                cur = self.parent[cur]
                rank += 1

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return self

        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        return self

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
import random

input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
_START_TIME = time.time()


def main():
    ans = solve()

    if ans is True or ans is False:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    N, K = iip()
    A = iip()


    ans = 10**30
    for i in range(2**N):
        cn = 0
        mh = 0
        cost = 0
        for j in range(N):
            if (i % 2**(j+1)) // 2**j == 1:
                #print(1)
                cn += 1
                mh = max(mh+1, A[j])
                cost += mh-A[j]
        if cn >= K:
            ans = min(cost, ans)

    print(ans)


#####################################################ライブラリ集ここから

def kiriage_warizan(a, b):
    return -(-a//b)


def iip(listed=True):  # 数字のinputをlistで受け取る
    d = input().rstrip("\n").split()
    try:
        ret = [int(i) for i in d]
    except:
        ret = [int(i) if i in _NUMINT_ALL else i for i in d]
        if len(ret) == 1 and listed is False:
            return ret[0]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iipt(l, listed=True, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed))
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m

def time_debug_10percent():
    a = random.randint(0, 9)
    if a == 9:
        sys.exit()

def time_debug(t):
    time.sleep(_START_TIME + t - time.time())
    sys.exit()


def make_graph_edge_flat(N):  # グラフ作成のための辺をリストで返す
    ret = []
    for i in range(N-1):
        a, b, c = iip()
        a -= 1
        b -= 1
        ret[a].append((b, c))
        ret[b].append((a, c))
    return ret


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


def nibutan_func(func, target, left, right, side="left"): # 関数を二分探索
    l = left
    r = right
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if func(x) == target:
            return x
        elif func(x) > target:
            r = x
        else:
            l = x

    if side == "left" or func(x) == target:
        return l
    else:
        return r


def nibutan_list(list_, target, side="left"):  # リストを二分探索
    if not isinstance(list_, list):
        list_ = list(list_)

    l = 0
    r = len(list_)
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if list_[x] == target:
            return x
        elif list_[x] > target:
            r = x
        else:
            l = x

    if side == "left" or list_[x] == target:
        return l
    else:
        return r


class UfTree():
    def __init__(self, maxnum):
        self.parent = list(range(maxnum))
        self._size = [1] * maxnum
        self.rank = [0] * maxnum

    def size(self, a):
        return self._size[self.root(a)]

    def root(self, a):
        rank = 0
        cur = a
        while True:
            if self.parent[cur] == cur:
                #for i in path: # 経路圧縮
                #    self.parent[i] = cur
                return cur
            else:
                self.parent[cur] = self.parent[self.parent[cur]]
                cur = self.parent[cur]
                rank += 1

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return self

        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        return self

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
result = 10000000000*15
def j_cost(cost):
    c = 1
    for i in range(1,len(A)):
        if(max(cost[:i]) < cost[i]):
            c += 1
    #print(c,cost)
    if c >= k:
        return True
    else:
        return False

for i in range(2**n):
    cost = [0]*len(A)
    for j in range(1,n):
        if((i>>j)&1):
            total = [x + y for (x,y) in zip(A[:j],cost[:j])]
            cost[j] = max(total)+1-A[j]
    #print([x + y for (x,y) in zip(A,cost)])
    if(sum(cost) < result and j_cost([x + y for (x,y) in zip(A,cost)])):
        result = sum(cost)
print(result)