Problem:


				Max Score: 350 Points 

Problem Statement

					There are N buildings along the line. The i-th building from the left is colored in color i, and its height is currently a_i meters. 

					Chokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least K buildings from the left. 

					You can increase height of buildings, but it costs 1 yens to increase 1 meters. It means you cannot make building that height is not integer. 

					You cannot decrease height of buildings. 

					Calculate the minimum cost of satisfying Chokudai's objective. 

					Note: "Building i can see from the left" means there are no j exists that (height of building j) ≥ (height of building i) and j < i. 

Input Format

N K
a_1 a_2 a_3 ... a_N

Output Format

						Print the minimum cost in one line. In the end put a line break.

Constraints

1 ≤ K ≤ N ≤ 15

1 ≤ a_i ≤ 10^9

Scoring

						Subtask 1 [120 points] 

N = K

						Subtask 2 [90 points] 

N ≤ 5

a_i ≤ 7

						Subtask 3 [140 points] 

There are no additional constraints.

Sample Input 1

5 5
3949 3774 3598 3469 3424

Sample Output 1

1541

					The optimal solution is (height of buildings from the left) = [3949, 3950, 3951, 3952, 3953].

Sample Input 2

5 3
7 4 2 6 4

Sample Output 2

7

					The optimal solution is (height of buildings from the left) = [7, 8, 2, 9, 4].



Sample input:
5 5
3949 3774 3598 3469 3424


Sample output:
1541

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# square869120Contest #4
# B - Buildings are Colorful!
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b
N, K = map(int, input().split())
*A, = map(int, input().split())

ans = float('inf')
for bit in range(2**N):
    cnt = 0
    for i in range(N):
        if bit >> i & 1:
            cnt += 1
    if cnt < K:
        continue

    cost = 0
    maxh = 0
    for i, a in enumerate(A):
        if maxh < a:
            maxh = a
        if bit >> i & 1 and maxh >= a:
            cost += maxh - a + 1
            maxh += 1

    if cost < ans:
        ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
# square869120Contest #4
# B - Buildings are Colorful!
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b
N, K = map(int, input().split())
*A, = map(int, input().split())

ans = float('inf')
for bit in range(2**N):
    cnt = 0
    for i in range(N):
        if bit >> i & 1:
            cnt += 1
    if cnt < K:
        continue

    cost = 0
    maxh = 0
    for i, a in enumerate(A):
        if maxh < a:
            maxh = a
        if bit >> i & 1 and maxh > a:
            cost += maxh - a + 1
            maxh += 1

    if cost < ans:
        ans = cost

print(ans)

Submission status: Wrong Answer

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

inf = 10 ** 9
ans = inf
for bits in range(2 ** n):
    selected = set()
    for shift in range(n):
        if bits & (1 << shift) > 0:
            selected.add(shift)

    if len(selected) < k:
        continue
    highest = aa[0]
    cost = 0

    for i in range(1, n):
        if highest < aa[i]:
            highest = aa[i]
        else:
            if i in selected:
                highest += 1
                cost += highest - aa[i]
    ans = min(ans, cost)
print(ans)

Submission status: Wrong Answer

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


def to(bits):
    ret = set()
    for shift in range(n):
        if bits & (1 << shift) > 0:
            ret.add(shift)
    return ret


inf = 10 ** 9
ans = inf
for bits in range(2 ** n):
    selected = to(bits)
    if len(selected) != k:
        continue
    highest = 0
    cost = 0

    for i in range(n):
        if aa[i] > highest:
            highest = aa[i]
        else:
            if i in selected:
                highest += 1
                cost += highest - aa[i]

    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 = list(map(int, input().split()))
ans = 10**17

for i in comb(a[1:], K-1):
    tmp = 0
    pre = a[0]
    for j in range(K-1):
        if i[j] <= pre:
            tmp += pre+1-i[j]
            pre += 1
        else:
            pre = i[j]
    
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int, input().split())
data = getInputIntList()

items = [n for n in range(1,n)]
comb = []
for v in itertools.combinations(items,k-1):
    comb.append(v)

ans = sys.maxsize
for com in comb:
    tmpans = 0
    highest = data[0]
    for c in com:
        if highest < data[c]:
            highest = data[c]
            continue
        else:
            highest += 1
            tmpans += highest - data[c]

    ans = min(ans, tmpans)

if k == 1:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int, input().split())
data = getInputIntList()

items = [n for n in range(1,n)]
comb = []
for v in itertools.combinations(items,k-1):
    comb.append(v)

ans = sys.maxsize
for com in comb:
    tmpans = 0
    highest = data[0]
    for c in com:
        if highest < data[c]:
            highest = data[c]
            continue
        else:
            highest += 1
            tmpans += highest - data[c]

    ans = min(ans, tmpans)



print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n, k = map(int, input().split())
data = getInputIntList()

items = [n for n in range(1,n)]
comb = []
for v in itertools.combinations(items,k-1):
    comb.append(v)

ans = sys.maxsize
for com in comb:
    tmpans = 0
    highest = data[0]
    for c in com:
        if highest < data[c]:
            highest = data[c]+1
            continue
        else:
            highest += 1
            tmpans += highest - data[c]

    ans = min(ans, tmpans)



print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline


def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))

# mod=10**9+7
# rstrip().decode('utf-8')

import numpy as np
from itertools import combinations

def main():
	n,k=MI()
	ans=10**18

	A=LI()
	A=np.array(A)

	for t in combinations(range(n),k):
		if t[0]==0:
			m=A[0]
		elif A[:t[0]].max()<A[t[0]]:
			m=A[0]
		else:
			m=A[:t[0]].max()+1
		B=A[list(t)]
		tmp=m
		cnt=0
		for i in B:
			cnt+=max(tmp-i,0)
			tmp=max(tmp+1,i+1)
		ans=min(ans,cnt)

	print(ans)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline


def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))

# mod=10**9+7
# rstrip().decode('utf-8')

import numpy as np
from itertools import combinations

def main():
	n,k=MI()
	ans=10**18

	A=LI()
	A=np.array(A)

	for t in combinations(range(n),k):
		if t[0]==0:
			m=A[0]
		elif A[:t[0]].max()<A[t[0]]:
			m=A[0]
		else:
			m=A[:t[0]].max()+1
		B=A[list(t)]
		C=m-B
		ans=min(ans,C.sum()+k*(k-1)//2)

	print(ans)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline


def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))

# mod=10**9+7
# rstrip().decode('utf-8')

import numpy as np
from itertools import combinations

def main():
	n,k=MI()
	ans=10**18

	A=LI()
	A=np.array(A)

	for t in combinations(range(n),k):
		m=max(A[:t[0]+1])
		B=A[list(t)]
		C=m-B
		ans=min(ans,C.sum()+k*(k-1)//2)
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:


class BitSeacher:
    def __init__(self,bitcount):
        self.counter =0
        self.bitcount = bitcount
        self.mx = 2**bitcount-1
    
    def __next__(self):
        if self.counter > self.mx:
            raise StopIteration()
        ans =[]
        flg =1
        for i in range(self.bitcount):
            if flg & self.counter != 0:
                ans.append(i)
            flg = flg.__lshift__(1)
        self.counter+=1
        return ans

    def __iter__(self):
        return self

def checker(A,group,K):
    if len(group) < K:
        return 10**10

    tbl = [False for i in A]
    for i in group:
        tbl[i]=True
    mx = 0
    count = 0
    for i in range(len(A)):
        if not tbl[i]:
            mx = max(mx,A[i])
            continue
        
        if mx < A[i]:
            mx = A[i]
        else:
            count += mx - A[i] +1
            mx +=1
    return count






def resolve():
    N,K = map(int,input().split())
    A= list(map(int,input().split()))
    mn = 10**10
    for group in BitSeacher(N):
        group.sort()
        mn = min(mn,checker(A,group,K))

    print(mn)



if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:


class BitSeacher:
    def __init__(self,bitcount):
        self.counter =0
        self.bitcount = bitcount
        self.mx = 2**bitcount-1
    
    def __next__(self):
        if self.counter > self.mx:
            raise StopIteration()
        ans =[]
        flg =1
        for i in range(self.bitcount):
            if flg & self.counter != 0:
                ans.append(i)
            flg = flg.__lshift__(1)
        self.counter+=1
        return ans

    def __iter__(self):
        return self

def checker(A,group,K):
    if len(group) < K:
        return 10**9

    tbl = [False for i in A]
    for i in group:
        tbl[i]=True
    mx = 0
    count = 0
    for i in range(len(A)):
        if not tbl[i]:
            mx = max(mx,A[i])
            continue
        
        if mx < A[i]:
            mx = A[i]
        else:
            count += mx - A[i] +1
            mx +=1
    return count






def resolve():
    N,K = map(int,input().split())
    A= list(map(int,input().split()))
    mn = 10**9
    for group in BitSeacher(N):
        group.sort()
        mn = min(mn,checker(A,group,K))

    print(mn)



if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

min_cost = 1 << 50
for mask in range(1<<N):
    cost = 0
    viewable_buildings = 0
    prev_height = A[0]
    for i in range(N):
        # not chose this building
        if mask & (1<<i) == 0:
            continue
        # extend height this builng
        viewable_buildings += 1
        if i == 0:
            continue
        if A[i] <= prev_height:
            cost += prev_height - A[i] + 1
            prev_height += 1
        else:
            prev_height = A[i]
    if viewable_buildings >= K:
        min_cost = min(min_cost, cost)
print(min_cost)

Submission status: Wrong Answer

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

min_cost = 1 << 64
for mask in range(1<<N):
    cost = 0
    viewable_buildings = 0
    prev_height = A[0]
    for i in range(N):
        if mask & (1<<i) >= 1:
            # extend height this builng
            viewable_buildings += 1
            if i == 0:
                continue
            if A[i] <= prev_height:
                cost += prev_height - A[i] + 1
                prev_height += 1
            else:
                prev_height = A[i]
    if viewable_buildings >= K:
        min_cost = min(min_cost, cost)
print(min_cost)

Submission status: Wrong Answer

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

min_cost = 1 << 64
for mask in range(1<<N):
    cost = 0
    viewable_buildings = 0
    prev_height = A[0]
    for i in range(N):
        # not chose this building
        if mask & (1<<i) == 0:
            continue
        # extend height this builng
        viewable_buildings += 1
        if i == 0:
            continue
        if A[i] <= prev_height:
            cost += prev_height - A[i] + 1
            prev_height += 1
        else:
            prev_height = A[i]
    if viewable_buildings >= K:
        min_cost = min(min_cost, cost)
print(min_cost)

Submission status: Wrong Answer

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

ans = 10 ** 10
for b in range(1 << n):
    if bin(b).count("1") != k:
        continue

    max_ai = 0
    cost = 0
    for i in range(n):
        if (b & 1 << i):
            if A[i] > max_ai:
                max_ai = A[i]
            else:
                cost += (max_ai + 1) - A[i]
                max_ai += 1
        else:
            max_ai = max(max_ai, A[i])

    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 ** 10
for b in range(1 << n):
    if bin(b).count("1") < k:
        continue

    max_ai = 0
    cost = 0
    for i in range(n):
        if (b & 1 << i):
            if A[i] > max_ai:
                max_ai = A[i]
            else:
                cost += (max_ai + 1) - A[i]
                max_ai += 1
        else:
            max_ai = max(max_ai, A[i])

    ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import combinations
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
ans = 10**10
for i in combinations(range(1,N),K-1):
    tallest = A[0]
    tmp = 0
    for j in range(1,N):
        if j in i:
            if tallest >= A[j]:
                tmp += (tallest - A[j] + 1)
                tallest = (tallest + 1)
            else:
                tallest = max(tallest,A[j])
        else:
            tallest = max(tallest,A[j])
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import combinations
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
ans = 10**9
for i in combinations(range(1,N),K-1):
    tallest = A[0]
    tmp = 0
    for j in range(1,N):
        if j in i:
            if tallest >= A[j]:
                tmp += (tallest - A[j] + 1)
                tallest = (tallest + 1)
            else:
                tallest = max(tallest,A[j])
        else:
            tallest = max(tallest,A[j])
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import combinations
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
ans = 10**9
for i in combinations(range(1,N),K-1):
    tallest = A[0]
    tmp = 0
    for j in range(1,N):
        if j in i:
            if tallest >= A[j]:
                tmp += (tallest - A[j] + 1)
                tallest = (tallest + 1)
            else:
                tallest = max(tallest,A[j])
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    ans = 10**30
    for i in range(1<<N):
        hght = A[0]
        cst = 0
        cnt = 1
        for j in range(1,N):
            hght = max(hght,A[j])
            if (i>>j)&1:
                cnt += 1
                if A[j]<=hght: 
                    cst += hght-A[j]+1
                    hght += 1
                else:
                    hght = A[j]
        if cnt >= K:
            ans = min(ans,cst)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    ans = 10**20
    for i in range(1<<N):
        hght = A[0]
        cst = 0
        cnt = 1
        for j in range(1,N):
            if (i>>j)&1:
                cnt += 1
                if A[j]<=hght: 
                    cst += hght-A[j]+1
                    hght += 1
                else:
                    hght = A[j]
        if cnt >= K:
            ans = min(ans,cst)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

ans = 10 ** 9 * 20
for bit in range(2 ** N):
    if bin(bit).count("1") < K or not (bit >> 0 & 1):
        continue

    res = 0
    tmp = A[0]
    for i in range(1, N):
        if bit >> i & 1:
            res += max(0, tmp - A[i] + 1)
            tmp = max(A[i], tmp + 1)    
    ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

ans = 10 ** 9 * 20
for bit in range(2 ** N):
    if bin(bit).count("1") < K:
        continue

    res = 0
    tmp = A[0]
    for i in range(1, N):
        if bit >> i & 1:
            res += max(0, tmp - A[i] + 1)
            tmp = max(A[i], tmp + 1)    
    ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K, *A = map(int, open(0).read().split())

ans = 10 ** 9 * 20
for bit in range(2 ** N):
    res = 0
    cnt = 1
    tmp = A[0]
    for i in range(1, N):
        if bit >> i & 1:
            res += max(0, tmp - A[i] + 1)
            cnt += 1
            tmp = max(A[i], tmp + 1)
    
    if cnt >= K:
        ans = min(ans, res)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 10**11#INF

# 先頭を除いたn-1個からk-1個を選んで全探索 n,kが小さいのでbit全探索する
for i in range(2**(n-1)):
  select = [0]#先頭は必ずk　の中に入る
  len_select = 1
  for j in range(n-1):
    if i >> j & 1 and len_select < k:
      select.append(j+1)#先頭を除いてj 個目を選ぶ
      len_select += 1
  # 選び終わったら、selectないの各要素に対してそれより前にselectの要素外に自身より大きいものがないか確かめる
  # 前から操作してmaxを保持すればO(n),めんどくさいのでO(n**3)でやる
  ok = 1
  #print(select,i)
  for m in select:
    for l in range(n):
      if l in select: continue
      if l < k and a[l] >= a[m]:
        #print(i,m)
        ok = 0
  if ok == 0 or len_select < k: continue
  else:
    ans_i = 0
    pre_val = a[0]#1つ前の要素の値
    for j in range(1,k):
      #print(i,a[select[j]])
      if pre_val >= a[select[j]]:
        ans_i += pre_val+1 - a[select[j]]
        pre_val += 1
      else:#元々大きい
        pre_val = a[select[j]]
  ans = min(ans,ans_i)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 10**11#INF

# 先頭を除いたn-1個からk-1個を選んで全探索 n,kが小さいのでbit全探索する
for i in range(2**(n-1)):
  select = [0]#先頭は必ずk　の中に入る
  len_select = 1
  for j in range(n-1):
    if i >> j & 1 and len_select < k:
      select.append(j+1)#先頭を除いてj 個目を選ぶ
      len_select += 1
  # 選び終わったら、selectないの各要素に対してそれより前にselectの要素外に自身より大きいものがないか確かめる
  # 前から操作してmaxを保持すればO(n),めんどくさいのでO(n**3)でやる
  ok = 1
  #print(select,i)
  for m in select:
    for l in range(n):
      if l in select: continue
      if l < k and a[l] > a[m]:
        #print(i,m)
        ok = 0
  if ok == 0 or len_select < k: continue
  else:
    ans_i = 0
    pre_val = a[0]#1つ前の要素の値
    for j in range(1,k):
      #print(i,a[select[j]])
      if pre_val >= a[select[j]]:
        ans_i += pre_val+1 - a[select[j]]
        pre_val += 1
      else:#元々大きい
        pre_val = a[select[j]]
  ans = min(ans,ans_i)
while ans == 10**11:
  pass
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 10**11#INF
for i in range(2**(n-1)):
  select = [0]#先頭は必ずk　の中に入る
  len_select = 1
  for j in range(n-1):
    if i >> j & 1 and len_select < k:
      select.append(j+1)#先頭を除いてj 個目を選ぶ
      len_select += 1
  # 選び終わったら、selectないの各要素に対してそれより前にselectの要素外に自身より大きいものがないか確かめる
  # 前から操作してmaxを保持すればO(n),めんどくさいのでO(n**3)でやる
  ok = 1
  #print(select,i)
  for m in select:
    for l in range(n):
      if l in select: continue
      if l < k and a[l] > a[m]:
        #print(i,m)
        ok = 0
  if ok == 0 or len_select < k: continue
  else:
    ans_i = 0
    pre_val = a[0]#1つ前の要素の値
    for j in range(1,k):
      #print(i,a[select[j]])
      if pre_val >= a[select[j]]:
        ans_i += pre_val+1 - a[select[j]]
        pre_val += 1
      else:#元々大きい
        pre_val = a[select[j]]
  ans = min(ans,ans_i)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env/ python3
#N = int(input())
#S = input()
#A, B, C = input().split()
N, K = map(int, input().split())
a = list(map(int, input().split()))
#N = int(input())
#H, W = map(int, input().split())
#S = [input() for _ in range(H)]
#print(a[0])
ans = 0
pos = 0
neg = []
for i in range(1,N):
    if a[i] - a[0] > 0: pos += 1
    else: neg.append(a[0] - a[i])
if pos < K:
    neg.sort()
    #print(neg)
    for i in range(K-pos-1):
        ans += neg[i] + i + 1
    print(ans)
else: print('0')

Submission status: Wrong Answer

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

ans = 10 ** 18
for build in product(range(2), repeat=N - 1):
    if sum(build) != K - 1:
        continue

    # 左のやつは絶対見る
    cost = 0
    h = A[0]
    for i, p in enumerate(build, start=1):
        if p == 0:
            continue
        if A[i] > h:
            h = A[i]
            continue
        else:
            h += 1
            cost += (h - A[i])
    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 = 1145141919810364364334
for i in range(1,2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
            
    if sum(ls) < k:
        continue
        
    elif ls[0] == 1:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = t[0]
        cost = 0
        for j in range(len(qq) - 1) :
            if t[j+1] <= mt:
                cost += (mt + 1 - t[j+1])
                mt += 1
            else:
                mt = max(t[j+1] , max(a[:qq[j+1]]))
    else:
        continue
                
    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 = 1145141919810364364334
for i in range(2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
    
    cost = 0
    if sum(ls) < k or ls[0] == 0:
        continue
        
    else:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = a[0]
        cost = 0
        for j in range(1,n):
            if ls[j] == 1:
                if a[j] <= mt:
                    cost += (mt + 1 - a[j])
                    mt += 1
                else:
                    mt = max(a[:j])
            else:
                mt = max(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 = 1145141919810364364334
for i in range(2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
    
    cost = 0
    if sum(ls) < k or ls[0] == 0:
        continue
        
    else:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = a[0]
        cost = 0
        for j in range(1,n):
            if ls[j] == 1:
                if a[j] <= mt:
                    cost += (mt + 1 - a[j])
                    mt += 1
                else:
                    mt = max(a[:j]) + 1
            else:
                mt = max(a[:j]) + 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 = 1145141919810364364334
for i in range(2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
    
    cost = 0
    if sum(ls) < k or ls[0] == 0:
        continue
        
    else:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = t[0]
        cost = 0
        for j in range(len(qq) - 1) :
            if t[j+1] <= mt:
                cost += (mt + 1 - t[j+1])
                mt += 1
            else:
                mt = t[j+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 = 1145141919810364364334
for i in range(2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
            print(ls,qq)
    
    cost = 0
    if sum(ls) < k or ls[0] == 0:
        continue
        
    else:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = t[0]
        cost = 0
        for j in range(len(qq) - 1) :
            if t[j+1] <= mt:
                cost += (mt + 1 - t[j+1])
                mt += 1
            else:
                mt = max(mt,t[j+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 = 1145141919810364364334
for i in range(1,2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
    
    cost = 0
    if sum(ls) < k or ls[0] == 0:
        continue
        
    else:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = t[0]
        cost = 0
        for j in range(len(qq) - 1) :
            if t[j+1] <= mt:
                cost += (mt + 1 - t[j+1])
                mt += 1
            else:
                mt = t[j+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 = 1145141919810364364334
for i in range(1,2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
            
    if sum(ls) < k:
        continue
        
    elif ls[0] == 1:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = t[0]
        cost = 0
        for j in range(len(qq) - 1) :
            if t[j+1] <= mt:
                cost += (mt + 1 - t[j+1])
                mt += 1
            else:
                mt = t[j+1]
                
    else:
        continue
                
    ans = min(ans,cost)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
N, K = list(map(int, input().split()))
A_orig = list(map(int, input().split()))
 
res = len(A_orig) * max(A_orig)
for pattern in itertools.combinations(range(N), K):
    cnt = 0
    A = A_orig[:]
    for p in pattern:
        if p == 0:
            continue
        extension = max(0, max(A[:p]) - A[p] + 1)
        A[p] += extension
        cnt += extension
    res = min(res, cnt)