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:
# Square869120Contest #4 B - Buildings are Colorful!　

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))


def dfs(height=a[0], cost=0, target=K, can_see=1, i=1):
    # もはや目的を達成しえぬ場合
    if can_see + N - i < target:
        return float('inf')
    if i >= len(a):
        return float('inf')
    if can_see >= target:
        return float('inf')

    # 建物 i を選ばない場合
    if height <= a[i]:
        cost1 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    else:
        cost1 = dfs(height=a[i], cost=cost, target=K, can_see=can_see+1, i=i+1)
    # print('cost1', cost1, 'can_see', can_see, 'i', i)

    # 建物　i を選んだ場合
    cost += max(0, height + 1 - a[i])
    can_see += 1
    height = max(a[i], height + 1)
    if can_see == target:
        # print('height', height)
        # print('can_see', can_see)
        # print('cost', cost)
        return cost
    cost2 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    # print('cost2', cost2)

    return min(cost1, cost2)


print(dfs(a[0], cost=0, target=K, can_see=1, i=1))

Submission status: Wrong Answer

Solution code:
# Square869120Contest #4 B - Buildings are Colorful!　

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))


def dfs(height=a[0], cost=0, target=K, can_see=1, i=1):
    # もはや目的を達成しえぬ場合
    if can_see + N - i < target:
        return float('inf')
    if i >= len(a):
        return float('inf')
    if can_see >= target:
        return float('inf')

    # 建物 i を選ばない場合
    if height <= a[i]:
        cost1 = dfs(height=max(height, a[i]), cost=cost, target=K, can_see=can_see, i=i+1)
    else:
        cost1 = float('inf')
    # print('cost1', cost1, 'can_see', can_see, 'i', i)

    # 建物　i を選んだ場合
    cost += max(0, height + 1 - a[i])
    can_see += 1
    height = max(a[i], height + 1)
    if can_see == target:
        # print('height', height)
        # print('can_see', can_see)
        # print('cost', cost)
        return cost
    cost2 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    # print('cost2', cost2)

    return min(cost1, cost2)


print(dfs(a[0], cost=0, target=K, can_see=1, i=1))

Submission status: Wrong Answer

Solution code:
# Square869120Contest #4 B - Buildings are Colorful!　

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))


def dfs(height=a[0], cost=0, target=K, can_see=1, i=1):
    # もはや目的を達成しえぬ場合
    if can_see + N - i < target:
        return float('inf')
    if i >= len(a):
        return float('inf')
    if can_see >= target:
        return float('inf')

    # 建物 i を選ばない場合
    cost1 = dfs(height=max(height, a[i]), cost=cost, target=K, can_see=can_see, i=i+1)
    # print('cost1', cost1, 'can_see', can_see, 'i', i)

    # 建物　i を選んだ場合
    cost += max(0, height + 1 - a[i])
    can_see += 1
    height = max(a[i], height + 1)
    if can_see == target:
        # print('height', height)
        # print('can_see', can_see)
        # print('cost', cost)
        return cost
    cost2 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    # print('cost2', cost2)

    return min(cost1, cost2)


print(dfs(a[0], cost=0, target=K, can_see=1, i=1))

Submission status: Wrong Answer

Solution code:
# Square869120Contest #4 B - Buildings are Colorful!　

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))


def dfs(height=a[0], cost=0, target=K, can_see=1, i=1):
    # もはや目的を達成しえぬ場合
    if can_see + N - i < target:
        return float('inf')
    if i >= len(a):
        return float('inf')
    if can_see >= target:
        return float('inf')

    # 建物 i を選ばない場合
    cost1 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    # print('cost1', cost1, 'can_see', can_see, 'i', i)

    # 建物　i を選んだ場合
    cost += max(0, height + 1 - a[i])
    can_see += 1
    height = max(a[i], height + 1)
    if can_see == target:
        # print('height', height)
        # print('can_see', can_see)
        # print('cost', cost)
        return cost
    cost2 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    # print('cost2', cost2)

    return min(cost1, cost2)


print(dfs(a[0], cost=0, target=K, can_see=1, i=1))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
import collections
picked = []
inf = 10**20
ans =int(inf)
for i in range(1 << n-1):
    tmp = 0
    picked.clear()
    minheight = int(a[0])
    for j in range(n-1):
        if (i >> j) & 1 == 1:
            picked.append(j+1)
    if len(picked) == k-1:
        for t in picked:
            tmp = 0
            if max(a[1:]) <= a[t]: 
                for j in picked:
                    if a[j] > minheight:
                        minheight = int(a[j])
                        tmp += 0
                    else:
                        minheight += 1
                        tmp += int(minheight - a[j])
                ans = min(ans,tmp)
            else: 
                tmp = inf
    else:
        picked.clear()
        tmp = inf

if ans == inf:
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
import collections
picked = []
inf = 10**20
ans =int(inf)
for i in range(1 << n-1):
    tmp = 0
    picked.clear()
    minheight = int(a[0])
    for j in range(n-1):
        if (i >> j) & 1 == 1:
            picked.append(j+1)
    # print(picked)
    # print(max(a[1:]))
    if len(picked) == k-1:
        for t in picked:
            # print(t,'t')
            if max(a[1:]) <= a[t]: 
                # print(picked)
                for j in picked:
                    if (picked[0] > 2):
                        # print(max(a[1:picked[0]]))
                        if max(a[1:picked[0]])>a[0]: 
                            tmp = inf
                            # print('f')
                            break
                    if a[j] > minheight:
                        # print('a')
                        minheight = int(a[j])
                        tmp += 0
                        # print('a')
                    else:
                        # print('b')
                        # print('b')
                        minheight += 1
                        tmp += int(minheight - a[j])
                ans = min(ans,tmp)
                # print(tmp)
                # print(picked)
            else: 
                # print('c')
                tmp = inf
    else:
        picked.clear()
        # print('s')
        tmp = inf

if ans == inf:
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
import collections
picked = []
inf = 10**20
ans =int(inf)
for i in range(1 << n-1):
    tmp = 0
    picked.clear()
    minheight = int(a[0])
    for j in range(n-1):
        if (i >> j) & 1 == 1:
            picked.append(j+1)
    # print(picked)
    # print(max(a[1:]))
    if len(picked) == k-1:
        for t in picked:
            # print(t,'t')
            if max(a[1:]) <= a[t]: 
                # print(picked)
                for j in picked:
                    if (picked[0] > 2):
                        # print(max(a[1:picked[0]]))
                        if max(a[1:picked[0]])>a[0]: 
                            tmp = inf
                            # print('f')
                            break
                    if a[j] > minheight:
                        # print('a')
                        minheight = int(a[j])
                        tmp += 0
                        # print('a')
                    else:
                        # print('b')
                        # print('b')
                        minheight += 1
                        tmp += int(minheight - a[j])
                ans = min(ans,tmp)
                # print(tmp)
                # print(picked)
            else: 
                # print('c')
                tmp = inf
    else:
        picked.clear()
        # print('s')
        tmp = inf

   

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
import collections
picked = []
ans = 10000000000000000000
for i in range(1 << n-1):
    tmp = 0
    picked.clear()
    minheight = a[0]
    for j in range(n-1):
        if (i >> j) & 1 == 1:
            picked.append(j+1)
    # print(picked)
    if len(picked) == k-1:
        # print(picked)
        for j in picked:
            if a[j] > minheight:
                minheight = a[j]
                tmp += 0
                # print('a')
            else:
                # print('b')
                minheight += 1
                tmp += minheight - a[j]
        ans = min(ans,tmp)
        # print(tmp)
if ans == 10000000000000000000:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
import collections
picked = []
ans = 100000000000000
for i in range(1 << n-1):
    tmp = 0
    picked.clear()
    minheight = a[0]
    for j in range(n-1):
        if (i >> j) & 1 == 1:
            picked.append(j+1)
    # print(picked)
    if len(picked) == k-1:
        # print(picked)
        for j in picked:
            if a[j] > minheight:
                minheight = a[j]
                tmp += 0
                # print('a')
            else:
                # print('b')
                minheight += 1
                tmp += minheight - a[j]
        ans = min(ans,tmp)
        # print(tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
 
n,k = LI()
a = LI()

min_val = 10000000000
for i in range(1 << (n-1)):
    count = 0
    val = 0
    pre = a[0]
    for j in range(n-1):
        if(i & (1 << j)):
            count += 1
            if(pre >= a[j+1]):
                val += pre - a[j+1] + 1
                pre = pre + 1
        if(pre < a[j+1]):
            pre = a[j+1]

    if(count >= k-1):
        if(min_val > val):
            min_val = val

print(min_val)

Submission status: Wrong Answer

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
 
n,k = LI()
a = LI()

min_val = 1000000000
for i in range(1 << (n-1)):
    count = 0
    val = 0
    pre = a[0]
    for j in range(n-1):
        if(i & (1 << j)):
            count += 1
            if(pre >= a[j+1]):
                val += pre - a[j+1] + 1
                pre = pre + 1
            else:
                pre = a[j+1]

    if(count >= k-1):
        if(min_val > val):
            min_val = val

print(min_val)

Submission status: Wrong Answer

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
 
n,k = LI()
a = LI()

min_val = 1000000000
for i in range(1 << (n-1)):
    count = 0
    val = 0
    pre = a[0]
    for j in range(n-1):
        if(i & (1 << j)):
            count += 1
            if(pre >= a[j+1]):
                val += pre - a[j+1] + 1
                pre = pre + 1

    if(count >= k-1):
        if(min_val > val):
            min_val = val

print(min_val)

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):
    bit=[(i>>j)&1 for j in range(n)]
    if sum(bit)==k and bit[0]:
        cost,kijun=0,a[0]
        for j in range(n):
            if not bit[j]:
                kijun=max(kijun,a[j])
                continue
            if a[j]>=kijun+1:
                kijun=a[j]
                continue
            cost+=kijun+1-a[j]
            kijun+=1
        ans=min(ans,cost)
print(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):
    bit=[(i>>j)&1 for j in range(n)]
    if sum(bit)==k and bit[0]:
        l=[]
        tmp=0
        for j in range(n):
            if bit[j]: l.append(a[j])
        for k in range(len(l)-1):
            p=l[k]+1-l[k+1]
            if p>0:
                tmp+=p
                l[k+1]+=p
        ans=min(ans,tmp)
print(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):
    if bin(i).count("1")==k and (i>>0)&1:
        l=[]
        tmp=0
        for j in range(n):
            if (i>>j)&1: l.append(a[j])
        for k in range(len(l)-1):
            p=+l[k]+1-l[k+1]
            if p>0:
                tmp+=p
                l[k+1]+=p
        ans=min(ans,tmp)
print(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):
    if bin(i).count("1")>k and (i>>0)&1:
        l=[]
        tmp=0
        for j in range(n):
            if (i>>j)&1: l.append(a[j])
        for k in range(len(l)-1):
            p=+l[k]+1-l[k+1]
            if p>0:
                tmp+=p
                l[k+1]+=p
        ans=min(ans,tmp)
print(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):
    if bin(i).count("1")>=k and (i>>0)&1:
        l=[]
        tmp=0
        for j in range(n):
            if (i>>j)&1: l.append(a[j])
        for k in range(len(l)-1):
            p=+l[k]+1-l[k+1]
            if p>0:
                tmp+=p
                l[k+1]+=p
        ans=min(ans,tmp)
print(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):
    if bin(i).count("1")>k and (i>>0)&1:
        print(bin(i).count("1"))
        l=[]
        tmp=0
        for j in range(n):
            if (i>>j)&1: l.append(a[j])
        print(l)
        for k in range(len(l)-1):
            p=+l[k]+1-l[k+1]
            if p>0:
                tmp+=p
                l[k+1]+=p
        ans=min(ans,tmp)
print(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):
    if bin(i).count("1")>=k and (i>>0)&1:
        print(bin(i).count("1"))
        l=[]
        tmp=0
        for j in range(n):
            if (i>>j)&1: l.append(a[j])
        print(l)
        for k in range(len(l)-1):
            p=+l[k]+1-l[k+1]
            if p>0:
                tmp+=p
                l[k+1]+=p
        ans=min(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

amin=10**9*20
for i in itertools.product([0,1], repeat=n):
    if sum(i)==k:
        hs=a[0]
        acnt=0
        for j in range(n):
#            print(j,hs,a[j],acnt)
            if i[j]==1 and j>0:
                if hs>=a[j]:
                    acnt+=hs-a[j]+1
                    hs=hs+1
            elif hs<a[j]:
                hs=a[j]
        amin=min(amin,acnt)
        
print(amin)

Submission status: Wrong Answer

Solution code:
import itertools

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

amin=10**9*20
for i in itertools.product([0,1], repeat=n):
    if sum(i)==k:
        hs=a[0]
        acnt=0
        for j in range(n-1):
#            print(j,hs,a[j],acnt)
            if i[j+1]==1:
                if hs>=a[j+1]:
                    acnt+=hs-a[j+1]+1
                    hs=hs+1
            elif hs<a[j+1]:
                hs=a[j+1]
        amin=min(amin,acnt)
        
print(amin)

Submission status: Wrong Answer

Solution code:
import itertools

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

amin=10**9*20
for i in itertools.product([0,1], repeat=n):
    if sum(i)==k:
        hs=a[0]
        acnt=0
        for j in range(n-1):
#            print(j,hs,a[j],acnt)
            if i[j]==1:
                if hs>=a[j+1]:
                    acnt+=hs-a[j+1]+1
                    hs=hs+1
            elif hs<a[j+1]:
                hs=a[j+1]
        amin=min(amin,acnt)
        
print(amin)

Submission status: Wrong Answer

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

ans = 10**18
for i in range(1<<N):
    cnt = 0
    cost = 0
    mini = 0
    for j in range(N):
        J = As[j]
        if (i>>j)&1:
            cnt += 1
            if mini>J:
                cost += mini+1-J
                mini += 1
        mini = max(mini, J)
        if cnt>=K:
            ans = min(ans, cost)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import copy
    n, k  = map(int, input().split())
    takasa_lis = list(map(int, input().split()))
   
    ans = 10**200
    
    for i in range(0, 2 ** n, 2):
        test_lis = copy.deepcopy(takasa_lis)
        print(test_lis)
        cnt = 0
        building_cost = 0
        for j in range(1, n):
            if ((i>>j) & 1):
                cnt += 1
                building_cost += max(max(test_lis[0:j]) - test_lis[j] + 1, 0)
                test_lis[j] = max(max(test_lis[0:j])+1, test_lis[j])
            else:
                if test_lis[j] > max(test_lis[0:j]):
                    cnt += 1
        print(test_lis,cnt, building_cost)
        if cnt >= k-1 and ans > building_cost:
            ans = building_cost
    
    print(ans)


    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import copy
    n, k  = map(int, input().split())
    takasa_lis = list(map(int, input().split()))
   
    ans = 10 ** 10
    
    for i in range(0, 2 ** n, 2):
        test_lis = copy.deepcopy(takasa_lis)
        cnt = 0
        building_cost = 0
        for j in range(1, n):
            if ((i>>j) & 1):
                cnt += 1
                building_cost += max(max(test_lis[0:j]) - test_lis[j] + 1, 0)
                test_lis[j] = max(max(test_lis[0:j])+1, test_lis[j])
            else:
                if test_lis[j] > max(test_lis[0:j]):
                    cnt += 1
        if cnt >= k-1 and ans > building_cost:
            ans = building_cost
    
    print(ans)


    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k  = map(int, input().split())
    takasa_lis = list(map(int, input().split()))
   
    ans = 10 ** 10
    
    for i in range(0, 2 ** n, 2):
        cnt = 0
        building_cost = 0
        highest = takasa_lis[0]
        for j in range(1, n):
            if ((i>>j) & 1):
                cnt += 1
                building_cost += max(highest + 1 - takasa_lis[j], 0)
                highest += 1
                #print(i, highest, takasa_lis[j], building_cost)
            else:
                if takasa_lis[j] > highest:
                    cnt += 1
                    highest = takasa_lis[j]

        if cnt >= k-1 and ans > building_cost:
            ans = building_cost
    
    print(ans)


    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k  = map(int, input().split())
    takasa_lis = list(map(int, input().split()))
   
    ans = 10 ** 10
    
    for i in range(0, 2 ** n, 2):
        cnt = 0
        building_cost = 0
        highest = takasa_lis[0]
        for j in range(1, n):
            if ((i>>j) & 1):
                cnt += 1
                building_cost += max(highest + 1 - takasa_lis[j], 0)
                highest += 1
                #print(i, highest, takasa_lis[j], building_cost)

        if cnt >= k-1 and ans > building_cost:
            ans = building_cost
    
    print(ans)


    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# B - Buildings are Colorful!
# 全探索、BIT全探索
N, K = map(int, input().split())
A = list(int(x) for x in input().split())

def solve(idx):
    ret = 0
    tmpmax = A[idx[0]]
    for i in range(1, len(idx)):
        if A[idx[i]] <= tmpmax:
            ret += tmpmax - A[idx[i]] + 1
            tmpmax += 1
        else:
            tmpmax = A[idx[i]]
    return ret

ans = 10**18
for i in range(1<<N):
    idx = []
    for j in range(N):
        if i>>j&1:
            idx.append(j)
    if len(idx)==K and idx[0]==0:
        ans = min(ans, solve(idx))

print(ans)

Submission status: Wrong Answer

Solution code:
# B - Buildings are Colorful!
# 全探索、BIT全探索
N, K = map(int, input().split())
A = list(int(x) for x in input().split())

def solve(idx):
    ret = 0
    tmpmax = A[idx[0]]
    for i in range(1, len(idx)):
        if A[idx[i]] <= tmpmax:
            ret += tmpmax - A[idx[i]] + 1
            tmpmax += 1
        else:
            tmpmax = A[idx[i]]
    return ret

ans = 10**18
for i in range(1<<N):
    idx = [0]
    for j in range(1, N):
        if i>>j&1:
            idx.append(j)
    if len(idx)==K:
        ans = min(ans, solve(idx))

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(2 ** n):
    res = 0
    cnt = 0
    prev_build = buildings[0]
    for j in range(n):
        if (i >> j) & 1 == 1:
            cnt += 1
            if prev_build > buildings[j]:
                res += (prev_build + 1) - buildings[j]
                prev_build = prev_build + 1
            else:
                prev_build = buildings[j]
        else:
            if prev_build < buildings[j]:
                prev_build = buildings[j]

    if cnt == k:
        ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=tuple(map(int,input().split()))
m=pow(10,12)
l=len(a)-1
for i in range((n-1)**2):
    cnt=1
    h=a[0]
    num=0
    for j in range(l):
        if (i>>j)&1:
            num+=max(0,h-a[j+1]+1)
            if a[j+1]<=h:
                h+=1
            else:
                h=a[j+1]
            cnt+=1
        else:
            if a[j+1]>h:
                cnt+=1
                h=a[j+1]
    if cnt>=k:
        m=min(m,num)
print(m)

Submission status: Wrong Answer

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

candidate = [[0], [1]]
while True:
    temp = []
    for c in candidate:
        temp.append(c + [0])
        temp.append(c + [1])
    candidate = temp
    if len(temp[0]) == N:
        break

ans = float('inf')
for c in candidate:
    if sum(c) != K:
        continue
    high = 0
    temp = 0
    for i, value in enumerate(c):
        high = max(high, a[i])
        if i == 0:
            continue
        if value == 1 and a[i] <= high:
            high += 1
            temp += high - a[i]
    ans = min(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
def get_building(i, N):
    ans = [0]*N
    ans[0] = 1
    for ind in range(1, N):
        if i % 2 == 0:
            ans[ind] = 1
        i //= 2
    return ans

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

ans = -1
for i in range(2**(N-1)):
    see_building = get_building(i, N)

    counter = 1
    now_height = a[0]
    cost = 0
    for j in range(1, N):
        if see_building[j] == 1:
            counter += 1
            if a[j] <= now_height:
                cost += (now_height - a[j]) + 1
                now_height += 1
            else:
                now_height = a[j]
    
    if counter >= K:
        if ans == -1:
            ans = cost
        else:
            ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
def get_building(i, N):
    ans = [0]*N
    ans[0] = 1
    for ind in range(1, N):
        if i % 2 == 0:
            ans[ind] = 1
        i //= 2
    return ans

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

ans = -1
for i in range(2**(N-1)):
    see_building = get_building(i, N)

    counter = 0
    now_height = a[0]
    cost = 0
    for j in range(N):
        if see_building[j] == 1:
            counter += 1
            if a[j] <= now_height:
                cost += (now_height - a[j])
                now_height += 1
            else:
                now_height = a[j]
    
    if counter >= K:
        if ans == -1:
            ans = cost
        else:
            ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
def get_building(i, N):
    ans = [0]*N
    ans[0] = 1
    for ind in range(1, N):
        if i % 2 == 0:
            ans[ind] = 1
        i //= 2
    return ans

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

ans = 10**11
for i in range(2**(N-1)):
    see_building = get_building(i, N)

    counter = 0
    now_height = a[0]
    cost = 0
    for j in range(N):
        if see_building[j] == 1:
            counter += 1
            if a[j] <= now_height:
                cost += (now_height - a[j])
                now_height += 1
            else:
                now_height = a[j]
    
    if counter >= K:
        ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product

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

ans = 10 ** 20
for bit in product([0, 1], repeat=n):
    if sum(bit) < k or bit[0] == 0:
        continue
    cnt = 0
    tmp = a[0]
    for i, v in enumerate(bit):
        if v == 1:
            if i != 0:
                cnt += max(tmp + 1 - a[i], 0)
                tmp = max(tmp + 1, a[i])
    ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product

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

ans = 10 ** 20
for bit in product([0, 1], repeat=n):
    if sum(bit) != k:
        continue
    cnt = 0
    tmp = a[0]
    for i, v in enumerate(bit):
        if v == 1:
            if a[i] == tmp:
                continue
            cnt += max(tmp + 1 - a[i], 0)
            tmp = max(tmp + 1, a[i])
    ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product

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

ans = 10 ** 20
for bit in product([0, 1], repeat=n):
    if sum(bit) != k or bit[0] == 0:
        continue
    cnt = 0
    tmp = a[0]
    for i, v in enumerate(bit):
        if v == 1:
            if i != 0:
                cnt += max(tmp + 1 - a[i], 0)
                tmp = max(tmp + 1, a[i])
    ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1>bit.count(1):
        continue
    NO_bit1 = [k for k in range(N-1) if bit[k]==1]
    temp,kijun = 0,a[0]
    for l,x in enumerate(NO_bit1):
        if a[x+1]>=kijun+1:
            kijun = a[x+1]
            continue
        temp += (kijun+1)-a[x+1]
        kijun += 1
    ans = min(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1!=bit.count(1):
        continue
    NO_bit1 = [k for k in range(N-1) if bit[k]==1]
    temp,kijun = 0,a[0]
    for l,x in enumerate(NO_bit1):
        if a[x+1]>=kijun+1:
            kijun = a[x+1]
            continue
        temp += (kijun+1)-a[x+1]
        kijun += 1
    ans = min(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1!=bit.count(1):
        continue
    NO_bit1 = [k for k in range(N-1) if bit[k]==1]
    temp = 0
    for l,x in enumerate(NO_bit1,1):
        if a[x+1]>=a[0]+l:
            continue
        temp += (a[0]+l)-a[x+1]
    ans = min(ans,temp)
print(ans)

Submission status: Wrong Answer

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

def f(p):
    ret = 0
    ma = 0
    for i, a in enumerate(A):
        if p[i] == 1:
            if ma == 0:
                pass
            else:
                if ma >= a:
                    ret += ma - a + 1
                    ma += 1
        ma = max(ma, a)
    return ret

ans = 10 ** 10
for p in product(range(2), repeat=N):
    if sum(p) < K: continue
    #print(p)
    ans = min(ans, f(p))

print(ans)

Submission status: Wrong Answer

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

# 小課題1
ans = 0
for i in range(1,N):
    if A[i-1] >= A[i]:
        ans += A[i-1] - A[i] + 1
        A[i] = A[i-1] + 1

print(ans)

Submission status: Wrong Answer

Solution code:
cin = open(0).read().strip().split('\n')
n, k = map(int, cin[0].split(' '))
a = list(map(int, cin[1].split(' ')))

ret = 0
for idx in range(2**(n-1)):
    cnt = 1
    tmp = 0
    left = a[0]
    for i in range(n-1):
        if (idx>>i) & 1:
            tmp += left+1 - a[i+1] if (left+1) > a[i+1] else 0
            left = max(left+1, a[i+1])
            cnt += 1
        else:
            cnt += 1 if left < a[i+1] else 0
            left = max(a[i+1], left)
    else:
        if cnt >= k:
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)

print(ret)