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()))

#始めの山頂の位置を検索
num = 0
for l in range(n - 1) :
    if a[l] > a[l - 1] :
        num = l
        break

#選び方
all_on_list = [] 
for i in range(2 ** n) :
    on_list = []
    for j in range(n) :
        if (i >> j) & 1 :
            on_list.append(1)
        else:
            on_list.append(0)
    
    if on_list.count(1) == k :
        if on_list[num] == 1 :
            all_on_list.append(on_list)     

#合計値の最小
min_total = 10 ** 9
for m in all_on_list  :
    h_list = []
    for o in range(n) :
        if m[o] == 1 :
            h_list.append(a[o])
    
    total = 0
    for h in range(k - 1) :
        if h_list[h] >= h_list[h + 1] :
            b = h_list[h] - h_list[h + 1] + 1
            total += b
            h_list[h + 1] += b
    
    min_total = min(min_total, total)

print(min_total)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    a = [int(x) for x in input().split()]

    ans = 10 ** 10
    for bit in range(1 << N):
        can = set()
        for i in range(1, N):
            if bit & (1 << i):
                can.add(i)
        h_max = a[0]
        cost = 0
        cnt = 1
        for i in range(1, N):
            # res = h_max - a[i] + 1 if i in can and a[i] <= h_max else 0
            # cost += res
            # cnt += 1 if a[i] + res > h_max else 0
            # h_max = max(h_max, a[i] + res)

            if i in can:
                cost += h_max - a[i] + 1
                h_max += 1
            h_max = max(h_max, a[i])

        # if cnt >= K:
        if len(can) >= K - 1:
            ans = min(ans, cost)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    a = [int(x) for x in input().split()]

    ans = 10 ** 10
    for bit in range(1 << N):
        can = set()
        for i in range(1, N):
            if bit & (1 << i):
                can.add(i)
        h_max = a[0]
        cost = 0
        cnt = 1
        for i in range(1, N):
            res = h_max - a[i] + 1 if i in can and a[i] <= h_max else 0
            cost += res
            cnt += 1 if a[i] + res > h_max else 0
            h_max = max(h_max, a[i] + res)
        if cnt >= K:
            ans = min(ans, cost)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    a = [int(x) for x in input().split()]

    ans = 10 ** 9
    for bit in range(1 << N):
        can = set()
        for i in range(1, N):
            if bit & (1 << i):
                can.add(i)
        h_max = a[0]
        cost = 0
        cnt = 1
        for i in range(1, N):
            res = (h_max - a[i] + 1) if i in can and a[i] <= h_max else 0
            cost += res
            cnt += 1 if a[i] + res > h_max else 0
            h_max = max(h_max, a[i] + res)
        if cnt >= K:
            ans = min(ans, cost)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    a = [int(x) for x in input().split()]

    ans = 10 ** 9
    for bit in range(1 << N):
        can = set()
        for i in range(1, N):
            if bit & (1 << i):
                can.add(i)
        h_max = a[0]
        cost = 0
        cnt = 1
        can_build = [a[0]]
        for i in range(1, N):
            res = h_max - a[i] + 1 if i in can and a[i] <= h_max else 0
            cost += res
            can_build.append(a[i] + res)
            cnt += 1 if can_build[i] > h_max else 0
            h_max = max(h_max, a[i] + res)
        if cnt >= K:
            ans = min(ans, cost)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
import copy

def main():
    N,K = map(int, input().split())
    a_list = [int(e) for e in input().split()]

    #見えるようにする建物を選ぶ
    #当該の建物まで左から見ていく
    #当該の建物より max((左の最大値+1 - 現状の高さ),0)が各建物に必要なコスト
    #各建物に必要なコストを合計する

    ans = 10**100

    l = [i for i in range(0,N)]

    for v in itertools.combinations(l,K): #最重で6435通り
        copied_a_list = copy.copy(a_list)
        
        v = set(v)
        left_max = copied_a_list[0]-1
        temp_ans = 0
        
        for i in range(N): #15
            if i in v:
                temp_ans += max(0,left_max+1 - copied_a_list[i])
                copied_a_list[i] = left_max+1
            
            if copied_a_list[i] > left_max:
                left_max = copied_a_list[i]
        
        #print(v,copied_a_list,temp_ans)
        
        if temp_ans < ans:
            ans = temp_ans
            
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
import itertools
import copy

def main():
    N,K = map(int, input().split())
    a_list = [int(e) for e in input().split()]

    #見えるようにする建物を選ぶ
    #当該の建物まで左から見ていく
    #当該の建物より max((左の最大値+1 - 現状の高さ),0)が各建物に必要なコスト
    #各建物に必要なコストを合計する

    ans = 10**100

    l = [i for i in range(0,N)]

    for v in itertools.combinations(l,K): #最重で6435通り
        copied_a_list = copy.copy(a_list)
        
        v = set(v)
        left_max = copied_a_list[0]-1
        temp_ans = 0
        
        for i in range(N): #15
            if i in v:
                temp_ans += max(0,left_max+1 - copied_a_list[i])
                copied_a_list[i] = left_max+1
            
            if copied_a_list[i] > left_max:
                left_max = copied_a_list[i]
        
        print(v,copied_a_list,temp_ans)
        
        if temp_ans < ans:
            ans = temp_ans
            
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
import itertools
import copy

def main():
    N,K = map(int, input().split())
    a_list = [int(e) for e in input().split()]

    #見えるようにする建物を選ぶ
    #当該の建物まで左から見ていく
    #当該の建物より max((左の最大値+1 - 現状の高さ),0)が各建物に必要なコスト
    #各建物に必要なコストを合計する

    ans = 15*(10**9)

    l = [i for i in range(0,N)]

    for v in itertools.combinations(l,K): #最重で6435通り
        copied_a_list = copy.copy(a_list)
        
        v = set(v)
        left_max = copied_a_list[0]-1
        temp_ans = 0
        
        for i in range(N): #15
            if i in v:
                temp_ans += max(0,left_max+1 - copied_a_list[i])
                copied_a_list[i] = left_max+1
            
            if copied_a_list[i] > left_max:
                left_max = copied_a_list[i]
        
        #print(v,copied_a_list,temp_ans)
        
        if temp_ans < ans:
            ans = temp_ans
            
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(1,N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K-1): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1, N):
        if i in target_indexes:
            if copy_A[i] < max(copy_A[:i]):
                diff = max(copy_A[:i]) - copy_A[i] # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
                copy_A[i] += diff + 1
                cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(1,N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K-1): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1, N):
        if i in target_indexes:
            diff = max(max(copy_A[:i]) - copy_A[i], 0) # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
            copy_A[i] += diff + 1
            cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(1,N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1, N):
        if i in target_indexes:
            diff = max(max(copy_A[:i]) - copy_A[i], 0) # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
            copy_A[i] += diff + 1
            cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1, N):
        if i in target_indexes:
            diff = max(max(copy_A[:i]) - copy_A[i], 0) # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
            copy_A[i] += diff + 1
            cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1,N):
        if i in target_indexes:
            diff = max(max(copy_A[:i]) - copy_A[i], 0) # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
            copy_A[i] += diff + 1
            cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,k = inpm()
A = inpl()

ans = float('inf')
for bi in itertools.product([0,1],repeat=n-1):
    if bi.count(1) < k-1:
        continue

    pre = A[0]
    s = 0
    for i in range(1,n):
        pre = max(pre+1,A[i])
        if bi[i-1] == 1:
            s += max(0,pre-A[i])
    ans = min(ans,s)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,k = inpm()
A = inpl()

ans = float('inf')
for bi in itertools.product([0,1],repeat=n-1):
    if bi.count(1) < k-1:
        continue

    l = [A[0]]
    for i in range(1,n):
        if bi[i-1] == 1:
            l.append(A[i])
    pre = l[0]
    s = 0
    for a in l[1:]:
        pre = max(pre+1,a)
        s += max(0,pre-a)
    ans = min(ans,s)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,k = inpm()
A = inpl()

ans = float('inf')
for bi in itertools.product([0,1],repeat=n-1):
    if bi.count(1) != k-1:
        continue

    l = [A[0]]
    for i in range(1,n):
        if bi[i-1] == 1:
            l.append(A[i])
    pre = l[0]
    s = 0
    for a in l[1:]:
        pre = max(pre+1,a)
        s += max(0,pre-a)
    ans = min(ans,s)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import sys
import itertools

# 標準入力を取得
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

# 求解処理
ans = sys.maxsize
for buildings in itertools.combinations(range(N), K):
    cost = 0
    highest = a[0] - 1
    for n in range(N):
        a_n = a[n]
        increase = 0
        if n in buildings:
            increase = highest - a_n + 1
            cost += max(increase, 0)
        highest = max(highest, a_n + increase)
    ans = min(ans, cost)

# 結果出力
print(ans)

Submission status: Wrong Answer

Solution code:
# 1
if __name__ == "__main__":
    N,K = map(int,input().split())
    a = list(map(int,input().split()))
    cost = 0
    for i in range(1,len(a)):
        if a[i-1] >= a[i]:
            cost += (a[i-1]-a[i]+1)
            a[i] += a[i-1]-a[i]+1
    print(cost)

Submission status: Wrong Answer

Solution code:
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()

Submission status: Wrong Answer

Solution code:
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 = float('inf')
    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()

Submission status: Wrong Answer

Solution code:
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 = float('inf')
    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)
                continue
            if 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()

Submission status: Wrong Answer

Solution code:
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:
        print(0)
        sys.exit()
    myT = LI()
    result = float('inf')
    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)
                continue
            if 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()

Submission status: Wrong Answer

Solution code:
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:
        print(0)
        sys.exit()
    myT = LI()
    result = float('inf')
    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)
                continue
            if cost1 < result and num1 + N - i > K:
                new_dq.append([num1, height1, cost1])
        dq = new_dq
    print(result)
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
ans = 10**9

for i in range(1<<N):
    seen = 1
    pay = 0
    now = a[0]
    for j in range(1, N):
        if (i >> j) & 1:
            if a[j] <= now:
                pay += now - a[j] + 1
                now += 1
                seen += 1
            else:
                break
        else:
            if a[j] > now:
                seen += 1
                now = a[j]
        if seen >= K:
            ans = min(ans, pay)
            break


print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = tuple(map(int, input().split()))
import itertools
import numpy as np
ans_cnt = np.Inf
for now in itertools.combinations(range(1,N), K-1):
    height = A[0]
    cnt = 0
    for i in range(len(now)):
        if A[now[i]] > height:
            height = A[now[i]]
        else:
            height += 1
            cnt += height - A[now[i]]
    ans_cnt = min(cnt, ans_cnt)
print(ans_cnt)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))

ans = float('inf')
for bit in range(2**(n-1)):
    marubatu = []
    for i in range(n-1):
        if bit & (1<<i):
            marubatu.append(i+1)
    if len(marubatu)>=(k-1):
        ans_pre = 0
        n_h = A[0]
        for num in marubatu:
            if n_h >= A[num]:
                ans_pre += n_h+1-A[num]
                n_h += 1
            else:
                n_h = A[num]
        ans = min(ans, ans_pre)
    
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))

    min_cost = 10**13
    for x in range(2**n):
        cost = 0
        height = a[0]

        xbit = bin(x)[2:].zfill(n)
        if xbit[0]!="1":
            continue
        xbit_list = [int(xbit[ni]) for ni in range(n)]
        color_num = sum(xbit_list[1:])

        if color_num!=k - 1:
            continue
        for ni in range(1, n):
            if xbit_list[ni]: 
                if a[ni] <= height + 1:
                    cost += height + 1 - a[ni]
                    height+=1
                else:
                    height = a[ni]

        if cost < min_cost:
            min_cost = cost

    print(min_cost)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))

    min_cost = 10**13
    for x in range(2**n):
        cost = 0
        height = a[0]

        xbit = bin(x)[2:].zfill(n)
        if xbit[0]!="1":
            continue
        xbit_list = [int(xbit[ni]) for ni in range(n)]
        color_num = sum(xbit_list[1:])

        if color_num!=k - 1:
            continue
        for ni in range(1, n):
            if xbit_list[ni]: 
                if a[ni] < height + 1:
                    cost += height + 1 - a[ni]
                    height+=1
                else:
                    height = a[ni]

        if cost < min_cost:
            min_cost = cost

    print(min_cost)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))

    min_cost = 10**13
    for x in range(2**n):
        cost = 0
        height = a[0]

        xbit = bin(x)[2:].zfill(n)
        if xbit[0]!="1":
            continue
        xbit_list = [int(xbit[ni]) for ni in range(n)]
        color_num = sum(xbit_list[1:])
        if color_num!=k - 1:
            continue
        for ni in range(1, n):
            if xbit_list[ni]: 
                cost += height + 1 - a[ni]
                height+=1

        if cost < min_cost:
            min_cost = cost

    print(min_cost)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))

    min_cost = 10**13
    height = a[0]
    for x in range(2**n):
        cost = 0
        xbit = bin(x)[2:].zfill(n)
        if xbit[0]!="1":
            continue
        xbit_list = [int(xbit[ni]) for ni in range(n)]
        color_num = sum(xbit_list[1:])
        if color_num!=k - 1:
            continue
        for ni in range(1, n):
            if xbit_list[ni]: 
                cost += height + 1 - a[ni]
                height+=1
                #a[ni] = height

        if cost < min_cost:
            min_cost = cost

    print(min_cost)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))

    cost = 0
    min_cost = 10**11
    height = a[0]
    for x in range(2**n):
        xbit = bin(x)[2:].zfill(n)
        if xbit[0]!="1":
            continue
        xbit_list = [int(xbit[ni]) for ni in range(n)]
        color_num = sum(xbit_list[1:])
        if color_num!=k - 1:
            continue
        for ni in range(1, n):
            if xbit_list[ni]: 
                cost += height + 1 - a[ni]
                height+=1
                a[ni] = height

        if cost < min_cost:
            min_cost = cost

    print(min_cost)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))

    cost = 0
    min_cost = 10**11
    height = a[0]
    for x in range(2**n):
        xbit = bin(x)[2:].zfill(n)
        xbit_list = [int(xbit[ni]) for ni in range(n)]
        color_num = sum(xbit_list[1:])
        if color_num < k - 1:
            continue
        for ni in range(1, n):
            if xbit_list[ni]: 
                cost += height + 1 - a[ni]
                height+=1
                a[ni] = height

        if cost < min_cost:
            min_cost = cost

    print(min_cost)

if __name__=="__main__":
    main()

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):
    h=[]
    for j in range(n):
        if (i>>j)&1:
            h.append(j)
    m=a[0]
    cnt=0
    if k<=len(h):
        for j in range(1,n):
            m=max(m,a[j])
            if j in h:
                cnt+=max(0,m-a[j]+1)
                m=max(a[j],m+1)
        ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
ans = float('inf')
if n==1:print(0);exit()
tmp =A[0]
for i in range(1,n):
    if A[i]<=A[i-1]:
        break
    else:
        tmp = A[i]
n-=i
k-=i
A = A[i:]
for bit in range(1<<n):
    cnt = 0
    money = 0
    tmp2 = tmp
    for i in range(n):
        if ((bit>>i)&1):
            cnt += 1
            val = max(tmp2+1-A[i],0)
            money += val
            tmp2 = val+A[i]
    if cnt <k:
        continue
    else:
        ans = min(ans,money)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [_ for _ in range(1,N)]
ans = float("inf")
cand = list(itertools.combinations(L,K-1))
#print(cand)
for C in cand:
  C = list(C)
  C.sort()
  temp = 0
  prev = A[0] #最初のビルは必ず見える。
  #print(C)
  for i in C:
    if A[i] <= prev:
      prev += 1
      temp += prev-A[i]
    else:
      prev = A[i]
  ans = min(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [_ for _ in range(1,N)]
ans = float("inf")
cand = list(itertools.combinations(L,K-1))
#print(cand)
for C in cand:
  temp = 0
  prev = A[0] #最初のビルは必ず見える。
  #print(C)
  for i in C:
    if A[i] <= prev:
      prev += 1
      temp += prev-A[i]
    else:
      prev = A[i]
  ans = min(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

min_cost = float('inf')

for target_buildings in itertools.combinations(buildings[1:], K-1):

    before_building = buildings[0]
    total_cost = 0
    for building in target_buildings:
        cost = max(before_building - building + 1, 0)
        before_building = building + cost
        total_cost += cost

    min_cost = min(min_cost, total_cost)

print(min_cost)

Submission status: Wrong Answer

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

# a[1:]で大きさが上位K-1についてフラグを付ける
# フラグがついているのと前回のものの差分＋１をしていく
# この処理をK-1回行った段階で終了させる

target_list = sorted(set(buildings[1:]), reverse=True)[:K-1]

i = 0 #ターゲットリストの添え字
total_cost = 0
before_building = buildings[0]

for building in buildings[1:]:

    if building in target_list:
        cost = max(before_building - building + 1, 0)
        total_cost += cost
        before_building = building + cost
        # print('building: ', building)
        # print('total_cost: ', total_cost)
        # print('before_building: ', before_building)
        i += 1

    if i == K - 1:
        break

print(total_cost)