aacord’s memo

abcを中心にpythonで解いた問題のメモ、整理をしています。緑になった。

square869120Contest #4 b 'Buildings are Colorful!' (python)

B - Buildings are Colorful!
制約が小さいのでビルの選び方を n-1Ck-1 通り全探索して間に合う
選んでないビルもビルの最大値に関わることがあることに注意

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):
  s = list(j)
  now_high = a[0]
  cost = 0
  for l in range(1,n):
    if l in s:
      if a[l] > now_high:
        now_high = a[l]
        continue
      cost += (now_high-a[l]+1)
      now_high += 1
    now_high = max(now_high, a[l])
  ans =  min(ans,cost)
print(ans)