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:
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 = 0
    tmp = 0
    left = 0
    for i in range(n-1):
        left = max(a[i], left)
        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

        if cnt == k-1:
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)

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 = 0
    tmp = 0
    left = 0
    for i in range(n):
        left = max(a[i], left)
        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

        if cnt == k-1:
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)

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 = 0
    tmp = 0
    left = a[0]
    for i in range(n):
        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

        if cnt == k-1:
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)

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
target = [a[0]+i for i in range(1, k)]
for idx in range(2**(n-1)):
    cnt = 0
    tmp = 0
    left = a[0]
    for i in range(n):
        if (idx>>i) & 1:
            tmp += left+1 - a[i+1] if (left+1) > a[i+1] else 0
            left = target[cnt] if target[cnt] > a[i+1] else a[i+1]
            cnt += 1

        if cnt == len(target):
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)

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
target = [a[0]+i for i in range(1, k)]
for idx in range(2**(n-1)):
    cnt = 0
    tmp = 0
    left = 0
    for i in range(n):
        if (idx>>i) & 1:
            if i == 0:
                tmp += target[cnt] - a[i+1] if target[cnt] > a[i+1] else 0
            else:
                tmp += left+1 - a[i+1] if left+1 > a[i+1] else 0
            cnt += 1
            left = a[i+1]

        if cnt == len(target):
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)

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
target = [a[0]+i for i in range(1, k)]
for idx in range(2**(n-1)):
    cnt = 0
    tmp = 0
    for i in range(n):
        if (idx>>i) & 1:
            tmp += target[cnt] - a[i+1] if target[cnt] > a[i+1] else 0
            cnt += 1

        if cnt == len(target):
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(str(ret) + '\n')

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
target = [a[0]+i for i in range(1, k)]
for idx in range(2**(n-1)):
    cnt = 0
    tmp = 0
    for i in range(n):
        if (idx>>i) & 1:
            tmp += target[cnt] - a[i+1] if target[cnt] > a[i+1] else 0
            cnt += 1

        if cnt == len(target):
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)
print()

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
target = [a[0]+i for i in range(1, k)]
for idx in range(2**(n-1)):
    cnt = 0
    tmp = 0
    for i in range(n):
        if (idx>>i) & 1:
            tmp += target[cnt] - a[i+1] if target[cnt] > a[i+1] else 0
            cnt += 1

        if cnt == len(target):
            if ret == 0:
                ret = tmp
            else:
                ret = min(ret, tmp)
            break

print(ret)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 20
for i in range(2 ** n):
    total = 0
    data = []
    for j in range(n):
        if i >> j & 1:
            data.append(a[j])
    if a[0] in data and len(data) >= k:
        #print(data)
        for j in range(1, k):
            if data[j - 1] >= data[j]:
                total += data[j - 1] + 1 - data[j]
                data[j] = data[j - 1] + 1
        
        #print(total)
    
        ans = min(ans, total)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 20
for i in range(2 ** n):
    total = 0
    data = []
    for j in range(n):
        if i >> j & 1:
            data.append(a[j])
    if a[0] in data and len(data) == k:
        #print(data)
        for j in range(1, k):
            if data[j - 1] >= data[j]:
                total += data[j - 1] + 1 - data[j]
                data[j] = data[j - 1] + 1
        
        #print(total)
    
        ans = min(ans, total)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 10
for i in range(2 ** n):
    total = 0
    data = []
    for j in range(n):
        if i >> j & 1:
            data.append(a[j])
    if a[0] in data and len(data) == k:
        #print(data)
        for j in range(1, k):
            if data[j - 1] >= data[j]:
                total += data[j - 1] + 1 - data[j]
                data[j] = data[j - 1] + 1
        
        #print(total)
    
        ans = min(ans, total)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k==1:
    print(0)
    exit()
a=list(map(int,input().split()))
k-=1
n-=1
c=(1<<k)-1
b=10**18
while c<1<<n:
    w=a[0]
    t=0
    for i in range(n):
        if c>>i&1:
            t+=max(0,w-a[i+1]+1)
            w=max(a[i+1],w+1)
    b=min(b,t)
    x=c&-c
    y=c+x 
    c=((c&~y)//x>>1)|y 
print(b)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k==1:
    print(0)
    exit()
a=list(map(int,input().split()))
k-=1
n-=1
c=(1<<k)-1
b=10**12
while c<1<<n:
    w=a[0]
    t=0
    for i in range(n):
        if c>>i&1:
            t+=max(0,w-a[i+1]+1)
            w=max(a[i+1],w+1)
    b=min(b,t)
    x=c&-c
    y=c+x 
    c=((c&~y)//x>>1)|y 
print(b)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k==1:
    print(0)
    exit()
a=list(map(int,input().split()))
k-=1
n-=1
comb = (1<<k) -1
ans=100000000000
while comb < 1<<n:
    now=a[0]
    tmp=0
    for i in range(n):
        if comb>>i&1:
            tmp+=max(0,now-a[i+1]+1)
            now=max(a[i+1],now+1)
    ans=min(ans,tmp)
    x = comb & -comb 
    y = comb + x 
    comb = ((comb & ~y) // x>>1) | y 
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
k-=1
n-=1
comb = (1<<k) -1
ans=100000000000
while comb < 1<<n:
    now=a[0]
    tmp=0
    for i in range(n):
        if comb>>i&1:
            tmp+=max(0,now-a[i+1]+1)
            now=max(a[i+1],now+1)
    ans=min(ans,tmp)
    x = comb & -comb 
    y = comb + x 
    comb = ((comb & ~y) // x>>1) | y 
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

ans = 10**12
for cmb in combinations(range(1,n),k-1):    
  cost = 0
  h = A[0]
  
  for i in range(1,n):
    h = max(h,A[i])    
    if i in cmb: 
      if A[i] >= h:        
        h = A[i] 
      else:
        cost += h - A[i] + 1        
        h += 1 
          
  ans = min(ans,cost)
          
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

ans = 10**12
for cmb in combinations(range(1,n),k-1):    
  cost = 0
  h = A[0]
  
  for i in range(1,n):
    h = max(h,A[i])    
    if i in cmb: 
      if A[i] > h:        
        h = A[i] 
      else:
        cost += h - A[i] + 1
        h += 1                
      
  ans = min(ans,cost)
          
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

ans = 10**10
for cmb in combinations(range(1,n),k-1):    
  cost = 0
  h = A[0]
  
  for i in range(1,n):
    h = max(h,A[i])    
    if i in cmb: 
      if A[i] > h:        
        h = A[i] 
      else:
        cost += h - A[i] + 1
        h += 1                
      
  ans = min(ans,cost)
          
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

ans = 10**10
for cmb in combinations(range(1,n),k-1):  
  tmp = 0
  h = A[0]  
  for c in cmb:    
    if A[c] > h:
      cost = 0
      h = A[c]      
    else:
      cost = h - A[c] + 1
      h += 1          
    tmp += cost
    
  ans = min(ans,tmp)
  
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

ans = 10**10
for cmb in combinations(range(1,n),k-1):  
  cost = 0
  h = A[0]
  for c in cmb:    
    if A[c] > h:      
      diff = A[c]-h
      h = A[c]      
    else:
      diff = h - A[c] + 1
      h += 1          
    cost += diff    
    
  ans = min(ans,cost)
  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
min_ = 10**12
for i in range(1<<n):
    max_height = li[0]
    money = 0
    can_look = 0
    li_h = []
    for j in range(n):
        if j != 0:
            max_height = max(max_height, li_h[j-1])
        #j番目が見えるようにする
        if j == 0:
            can_look += 1
            li_h.append(li[0])
            continue
        if (i>>j)&1:
            can_look += 1
            if li[j] <= max_height:
                li_h.append(max_height+1)
            else:
                li_h.append(li[j])
        else:
            li_h.append(li[j])
    if can_look >= k:
        money = sum(li_h) - sum(li)
        min_ = min(min_, money)
        print(li_h)
        print(li)
        
print(min_)

Submission status: Wrong Answer

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

res = float("inf")
for i in range(2**n):
    if(i%2 == 0):
        continue
    useidx = [0 for _ in range(n)]
    usecnt = 0
    for j in range(n):
        if (i>>j)&1:
            useidx[j] = 1
            usecnt = usecnt+1
    if (usecnt != k):
        continue
    sm = 0
    mx = a[0]
    for j in range(1,n):
        if useidx[j]==1:
            if a[j]<=mx:
                sm = sm + (mx+1)-a[j]
                mx = mx +1
        mx = max(mx, a[j])
    res = min(res,sm)
print(sm)

Submission status: Wrong Answer

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

res = float("inf")
for i in range(2**n):
    if(i%2 == 0):
        continue
    useidx = [0 for _ in range(n)]
    usecnt = 0
    for j in range(n):
        if (i>>j)&1:
            useidx[j] = 1
            usecnt = usecnt+1
    if (usecnt < k):
        continue
    sm = 0
    mx = a[0]
    for j in range(1,n):
        if useidx[j]==1:
            if a[j]<=mx:
                sm = sm + (mx+1)-a[j]
                mx = mx +1
        mx = max(mx, a[j])
    res = min(res,sm)
print(sm)

Submission status: Wrong Answer

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

res = float("inf")
for i in range(2**n):
    if(i%2 == 0):
        continue
    useidx = [0 for _ in range(n)]
    usecnt = 0
    for j in range(n):
        if (i>>j)&1:
            useidx[j] = 1
            usecnt = usecnt+1
    if (usecnt < k):
        continue
    sm = 0
    mx = a[0]
    for j in range(1,n):
        if useidx[j]==1:
            if a[j]<=mx:
                print("a[j]="+str(a[j]))
                sm = sm + (mx+1)-a[j]
                print("max="+str(mx+1))
                mx = mx +1
        mx = max(mx, a[j])
    res = min(res,sm)
print(sm)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

ans = 10 ** 12
for i in range(1, 2 ** N):
    if bin(i)[2:].count('1') != K: continue
    tmp = 0
    h = a[0]
    for j in range(1, N):
        if (i >> j) & 1:
            h = max(h + 1, a[j])
            tmp += h - a[j]
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

ans = 10 ** 12
for i in range(2 ** N):
    if bin(i)[2:].count('1') < K: continue
    tmp = 0
    left = a[0]
    for j in range(1, N):
        if (i >> j) & 1:
            add = max(0, left + 1 - a[j])
            left = a[j] + add
            tmp += add
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = 10**30
    for pattern in combinations(range(1,N), K-1):
        height = A[0]
        tmp = 0
        for i in pattern:
            height += 1
            if A[i] >= height:
                height = A[i]
                continue
            tmp += height - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = 10**30
    for pattern in combinations(range(1,N), K-1):
        height = A[0]
        tmp = 0
        print(pattern)
        for i in pattern:
            height += 1
            if A[i] >= height:
                height = A[i]
                continue
            tmp += height - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = float('inf')
    for pattern in combinations(range(1,N), K-1):
        height = A[0]
        tmp = 0
        for i in pattern:
            height += 1
            if A[i] >= height:
                height = A[i]
                continue
            else:
                tmp += height - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = float('inf')
    for pattern in combinations(range(1,N), K-1):
        height = A[0]
        tmp = 0
        for i in pattern:
            height += 1
            if A[i] >= height:
                height = A[i]
                continue
            tmp += height - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = float('inf')
    for pattern in combinations(range(1,N), K-1):
        a_0 = A[0]
        tmp = 0
        for i in pattern:
            a_0 += 1
            if A[i] > a_0:
                a_0 = A[i]
                continue
            tmp += a_0 - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = float('inf')
    for pattern in combinations(range(1,N), K-1):
        a_0 = A[0]
        tmp = 0
        for i in pattern:
            a_0 += 1
            tmp += a_0 - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
from itertools import combinations 
INF = int(1e9)*15
def main():
    n, k = map(int, readline().split()) 
    a = np.array(readline().split(), np.int64)
    c = np.array([int(i) for i in range(1, n)])
    ans = INF
    for comb in combinations(c, k-1):
        mx = a[0]
        cost = 0
        for i in range(n):
            if i in comb:
                if a[i] > mx:
                    cost += mx-a[i]
                    mx = max(a[i], mx)
                else:
                    mx += 1
                    cost += mx-a[i] 
            else:
                mx = max(mx, a[i])

        ans = min(cost, ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
from itertools import combinations 
INF = int(1e9)*15
def main():
    n, k = map(int, readline().split()) 
    a = np.array(readline().split(), np.int64)
    c = np.array([int(i) for i in range(1, n)])
    ans = INF
    for comb in combinations(c, k-1):
        mx = a[0]
        cost = 0
        for i in range(n):
            mx = max(mx, a[i])
            if i in comb:
                mx += 1
                cost += mx-a[i] 
        ans = min(cost, ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
from itertools import combinations 
INF = int(1e9)*15
def main():
    n, k = map(int, readline().split()) 
    a = np.array(readline().split(), np.int64)
    c = np.array([int(i) for i in range(1, n)])
    ans = INF
    for comb in combinations(c, k-1):
        mx = a[0]
        cost = 0
        for i in range(n):
            if i in comb:
                mx += 1
                cost += mx-a[i] 
        ans = min(cost, ans)
    print(ans)
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(2**N):
    if bin(i).count('1')!=K:
        continue
    cost = 0
    now = 0
    for j in range(N):
        if i>>j&1:
            if now>=a[j]:
                cost += now-a[j]+1
                now += 1
            else:
                now = a[j]
        else:
            now = max(now,a[j])
    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 = 10**9
for i in range(2**N):
    c = 0
    cost = 0
    now = 0
    for j in range(N):
        
        if i>>j&1:
            if now>=a[j]:
                cost += now-a[j]+1
                now += 1
            else:
                now = a[j]
            c += 1
        else:
            if now<a[j]:
                c += 1
            now = max(now,a[j])
    if c>=K:
        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 = 10**9
for i in range(2**N):
    if bin(i).count('1')<K:
        continue
    cost = 0
    now = 0
    for j in range(N):
        if i>>j&1:
            if now>=a[j]:
                cost += now-a[j]+1
                now += 1
            else:
                now = a[j]
        else:
            now = max(now,a[j])
    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 = 10**9
for i in range(2**N):
    if bin(i).count('1')<K:
        continue
    cost = 0
    now = 0
    for j in range(N):
        if i>>j&1:
            if now>a[j]:
                cost += now-a[j]+1
                now += 1
            else:
                now = a[j]
        else:
            now = max(now,a[j])
    ans = min(ans,cost)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations as comb
N,K = map(int,input().split())
a = [int(i) for i in input().split()]
ans = float("INF")

for c in comb(range(1,N),K-1):
    cnt = 0
    color = 0
    mh = a[0]
    for i in range(1,N):
        if i == c[color]:
            cnt += max(mh-a[i]+1,0)
            color += 1
            mh = max(mh+1,a[i])
        if color == K-1: break
    ans = min(cnt ,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
a = [int(i) for i in input().split()]
ans = float("INF")
for i in range(1<<N):
    cnt = 0
    tmp = a[:]
    m = tmp[0]
    for j in range(1,N):
        if i >> j & 1 and m >= tmp[j]:
            cnt += m - tmp[j] + 1
            tmp[j] = m + 1
        m = tmp[j]
    color = 1
    m = tmp[0]
    for j in range(1,N):
        if tmp[j] > m:
            color += 1
            m = tmp[j]
    if color >= K: ans = min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
a = [int(i) for i in input().split()]
ans = float("INF")
for i in range(1<<N):
    cnt = 0
    tmp = a[:]
    m = tmp[0]
    for j in range(1,N):
        if i >> j & 1 and m >= tmp[j]:
            cnt += m - tmp[j] + 1
            tmp[j] = m + 1
            m = tmp[j]
    color = 1
    m = tmp[0]
    for j in range(1,N):
        if tmp[j] > m: 
            color += 1
            m = tmp[j]
    if color >= K: ans = min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
if k == 1:
  print(0)
  exit()
  
b = [int(i)+1 for i in range(n-1)]
ans = float('inf')
import itertools
for j in itertools.combinations(b, k-1):
  now_high = a[0]
  cost = 0
  for l in j:
    if a[l] > now_high:
      now_high = a[l]
    else:
      cost += (now_high-a[l]+1)
      now_high += 1
  ans =  min(ans,cost)
  
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product
import copy
N,K=map(int,input().split())
b =list(map(int,input().split()))
cost=float("Inf")
for pattern in product([0,1],repeat=N-1):
    tmp_cost=0
    idx = 0
    a=copy.deepcopy(b)
    if pattern.count(1) == K-1:
        for i in range(1,len(pattern)+1):
            if pattern[i-1]==1:
                if max(a[idx:i+1]) == a[i]:
                    if a[idx]==a[i]:
                        a[i]+=1
                        tmp_cost+=1
                        idx=i
                    else:
                        idx=i
                        continue
                else:             
                    tmp_cost+=max(a[0:i+1])-a[i]+1
                    a[i]+=a[idx]-a[i]+1
                    idx = i
        cost=min(cost,tmp_cost)

print(cost,end="\n")

Submission status: Wrong Answer

Solution code:
from itertools import product
import copy
N,K=map(int,input().split())
b =list(map(int,input().split()))
cost=float("Inf")
for pattern in product([0,1],repeat=N-1):
    tmp_cost=0
    idx = 0
    a=copy.deepcopy(b)
    if pattern.count(1) == K-1:
        for i in range(1,len(pattern)+1):
            if pattern[i-1]==1:
                if max(a[idx:i+1]) == a[i]:
                    idx=i
                    continue
                else:             
                    tmp_cost+=max(a[0:i+1])-a[i]+1
                    a[i]+=a[idx]-a[i]+1
                    idx = i
        cost=min(cost,tmp_cost)

print(cost,end="\n")

Submission status: Wrong Answer

Solution code:
from itertools import product
import copy
N,K=map(int,input().split())
b =list(map(int,input().split()))
cost=float("Inf")
for pattern in product([0,1],repeat=N-1):
    tmp_cost=0
    idx = 0
    #print(pattern)
    a=copy.deepcopy(b)
    if pattern.count(1) == K-1:
        for i in range(1,len(pattern)+1):
            if pattern[i-1]==1:
               # print(f"max={max(a[idx:i+1])},a[{i}]={a[i]}")
                if max(a[idx:i+1]) == a[i]:
                    idx=i
                    continue
                else:
                    #print(f"i-1={i-1},idx={idx},tmp_cost=a[{idx}]-a[{i}]+1,tmp_cost = {tmp_cost}")             
                    tmp_cost+=max(a[0:i])-a[i]+1
                    a[i]+=a[idx]-a[i]+1
                    #print(f"tmp_cost={tmp_cost}")
                    idx = i
        cost=min(cost,tmp_cost)

print(cost,end="\n")

Submission status: Wrong Answer

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

res = 1<<60
for i in range(1<<n):    
    if i % 2 == 0: continue

    use_idx = [0 for _ in range(n)]
    use_cnt = 0
    for j in range(n):
        if i & (1<<j):
            use_idx[j] = 1
            use_cnt += 1
    
    if use_cnt != k: continue

    sm = 0
    mx = a[0]
    for j in range(1, n):
        if use_idx[j]:
            if a[j] < mx:
                sm += mx+1 - a[j]
                mx += 1
        mx = max(mx, a[j])
    
    res = min(res, sm)

print(res)

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(1,2**n):
    num=format(i,"b").zfill(n)
    point=0
    if num.count("1")!=k:continue
    highest=A[0]
    for j in range(1,n):
        if highest>A[j] and num[j]=="1":
            point +=highest-A[j]+1
            highest +=1
        highest=max(highest,A[j])
    ans=min(ans,point)

print(ans)

Submission status: Wrong Answer

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

res = 10 ** 15

for bit in range(2 ** N):
    # b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if bit >> 0 & 1 == 1:
        cost = 0
        cnt = 1
        height = A[0]
        for i in range(1, N):
            if bit >> i & 1 == 1:
                cnt += 1
                if A[i] <= height:
                    cost += height - A[i] + 1
                    height += 1
                else:
                    height = A[i]

        if cnt >= K:
            res = min(res, cost)

print(res)

Submission status: Wrong Answer

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

res = 10 ** 15

for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) == K and bit >> 0 & 1 == 1:
        cnt = 0
        height = A[0]
        for i in range(1, len(b)):
            if bit >> i & 1 == 1:
                if A[i] <= height:
                    cnt += height - A[i] + 1
                    height += 1
                else:
                    height = A[i]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

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

res = 10 * 15

for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) == K and bit >> 0 & 1 == 1:
        cnt = 0
        height = A[0]
        for i in range(1, len(b)):
            if bit >> i & 1 == 1:
                if A[i] <= height:
                    cnt += height - A[i] + 1
                    height += 1
                else:
                    height = A[i]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

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

res = 100000000000

for bit in range(2 ** N):
    b = [int(bin(bit)[i + 2]) for i in range(len(bin(bit)) - 2)]
    if b.count(1) == K and bit >> 0 & 1 == 1:
        cnt = 0
        height = A[0]
        for i in range(1, len(b)):
            if bit >> i & 1 == 1:
                if A[i] <= height:
                    cnt += height - A[i] + 1
                    height += 1
                else:
                    height = A[i]

        res = min(res, cnt)

print(res)

Submission status: Wrong Answer

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

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

        res = min(res, cnt)

print(res)