Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N-=K
        if N<=0:
            if abs(N)<abs(M):
                print(abs(N))
                break
            else:
                print(abs(M))
                break

Fixed submission:
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N=N%K
        if M==N:
            M = N
            N-=K
            if N<=0:
                if abs(N)<abs(M):
                    print(abs(N))
                    break
                else:
                    print(abs(M))
                    break

Buggy python submission (verdict: Runtime Error):
from sys import stdin
import numpy as np
N,K=list(map(int,(stdin.readline().strip().split())))
print(np.gcd(N,K))

Fixed submission:
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N=N%K
        if M==N:
            M = N
            N-=K
            if N<=0:
                if abs(N)<abs(M):
                    print(abs(N))
                    break
                else:
                    print(abs(M))
                    break

Buggy python submission (verdict: Runtime Error):
from sys import stdin
import math
N,K=list(map(int,(stdin.readline().strip().split())))
print(math.gcd(N,K))

Fixed submission:
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N=N%K
        if M==N:
            M = N
            N-=K
            if N<=0:
                if abs(N)<abs(M):
                    print(abs(N))
                    break
                else:
                    print(abs(M))
                    break
Buggy python submission (verdict: Runtime Error):
from sys import stdin
from scipy import special
N,M = list(map(int, (stdin.readline().strip().split())))
count=0
count+=special.comb(N,2, True)
count+=special.comb(M,2, True)
print(count)

Fixed submission:
from sys import stdin
import itertools
N,M = list(map(int, (stdin.readline().strip().split())))
count=0
count+=len(list(itertools.combinations(range(N), 2)))
count+=len(list(itertools.combinations(range(M), 2)))
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from collections import deque
S=list(stdin.readline().strip())
N=list(map(int,stdin.readline().strip().split()))[0]

d = deque(S)
rev=False
for i in range(N):

    Q = list(stdin.readline().strip().split())
    if Q[0]=='1' :
        if rev==False:rev=True
        else:rev=False

    if Q[0]=='2':
        if Q[1]=='1':
            if rev==False:d.appendleft(Q[2])
            else:d.append(Q[2])
        if Q[1]=='2':
            if rev == False:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])
if rev==False:[print(x,end="") for x in d]
else:[print(d[x],end="") for x in range(len(d)-1,-1,-1)]

Fixed submission:
from sys import stdin
from collections import deque
d=deque(list(stdin.readline().strip()))
N=list(map(int,stdin.readline().strip().split()))[0]
rev=False
for i in range(N):
    Q = list(stdin.readline().strip().split())
    if Q[0]=='1' :
        if rev==False:rev=True
        else:rev=False
    if Q[0]=='2':
        if Q[1]=='1':
            if rev==False:d.appendleft(Q[2])
            else:d.append(Q[2])
        if Q[1]=='2':
            if rev == False:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])
# if rev==False:[print(x,end="") for x in d]
# else:[print(d[x],end="") for x in range(len(d)-1,-1,-1)]
if rev==False:[print(d.popleft(),end="") for x in range(len(d))]
else:[print(d.pop(),end="") for x in range(len(d))]
Buggy python submission (verdict: Runtime Error):
from sys import stdin
A,B,X=list(map(int,(stdin.readline().strip().split())))

left=0
right=10**(9)+1

while(right-left>1):
    num = left + (right - left) // 2
    cost=A*num+B*(len(str(num)))
    if cost<=X:
        left=num
    else:
        right=num
print(left)

Fixed submission:
week=['SUN','MON','TUE','WED','THU','FRI','SAT']
S=input()
day=week.index(S)
print(7-day)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import numpy as np
N=int(input())
retu=[i for i in range(N)]
XY=[]
for i in range(N):
    x=list(map(int,input().split()))
    XY.append(x)
XY=np.array(XY)
permi=list(itertools.permutations(retu))
goukei=0
count=0
for i in permi:
    count+=1
    for k in range(1,N):
        goukei+=np.linalg.norm(XY[i[k-1]]-XY[i[k]])
print(goukei/count)

Fixed submission:
import itertools
import numpy as np
N=int(input())
retu=[i for i in range(N)]
XY=[]
for i in range(N):
    x=list(map(int,input().split()))
    XY.append(x)
permi=list(itertools.permutations(retu))
goukei=0
count=0
for i in permi:
    count+=1
    for k in range(1,N):
        a=XY[i[k - 1]][0]- XY[i[k]][0]
        b=XY[i[k - 1]][1]- XY[i[k]][1]
        goukei+=(a**2+b**2)**0.5
print(goukei/count)

Buggy python submission (verdict: Runtime Error):
import itertools
import numpy as np
N=int(input())
retu=[i for i in range(N)]
XY=[]
for i in range(N):
    x=list(map(int,input().split()))
    XY.append(x)
XY=np.array(XY)
permi=list(itertools.permutations(retu))
goukei=0
count=0
for i,j,k in permi:
    count+=1
    goukei+=np.linalg.norm(XY[i]-XY[j])+np.linalg.norm(XY[j]-XY[k])
print(goukei/count)

Fixed submission:
import itertools
import numpy as np
N=int(input())
retu=[i for i in range(N)]
XY=[]
for i in range(N):
    x=list(map(int,input().split()))
    XY.append(x)
permi=list(itertools.permutations(retu))
goukei=0
count=0
for i in permi:
    count+=1
    for k in range(1,N):
        a=XY[i[k - 1]][0]- XY[i[k]][0]
        b=XY[i[k - 1]][1]- XY[i[k]][1]
        goukei+=(a**2+b**2)**0.5
print(goukei/count)
Buggy python submission (verdict: Runtime Error):
N=list(input())
ans=0
for i in range(len(N)-1,-1,-1):
    if i==0:
        if len(N)%2==1:
            del N[0]
            s=""
            for i in N:
                s=s+i
            s=int(s)
            s+=1
            ans+=s
    else:
        if i==len(N)-1:
            ans+=9
        elif i%2==0:
            ans+=(10**(i+1)-10**(i))

print(ans)

Fixed submission:
N=list(input())
ans=0
for i in range(len(N)):
    if i==len(N)-1:
        if len(N)%2==1:
            s=""
            for num in N:
                s=s+num
            s=int(s)-10**i
            s+=1
            ans+=s
    else:
        if i%2==0:
            ans+=10**(i+1)-10**i
print(ans)
Buggy python submission (verdict: Runtime Error):
from sys import stdin
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)
A,B,C,D=list(map(int,stdin.readline().strip().split()))
C_A=(A-1)//C
C_B=B//C
D_A=(A-1)//D
D_B=B//D
CD=lcm(C,D)
CD_A=(A-1)//(CD)
CD_B=B//(CD)
# CD_A=0
# CD_B=0
print((1+B-A)-((C_B-C_A)+((D_B-D_A)-(CD_B-CD_A))))

Fixed submission:
from sys import stdin
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
A,B,C,D=list(map(int,stdin.readline().strip().split()))
C_A=(A-1)//C
C_B=B//C
D_A=(A-1)//D
D_B=B//D
CD=lcm(C,D)
CD_A=(A-1)//(CD)
CD_B=B//(CD)

print((1+B-A)-((C_B-C_A)+((D_B-D_A)-(CD_B-CD_A))))
Buggy python submission (verdict: Runtime Error):
S=list(input())
line=[]
A=["A","C","G","T"]
num_list=[]
flag=False
for s in S:
    if s in A:
        line.append(s)
        flag=True
    elif flag:
        num_list.append(len(line))
        line=[]
        flag=False
print(max(num_list))

Fixed submission:
S=list(input())
line=[]
A=["A","C","G","T"]
num_list=[]
flag=False
for s in S:
    if s in A:
        line.append(s)
        flag=True
    elif flag:
        num_list.append(len(line))
        line=[]
        flag=False
num_list.append(len(line))
if len(num_list)==0:print(0)
else:
    print(max(num_list))

Buggy python submission (verdict: Runtime Error):
S=list(input())
line=[]
A=["A","C","G","T"]
num_list=[]
flag=False
for s in S:
    if s in A:
        line.append(s)
        flag=True
    elif flag:
        num_list.append(len(line))
        line=[]
        flag=False
print(max(num_list))

Fixed submission:
S=list(input())
line=[]
A=["A","C","G","T"]
num_list=[]
flag=False
for s in S:
    if s in A:
        line.append(s)
        flag=True
    elif flag:
        num_list.append(len(line))
        line=[]
        flag=False
num_list.append(len(line))
if len(num_list)==0:print(0)
else:
    print(max(num_list))
Buggy python submission (verdict: Runtime Error):

K=list(map(int,input().split()))
ans=0
num_list=[]
for i in range(1,min(K[0],K[1]+1)):
    if K[0]%i==0 and K[1]%i==0:num_list.append(i)
num_list.reverse()
print(num_list[K[2]-1])

Fixed submission:

K=list(map(int,input().split()))
ans=0
num_list=[]
for i in range(1,min(K[0],K[1])+1):
    if K[0]%i==0 and K[1]%i==0:num_list.append(i)
num_list.reverse()
print(num_list[K[2]-1])
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,W=list(map(int,input().split()))
DP=np.arange((N+1)*(W+1)).reshape(N+1,W+1)
DP=np.zeros_like(DP,dtype=float)
sumv=float(0)
for i in range(1,N+1):
    w,v=list(map(int,input().split()))
    sumv+=v
    for j in range(W+1):
        if j-w>=0:
            DP[i][j]=max(DP[i-1][j],DP[i-1][j-w]+v)
            if DP[i][j]==sumv:
                DP[i][j:]=DP[i][j]
                break
        else:
            DP[i][j]=max(DP[i-1][j],DP[i][j-1])
print(int(DP[-1][-1]))
# for i in DP:
#     print(i)


Fixed submission:
import numpy as np
N,W=list(map(int,input().split()))
DP=np.zeros(W+1)

for i in range(N):
    w,v=list(map(int,input().split()))
    a=DP[:-w]+v
    b=DP[w:]
    c=np.maximum(a,b)
    DP[w:]=c



print(int(DP[-1]))
# for i in DP:
#     print(i)

Buggy python submission (verdict: Time Limit Exceeded):
N,W=list(map(int,input().split()))
DP=[[0]*(W+1) for i in range(N+1)]
for i in range(1,N+1):
    w,v=list(map(int,input().split()))
    for j in range(W+1):
        if j-w>=0:
            DP[i][j]=max(DP[i-1][j],DP[i-1][j-w]+v)
        else:
            DP[i][j]=max(DP[i-1][j],DP[i][j-1])
print(DP[-1][-1])

Fixed submission:
import numpy as np
N,W=list(map(int,input().split()))
DP=np.zeros(W+1)

for i in range(N):
    w,v=list(map(int,input().split()))
    a=DP[:-w]+v
    b=DP[w:]
    c=np.maximum(a,b)
    DP[w:]=c



print(int(DP[-1]))
# for i in DP:
#     print(i)
Buggy python submission (verdict: Runtime Error):
N,X=list(map(int,input().split()))
p=[]
a=[]
def search(N):
    if N==0:
        p.append(1)
        a.append(1)
        return 1,1
    else:
        search(N-1)
        p.append(2*p[N-1]+1)
        a.append(2*a[N-1]+3)
search(N)
def bug(N,X):#p,a
    if X==1:return 0
    if 1<X<=1+a[N-1]:
        return bug(N-1,X-1)
    elif X==a[N-1]+2:
        return p[N-1]+1
    elif a[N-1]+2<X<=2+2*a[N-1]:
        return p[N-1]+1+bug(N-1,X-2-a[N-1]-1)
    elif X==3+2*a[N-1]:
        return 2*p[N-1]-1
print(bug(N,X))

Fixed submission:
N,X=list(map(int,input().split()))
p=[]
a=[]
def search(N):
    if N==0:
        p.append(1)
        a.append(1)
        return 1,1
    else:
        search(N-1)
        p.append(2*p[N-1]+1)
        a.append(2*a[N-1]+3)
search(N)
def bug(N,X):#p,a
    if N==0 and X>=1:return 1
    if X==1:return 0
    if 1<X<=1+a[N-1]:
        return bug(N-1,X-1)
    elif X==a[N-1]+2:
        return p[N-1]+1
    elif a[N-1]+2<X<=2+2*a[N-1]:
        return p[N-1]+1+bug(N-1,X-2-a[N-1])
    elif X==3+2*a[N-1]:
        return 2*p[N-1]+1
print(bug(N,X))
Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

N,M=list(map(int,input().split()))
uf=UnionFind(N+1)
p=list(map(int,input().split()))
for i in range(M):
    x,y=list(map(int,input().split()))
    uf.union(x,y)
count=0
for i in range(1,N+1):
    if p[i-1] in uf.members(i):count+=1
print(count)

Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

N,M=list(map(int,input().split()))
uf=UnionFind(N+1)
p=list(map(int,input().split()))
for i in range(M):
    x,y=list(map(int,input().split()))
    uf.union(x,y)
count=0
for i in range(1,N+1):
    if (uf.same(p[i-1],i)):count+=1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,Y=list(map(int,stdin.readline().strip().split()))
man=Y//10000
gosen=Y//5000

for i in range(man+1,-1,-1):
    if i>N:continue
    for j in range(0,gosen+1):
        if i+j > N or  10000*i+j*5000>Y : break
        for k in range(0,N-(i+j)+1):
            if i + j+k > N: break
            goukei=10000*i+j*5000+k*1000
            if goukei==Y and (i+j+k)==N:
                print(i,j,k)
                exit()

print(-1,-1,-1)


Fixed submission:
from sys import stdin
N,Y=list(map(int,stdin.readline().strip().split()))
a=0
b=0
c=0
i=0
for i in range(N+1):
    for j in range(N+1):
        z=N-i-j
        if i*1000+5000*j+z*10000==Y and z>=0:
            print("%d %d %d" %(z,j,i))
            exit()
print("-1 -1 -1")

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,Y=list(map(int,stdin.readline().strip().split()))
man=Y//10000
gosen=Y//5000

for i in range(man+1,-1,-1):
    if i>N:continue
    for j in range(0,gosen+1):
        if i+j > N or  10000*i+j*5000>Y : break
        for k in range(0,N-(i+j)+1):
            if i + j+k > N: break
            goukei=10000*i+j*5000+k*1000
            if goukei==Y and (i+j+k)==N:
                print(i,j,k)
                exit()

print(-1,-1,-1)


Fixed submission:
from sys import stdin
N,Y=list(map(int,stdin.readline().strip().split()))
a=0
b=0
c=0
i=0
for i in range(N+1):
    for j in range(N+1):
        z=N-i-j
        if i*1000+5000*j+z*10000==Y and z>=0:
            print("%d %d %d" %(z,j,i))
            exit()
print("-1 -1 -1")

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,Y=list(map(int,stdin.readline().strip().split()))
man=Y//10000
gosen=Y//5000

for i in range(man+1,-1,-1):
    for j in range(gosen+1,-1,-1):
        for k in range(N-(i+j)+1,-1,-1):
            goukei=10000*i+j*5000+k*1000
            if goukei==Y and (i+j+k)==N:
                print(i,j,k)
                exit()
            if goukei<Y or 10000*i+j*5000>Y :
                break
print(-1,-1,-1)


Fixed submission:
from sys import stdin
N,Y=list(map(int,stdin.readline().strip().split()))
a=0
b=0
c=0
i=0
for i in range(N+1):
    for j in range(N+1):
        z=N-i-j
        if i*1000+5000*j+z*10000==Y and z>=0:
            print("%d %d %d" %(z,j,i))
            exit()
print("-1 -1 -1")
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N=int(input())
ue=list(map(int,input().split()))
naka=list(map(int,input().split()))
sita=list(map(int,input().split()))
ue.sort(),sita.sort(),naka.sort()
count=0
stop=[0]*N
for i in range(N):
    k=naka[i]
    stop_index=bisect.bisect_right(sita,k)
    if stop_index!=N:
        stop[i]=N-stop_index
for i in range(N):
    k=ue[i]
    a=bisect.bisect_right(naka,k)
    if a!=N:
        count+=sum(stop[a:N])
print(count)


Fixed submission:
import bisect
N=int(input())
ue=list(map(int,input().split()))
naka=list(map(int,input().split()))
sita=list(map(int,input().split()))
sita.sort(),naka.sort(),ue.sort()
count2=[0]*N
count1=[0]*N
for i in range(N):
    m = naka[i]
    k=naka[i]
    small=bisect.bisect_left(ue,k)
    count1[i]=small
    big = bisect.bisect_right(sita, m)
    count2[i]=N-big
ans=0
for i in range(N):
    ans+=count1[i]*count2[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N=int(input())
ue=list(map(int,input().split()))
naka=list(map(int,input().split()))
sita=list(map(int,input().split()))
ue.sort(),sita.sort(),naka.sort()
count=0
stop=[N]*N
for i in range(N):
    k=naka[i]
    stop_index=bisect.bisect_right(sita,k)
    if stop_index!=N:
        stop[i]=N-stop_index
for i in range(N):
    k=ue[i]
    a=bisect.bisect_right(naka,k)
    if a!=N:
        for j in range(a,N):
            count+=stop[j]
print(count)


Fixed submission:
import bisect
N=int(input())
ue=list(map(int,input().split()))
naka=list(map(int,input().split()))
sita=list(map(int,input().split()))
sita.sort(),naka.sort(),ue.sort()
count2=[0]*N
count1=[0]*N
for i in range(N):
    m = naka[i]
    k=naka[i]
    small=bisect.bisect_left(ue,k)
    count1[i]=small
    big = bisect.bisect_right(sita, m)
    count2[i]=N-big
ans=0
for i in range(N):
    ans+=count1[i]*count2[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N=int(input())
ue=list(map(int,input().split()))
naka=list(map(int,input().split()))
sita=list(map(int,input().split()))
ue.sort(),sita.sort(),naka.sort()
count=0
stop=[N]*N
for i in range(N):
    k=naka[i]
    stop_index=bisect.bisect_right(sita,k)
    if stop_index!=N:
        stop[i]=N-stop_index
for i in range(N):
    k=ue[i]
    a=bisect.bisect_right(naka,k)
    if a!=N:
        for j in range(a,N):
            count+=stop[j]
print(count)


Fixed submission:
import bisect
N=int(input())
ue=list(map(int,input().split()))
naka=list(map(int,input().split()))
sita=list(map(int,input().split()))
sita.sort(),naka.sort(),ue.sort()
count2=[0]*N
count1=[0]*N
for i in range(N):
    m = naka[i]
    k=naka[i]
    small=bisect.bisect_left(ue,k)
    count1[i]=small
    big = bisect.bisect_right(sita, m)
    count2[i]=N-big
ans=0
for i in range(N):
    ans+=count1[i]*count2[i]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
A,B,C,D,E,F=list(map(int,input().split()))
max=0
satou=0
water=0
for i in range(31):
    for j in range(31):
        for k in range(101):
            for l in range(101):
                a=100*A*i+100*B*j
                s=C*k +D*l
                if a+s<=F and (a/100)*E>=s and a!=0:
                    if max<s/(a+s):
                        max=s/(a+s)
                        water=a+s
                        satou=s

print(water,satou)

Fixed submission:
A,B,C,D,E,F=list(map(int,input().split()))
max=0
satou=0
water=0
for i in range(31):
    for j in range(31):
        for k in range(101):
            for l in range(101):
                a=100*A*i+100*B*j
                s=C*k +D*l
                if a==0:break
                if a+s<=F and (a/100)*E>=s :
                    if max<100*s/(a+s):
                        max=100*s/(a+s)
                        water=a+s
                        satou=s
if satou>0:
    print(water,satou)
else:
    print(100*A,0)
Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]+=tarou-height2[j]+1

        if ans > cost
            ans = cost

print(ans)

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

print(ans)

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

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


print(ans)

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

print(ans)