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:
import itertools
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))
temp_a = copy.copy(a)
ans = 10**100

#combinationは昇順ででてくる
for pattern in itertools.combinations(range(1,N),K-1):
    temp_cost = 0
    temp_pattern = [0] + list(pattern)
    temp_a = copy.copy(a)
    for i in range(K-1):
        diff = temp_a[temp_pattern[i]] - temp_a[temp_pattern[i+1]]
        if diff >=  0:
            temp_a[temp_pattern[i+1]] += diff + 1
            temp_cost += diff + 1
    
    ans = min(ans, temp_cost)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))
temp_a = copy.copy(a)
ans = 10**15

#combinationは昇順ででてくる
for pattern in itertools.combinations(range(1,N),K-1):
    temp_cost = 0
    temp_pattern = [0] + list(pattern)
    temp_a = copy.copy(a)
    for i in range(K-1):
        diff = temp_a[temp_pattern[i]] - temp_a[temp_pattern[i+1]]
        if diff >=  0:
            temp_a[temp_pattern[i+1]] += diff + 1
            temp_cost += diff + 1
    
    ans = min(ans, temp_cost)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))
temp_a = copy.copy(a)
ans = 10**10

#combinationは昇順ででてくる
for pattern in itertools.combinations(range(1,N),K-1):
    temp_cost = 0
    temp_pattern = [0] + list(pattern)
    temp_a = copy.copy(a)
    for i in range(K-1):
        diff = temp_a[temp_pattern[i]] - temp_a[temp_pattern[i+1]]
        if diff >=  0:
            temp_a[temp_pattern[i+1]] += diff + 1
            temp_cost += diff + 1
    
    ans = min(ans, temp_cost)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))
temp_a = copy.copy(a)
ans = 10**9

#combinationは昇順ででてくる
for pattern in itertools.combinations(range(1,N),K-1):
    temp_cost = 0
    temp_pattern = [0] + list(pattern)
    temp_a = copy.copy(a)
    for i in range(K-1):
        diff = temp_a[temp_pattern[i]] - temp_a[temp_pattern[i+1]]
        if diff >=  0:
            temp_a[temp_pattern[i+1]] += diff + 1
            temp_cost += diff + 1
    
    ans = min(ans, temp_cost)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

N, K = map(int, input().split())
a = list(map(int, input().split()))
temp_a = copy.copy(a)
ans = 10**9

#combinationは昇順ででてくる
for pattern in itertools.combinations(range(1,N),K-1):
    temp_cost = 0
    temp_pattern = [0] + list(pattern)
    temp_a = copy.copy(a)
    for i in range(K-1):
        diff = temp_a[temp_pattern[i]] - temp_a[temp_pattern[i+1]]
        if diff >=  0:
            temp_a[temp_pattern[i+1]] += diff + 1
            temp_cost += diff + 1
    
    if temp_cost == 5:
        print('a')
    ans = min(ans, temp_cost)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         tarou=max(height2[:i])
#         if tarou>=height2[i]:
#             cost+=tarou-height2[i]+1
#             height2[i]=tarou+1
#     print(cost)
#     exit()
for i in range(2 ** N):
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(1,N):
            if i >> j & 1:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]=tarou+1
        # if N==K:
        #     print(cost)
        #     exit()
        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(1,N):
            if i >> j & 1:
                if j!=0:
                    if N==K:
                        if height2[j-1]>=height2[j]:
                                cost+=height2[j-1]-height2[j]+1
                                height2[j]+=height2[j-1]-height2[j]+1
                    else:
                        tarou=max(height2[:j])
                        if height2[j]<=tarou:
                            cost+=tarou-height2[j]+1
                            height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    if N==K:
                        if height2[j-1]>=height2[j]:
                                cost+=height2[j-1]-height2[j]+1
                                height2[j]+=height2[j-1]-height2[j]+1
                    else:
                        tarou=max(height2[:j])
                        if height2[j]<=tarou:
                            cost+=tarou-height2[j]+1
                            height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    if N==K:
                        if height2[j-1]>=height2[j]:
                                cost+=height2[j-1]-height2[j]+1
                                height2[j]+=height2[j-1]-height2[j]+1
                    else:
                        tarou=max(height2[:j])
                        if height2[j]<=tarou:
                            cost+=tarou-height2[j]+1
                            height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    if N==K:
                        if height2[j]<=height2[j-1]:
                            cost+=height2[j-1]-height2[j]+1
                            height2[j]=height2[j-1]+1
                    else:
                        tarou=max(height2[:j])
                        if height2[j]<=tarou:
                            cost+=tarou-height2[j]+1
                            height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    if N==K:
                        if height[j]<=height[j-1]:
                            cost+=height[j-1]-height[j]+1
                            height[j]=height[j-1]+1
                    else:
                        tarou=max(height2[:j])
                        if height2[j]<=tarou:
                            cost+=tarou-height2[j]+1
                            height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]+=tarou-height2[j]+1

        if ans > cost:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]+=tarou-height2[j]+1

                    count+=1
        # zenkai=0
        # for x in range(1,N):
        #     if height2[zenkai] < height2[x]:
        #         count += 1
        #         zenkai=x
        if ans > cost and count >= K:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]+=tarou-height2[j]+1
                        count+=1
        # zenkai=0
        # for x in range(1,N):
        #     if height2[zenkai] < height2[x]:
        #         count += 1
        #         zenkai=x
        if ans > cost and count >= K:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]+=tarou-height2[j]+1
                        count+=1
                    else:count+=1
        # zenkai=0
        # for x in range(1,N):
        #     if height2[zenkai] < height2[x]:
        #         count += 1
        #         zenkai=x
        if ans > cost and count >= K:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
ans = 10 ** 10
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    for j in range(N):
        if i >> j & 1:
            if j!=0:
                tarou=max(height2[:j])
                if height2[j]<=tarou:
                    cost+=tarou-height2[j]+1
                    height2[j]+=tarou-height2[j]+1
                    count+=1
                else:count+=1
    # zenkai=0
    # for x in range(1,N):
    #     if height2[zenkai] < height2[x]:
    #         count += 1
    #         zenkai=x
    if ans > cost and count >= K:
        ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
ans = 10 ** 10
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    for j in range(N):
        if i >> j & 1:
            if j!=0:
                tarou=max(height2[:j])
                if height2[j]<=tarou:
                    cost+=tarou-height2[j]+1
                    height2[j]+=tarou-height2[j]+1
            # for k in range(j):
            #     if height2[k] >= height2[j]:
            #         cost += height2[k] - height2[j] + 1
            #         height2[j] += height2[k] - height2[j] + 1
    zenkai=0
    for x in range(1,N):
        if height2[zenkai] < height2[x]:
            count += 1
            zenkai=x
    if ans > cost and count >= K:
        ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

from itertools import combinations

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

minc=10**12
for i in combinations(a,K):
    cost=0
    i=list(i)
    for j in range(1,K):
        costt=max(i[j-1]-i[j]+1,0)
        i[j]+=costt
        cost+=costt
    minc=min(cost,minc)

print(minc)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
ans = 10 ** 10
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    for j in range(N):
        if i >> j & 1:
            count = 0
            for k in range(j):
                if height2[k] >= height2[j]:
                    cost += height2[k] - height2[j] + 1
                    height2[j] += height2[k] - height2[j] + 1
    zenkai=0
    for x in range(1,N):
        if height2[zenkai] < height2[x]:
            count += 1
            zenkai=x
    if ans > cost and count >= K-1:
        ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

from itertools import combinations

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

minc=10**12
for i in combinations(a[1:],K-1):
    cost=0
    l=[a[0]]+list(i)
    for j in range(1,K):
        costt=max(l[j-1]-l[j]+1,0)
        l[j]+=costt
        cost+=costt
    minc=min(cost,minc)

print(minc)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

from itertools import combinations

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

minc=10**11
for i in combinations(a[1:],K-1):
    cost=0
    l=[a[0]]+list(i)
    for j in range(1,K):
        costt=max(l[j-1]-l[j]+1,0)
        l[j]+=costt
        cost+=costt
    minc=min(cost,minc)

print(minc)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
ans = 10 ** 10
for i in range(2 ** N):
    count=0
    cost = 0
    height2 = height.copy()
    for j in range(N):
        if i >> j & 1:
            count = 0
            for k in range(j):
                if height2[k] >= height2[j]:
                    cost += height2[k] - height2[j] + 1
                    height2[j] += height2[k] - height2[j] + 1
    zenkai=0
    for x in range(1,N):
        if height2[zenkai] < height2[x]:
            count += 1
            zenkai=x
    if ans > cost and count >= K-1:
        ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

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

upend=N-1
for i in range(N-1):
    if a[i+1]-a[i]<=0:
        upend=i
        break
    
b=sorted(a[upend+1:],reverse=True)
cost=0
maxi=[]
for i in range(K-1-upend):
    t=a[upend+1:].index(b[i])+upend+1
    while(1):
        if not t in maxi:
            maxi.append(t)
            break
        else:
            t=a[t+1:].index(b[i])+t+1
maxi.sort()
maxi=[i for i in range(upend+1)]+maxi
for i in range(1,K):
    j=maxi[i]
    jh1=maxi[i-1]
    costt=max(a[jh1]-a[j]+1,0)
    a[j]+=costt
    cost+=costt

print(cost)

Submission status: Wrong Answer

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

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

min_cost = float("inf")
for i in range(2**n):
    if bin(i).count("1") != k or not i & 1:
        continue

    min_h = a[0]
    cost = 0
    for j in range(1, n):
        if i >> j & 1:
            cost += max(0, min_h-a[j]+1)
            min_h = max(min_h+1, a[j])

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
ans = 10 ** 10
count = 0
for i in range(2 ** N):
    cost = 0
    height2 = height.copy()
    for j in range(N):
        if i >> j & 1:
            count = 0
            for k in range(j):
                if height2[k] >= height2[j]:
                    cost += height2[k] - height2[j] + 1
                    height2[j] += height2[k] - height2[j] + 1
    for x in range(1, N):
        if height2[0] < height2[x]:
            count += 1

    if ans > cost and count >= K - 1:
        ans = cost

print(ans)

Submission status: Wrong Answer

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

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

min_cost = float("inf")
for i in range(2**n):
    if bin(i).count("1") != k or not i & 1:
        continue

    min_h = a[0]
    cost = 0
    for j in range(1, n):
        if i >> j & 1:
            min_h += 1
            cost += max(0, min_h-a[j])

    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

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

upend=N-1
for i in range(N-1):
    if a[i+1]-a[i]<=0:
        upend=i
        break
    
b=sorted(a[upend+1:],reverse=True)
cost=0
maxi=[]
for i in range(K-1-upend):
    t=a.index(b[i])
    while(1):
        if not t in maxi:
            maxi.append(t)
            break
        else:
            t=a[t+1:].index(b[i])+t+1
maxi.sort()
maxi=[i for i in range(upend+1)]+maxi
for i in range(1,K):
    j=maxi[i]
    jh1=maxi[i-1]
    costt=max(a[jh1]-a[j]+1,0)
    a[j]+=costt
    cost+=costt

print(cost)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
height=list(map(int,input().split()))
ans=10**10
count=0
for i in range(2**N):
    cost=0
    height2=height.copy()
    for j in range(N):
        if i>>j&1:
            count=0
            for k in range(j):
                if height2[k]>=height2[j]:
                    cost+=height2[k]-height2[j]+1
                    height2[j]+=height2[k]-height2[j]+1
            for x in range(1,N):
                if height2[0]<height2[x]:
                    count+=1

            if ans>cost and count>=K-1:
                ans=cost


print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
height=list(map(int,input().split()))
ans=10**10
count=0
for x in range(1, N):
    if height[0] < height[x]:
        count += 1
if count>=K-1:
    print(0)
    exit()
for i in range(2**N):
    cost=0
    height2=height.copy()
    for j in range(N):
        if i>>j&1:
            count=0
            for k in range(j):
                if height2[k]>=height2[j]:
                    cost+=height2[k]-height2[j]+1
                    height2[j]+=height2[k]-height2[j]+1
            for x in range(1,N):
                if height2[0]<height2[x]:
                    count+=1

            if ans>cost and count==K-1:
                ans=cost


print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
height=list(map(int,input().split()))
ans=10**10
count=0
for x in range(1, N):
    if height[0] < height[x]:
        count += 1
if count>=K:
    print(0)
    exit()
for i in range(2**N):
    cost=0
    height2=height.copy()
    for j in range(N):
        if i>>j&1:
            count=0
            for k in range(j):
                if height2[k]>=height2[j]:
                    cost+=height2[k]-height2[j]+1
                    height2[j]+=height2[k]-height2[j]+1
            for x in range(1,N):
                if height2[0]<height2[x]:
                    count+=1

            if ans>cost and count==K-1:
                ans=cost


print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
height=list(map(int,input().split()))
ans=10**10
for i in range(2**N):
    cost=0
    height2=height.copy()
    for j in range(N):
        if i>>j&1:
            count=0
            for k in range(j):
                if height2[k]>=height2[j]:
                    cost+=height2[k]-height2[j]+1
                    height2[j]+=height2[k]-height2[j]+1
            for x in range(1,N):
                if height2[0]<height2[x]:
                    count+=1

            if ans>cost and count==K-1:
                ans=cost


print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

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

upend=N-1
for i in range(N-1):
    if a[i+1]-a[i]<=0:
        upend=i
        break
    
b=sorted(a[upend+1:],reverse=True)
    
cost=0
maxi=[]
for i in range(K-1-upend):
    maxi.append(a.index(b[i]))
maxi.sort()
maxi=[i for i in range(upend+1)]+maxi
for i in range(1,K):
    j=maxi[i]
    jh1=maxi[i-1]
    costt=max(a[jh1]-a[j]+1,0)
    a[j]+=costt
    cost+=costt

print(cost)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

N,K=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(a[1:],reverse=True)
upend=0
for i in range(N-1):
    if a[i+1]-a[i]<=0:
        upend=i
        break
    
cost=0
maxi=[]
for i in range(K-1-upend):
    maxi.append(a.index(b[i]))
maxi.sort()
maxi=[i for i in range(upend+1)]+maxi
for i in range(1,K):
    j=maxi[i]
    jh1=maxi[i-1]
    costt=max(a[jh1]-a[j]+1,0)
    a[j]+=costt
    cost+=costt

print(cost)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

N,K=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(a[1:],reverse=True)

cost=0
maxi=[]
for i in range(K-1):
    maxi.append(a.index(b[i]))
maxi.sort()
maxi=[0]+maxi
for i in range(1,K):
    j=maxi[i]
    jh1=maxi[i-1]
    costt=max(a[jh1]-a[j]+1,0)
    a[j]+=costt
    cost+=costt

print(cost)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost



result=10**100
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")>=k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost

if k==1:
    print(0)
    exit()



result="INF"
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")>=k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        if result=="INF":
            result=cost
        else:
            result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost

if k==1:
    print(0)
    exit()



result="INF"
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")==k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        if result=="INF":
            result=cost
        else:
            result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost

if k==1:
    print(0)
    exit()



result=10**14
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")==k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost

if k==1:
    print(0)
    exit()



result=10**12
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")==k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost

if k==1:
    print(0)
    exit()



result=10**9+10
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")==k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost


result=10**9
for i in range(2**(n-1)):
    #print(bin(i))
    if str(bin(i)).count("1")==k-1:
        #print(">>>>>>",bin(i))
        bk=a[0]
        cost=0
        for j in range(n-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk+=1
                    cost+=(bk-a[j+1])
                #print(bk,a[j+1],bk-a[j+1])
        result=min(cost,result)
print(result)

Submission status: Wrong Answer

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

#左からi個、k種類選んだ場合
#dp[i][k]=cost


result=10**9
for i in range(2**(k-1)):
    if str(bin(i)).count("1")==k-1:
        #print(bin(i))
        bk=a[0]
        cost=0
        for j in range(k-1):
            if i>>j&1:
                if bk<a[j+1]:
                    bk=a[j+1]
                else:
                    bk=bk+1
                    cost+=(bk-a[j+1])
            #print(bk,bk-a[j+1])
        result=min(cost,result)
print(result)

Submission status: Wrong Answer

Solution code:
import itertools

n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
ans=[]

for li in itertools.product([0,1], repeat=n-1):
  if li.count(1) < k-1:
    continue
  cost,height=0,A[0]
  for i in range(1,n):
    if li[i-1]:
      if height < A[i]:
        height=A[i]
        continue
      cost += (height-A[i]+1)
      height += 1 
  ans.append(cost)
  
print(min(ans))