qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from collections import deque

N = int(input())
c = list(str(input()))
dw = deque()
dr = deque()

for i, c in enumerate(c, 1):
    if c == 'W':
        dw.append(i)
    else:
        dr.appendleft(i)

ans = 0
while dw:
    w = dw.popleft()
    r = dr.popleft()
    print(w, r)
    if w < r:
        ans += 1

    else:
        break
print(ans)

Fixed code:
from collections import deque

N = int(input())
c = list(str(input()))
dw = deque()
dr = deque()

for i, c in enumerate(c, 1):
    if c == 'W':
        dw.append(i)
    else:
        dr.appendleft(i)

ans = 0
while dw and dr:
    w = dw.popleft()
    r = dr.popleft()
    if w < r:
        ans += 1

    else:
        break
print(ans)
Buggy code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
c = [1 if _ == 'R' else 0 for _ in input()]
b = n-sum(c)

w=0
r=0

for i in range(n):
  if c[i] == 0 and i <= b:
    w+=1
  if c[i] == 1 and i > b:
    r+=1

print(max(w,r))




Fixed code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
c = [1 if _ == 'R' else 0 for _ in input()]
b = sum(c)

w=0
r=0

for i in range(n):
  if c[i] == 0 and i < b:
    r+=1
  if c[i] == 1 and i >= b:
    w+=1
 

print(max(w,r))
Buggy code:
n = int(input())
c = list(map(int, input().split()))
left = 0
cnt = 0
right = len(c) - 1
while left < n and right >=0 and left < right:
	while left < n and c[left] != 'W':
		left += 1
	while right >= 0 and c[right] != 'R':
		right -= 1
	if left< n and right >= 0 and left<right:
		c[left], c[right] = c[right], c[left]
		cnt += 1
		left += 1
		right -=1 
print(cnt)



Fixed code:
n = int(input())
c = list(input())
left = 0
cnt = 0
right = len(c) - 1
while left < n and right >=0 and left < right:
    while left < n and c[left] != 'W':
        left += 1
    while right >= 0 and c[right] != 'R':
        right -= 1
    if left< n and right >= 0 and left<right:
        c[left], c[right] = c[right], c[left]
        cnt += 1
        left += 1
        right -=1 
print(cnt)
Buggy code:
n = int(input())
s1 = input()
s2 = s1[::-1]
i1 = 0
i2 = 0
ans = 0
while i1 < n:
  if s1[i1]=='W':
    while i2 < n:
      if s2[i2]=='R':
        ans += 1
        i2 += 1
        break
      elif i1+i2 >= n-2:
        break
      else:
        i2 += 1
  i1 += 1
  if i1+i2 >= n-2:
    break
    
print(ans)

Fixed code:
n = int(input())
s1 = input()
s2 = s1[::-1]
i1 = 0
i2 = 0
ans = 0
while i1 < n:
  if s1[i1]=='W':
    while i2 < n:
      if s2[i2]=='R':
        ans += 1
        i2 += 1
        break
      elif i1+i2 >= n-2:
        break
      else:
        i2 += 1
  if i1+i2 >= n-2:
    break
  i1 += 1
print(ans)
Buggy code:
n = int(input())
c = input()


cnt = 0
for i in range(n):
  w_idx = c.find("W")
  r_idx = c.rfind("R")
  if w_idx < r_idx:
    c = c[w_idx+1:r_idx]
    cnt += 1

print(cnt)

Fixed code:
n = int(input())
c = input()


cnt = 0
for i in range(n):
  w_idx = c.find("W")
  r_idx = c.rfind("R")
  if w_idx < r_idx and w_idx != -1:
    c = c[w_idx+1:r_idx]
    cnt += 1

print(cnt)
Buggy code:
N = int(input())
RW = input()

j = N-1
count = 0
for i in range(N):
    if RW[i]=='W':
        while i<j:
            if RW[j]=='R':
                count += 1
                break
            j -= 1

print(count)

Fixed code:
N = int(input())
RW = input()

j = N-1
count = 0
for i in range(N):
    if RW[i]=='W':
        while i<j:
            if RW[j]=='R':
                count += 1
                j -= 1
                break
            j -= 1

print(count)
Buggy code:
read = sys.stdin.read
from collections import deque
def main():
    n = int(input())
    a = input()
    if not 'WR' in a:
        print(0)
        sys.exit()
    a = list(a)
    rnum = a.count('R')
    r = 0
    for i1 in range(rnum):
        r += a[i1] == 'W'
    for j1 in range(n - rnum):
        r += a[rnum + j1] == 'R'

    r = r // 2
    print(r)


if __name__ == '__main__':
    main()


Fixed code:
import sys
read = sys.stdin.read
def main():
    n = int(input())
    a = input()
    if not 'WR' in a:
        print(0)
        sys.exit()
    a = list(a)
    rnum = a.count('R')
    r = 0
    for i1 in range(rnum):
        r += a[i1] == 'W'
    for j1 in range(n - rnum):
        r += a[rnum + j1] == 'R'
    r = r // 2
    print(r)
if __name__ == '__main__':
    main()
Buggy code:
N = int(input())
S = input()
n=0
if S.find("W")==-1 or S.find("R")==-1:
  print(0)
else:
  while S.find("W")<=S.rfind("R"):
    S = S[S.find("W")+1:S.rfind("R")]
    n+=1
  print(n)

Fixed code:
N = int(input())
S = input()
n=0
if S.find("W")==-1 or S.find("R")==-1:
  print(0)
else:
  while S.find("W")<S.rfind("R") and S.find("W")>=0:
    S = S[S.find("W")+1:S.rfind("R")]
    n+=1
  print(n)
Buggy code:
N = int(input())

c = input()

k = -1
for i in range(N):
    if c[i] == 'W' and k == -1:
        k = i 

if k == -1 or k == N-1:
    print(0)
else:
    w = [0] * N
    r = [0] * N

    if c[0] == 'W':
        w[0] = 1
    if c[N-1] == 'R':
        r[N-1] = 1
    for i in range(1,N):
        if c[i] == 'W':
            w[i] = w[i-1] + 1
        else:
            w[i] = w[i-1]

        if c[N-i-1] == 'R':
            r[N-i-1] = r[N-i] + 1
        else:
            r[N-i-1] = r[N-i]

    print(w)
    print(r)
    for i in range(N):
        if w[i] == r[i]:
            print(w[i])
    

Fixed code:
N = int(input())

c = input()

k = -1
for i in range(N):
    if c[i] == 'W' and k == -1:
        k = i 

if k == -1 or k == N-1:
    print(0)
else:
    w = [0] * N
    r = [0] * N

    if c[0] == 'W':
        w[0] = 1
    if c[N-1] == 'R':
        r[N-1] = 1
    for i in range(1,N):
        if c[i] == 'W':
            w[i] = w[i-1] + 1
        else:
            w[i] = w[i-1]

        if c[N-i-1] == 'R':
            r[N-i-1] = r[N-i] + 1
        else:
            r[N-i-1] = r[N-i]
    
    if w[N-1] == N:
        print(0)
    else:
        for i in range(N-1):
            if w[i] == r[i+1]:
                print(w[i])
                break
Buggy code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in B:
        if c == 'R':
            red_cnt += 1

    for i in range(red_cnt+1):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)

Fixed code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in C:
        if c == 'R':
            red_cnt += 1

    for i in range(red_cnt):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)
Buggy code:
n = int(input())
c = input()
l = 0
j = 0
a = [0]*n
b = [0]*n
for i in range(n):
  if(c[i]=='W'):
    a[l] =i+1
    l +=1
l = 0
for i in range(n-1,0,-1):
  if(c[i]=='R'):
    b[j] = i+1
    j +=1
j = 0
while(a[l] < b[l]):
      a[l]=0
      b[l]=0
      l+=1
      j+=1
print(j)

Fixed code:
n = int(input())
c = input()
l = 0
j = 0
a = [0]*n
b = [0]*n
for i in range(n):
  if(c[i]=='W'):
    a[l] =i+1
    l +=1
l = 0
for i in range(n-1,0,-1):
  if(c[i]=='R'):
    b[j] = i+1
    j +=1
j = 0
while(a[l] < b[l] and not(a[l]==0 or b[l] == 0)):
      a[l]=0
      b[l]=0
      l+=1
      j+=1
print(j)
Buggy code:
N = int(input())
S = input()

c = Counter(S)
S_target = 'R' * c['R'] + 'W' * c['W']

ans = 0
for a, b in zip(S, S_target):
    if b == 'W':
        break
    if a != b:
        ans += 1
print(ans)


Fixed code:
from collections import Counter

N = int(input())
S = input()

c = Counter(S)
S_target = 'R' * c['R'] + 'W' * c['W']

ans = 0
for a, b in zip(S, S_target):
    if b == 'W':
        break
    if a != b:
        ans += 1
print(ans)
Buggy code:
X = input()
count_w, n, count = X.count('W'), len(X), 0
for i in range(n - 1, n -1 - count_w, -1):
  count += 1 if X[i] == 'R' else 0
  
print(count)

Fixed code:
n, X = int(input()), input()
count_w, count = X.count('W'), 0
for i in range(n - 1, n -1 - count_w, -1):
  count += 1 if X[i] == 'R' else 0
  
print(count)
Buggy code:
n = int(input())
c = input()
r=0
w=0
p=0
count = 0
for i in range(len(c)):
    if c[i]=="R":
        r+=1
    else:
        w+=1
white = []

for i in range(len(c)):
    if c[i]=="R":
        r-=1
        
    else:
        white.append(r)
    if r==0:
        break
if len(white)==0:
    print(0)
if white[0]==0:
    print(0)
else:
    for i in range(len(white)):
        if white[-i-1]>=len(white)-i:
            print(len(white)-i)
            break
print(white)

Fixed code:
n = int(input())
c = input()
r=0
w=0
p=0
count = 0
for i in range(len(c)):
    if c[i]=="R":
        r+=1
    else:
        w+=1
white = []

for i in range(len(c)):
    if c[i]=="R":
        r-=1
        
    else:
        white.append(r)
    if r==0:
        break
if len(white)==0:
    print(0)
elif white[0]==0:
    print(0)
else:
    for i in range(len(white)):
        if white[-i-1]>=len(white)-i:
            print(len(white)-i)
            break
Buggy code:
n,k=map(int,input().split())
A=[int(i) for i in input().split()]


def f(A,i):
    return sum([(a-1)//i for a in A])
l=-1
r=10**9+1
while r-l>1:
    m = (l+r)//2
    #print(m)
    x = f(A,m)
    if x<=k:
        r=m
    else:
        l=m
    #print(x,k,l,r,m)
print(m)

Fixed code:
n,k=map(int,input().split())
A=[int(i) for i in input().split()]


def f(i):
    if i<=0: return False
    if i>= 10**9: return True
    x = sum([(a-1)//i for a in A])
    if x<=k:
        return True
    else:
        return False
l=-1
r=10**9
while r-l>1:
    m = (l+r)//2
    if f(m):
        r=m
    else:
        l=m
    #print(l,m,r)
print(r)
Buggy code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = 0
r = 10 ** 9

while r-l > 1 :
    m = (l+r)//2 
    b = list(map(lambda x: -(-x//m) - 1 ,a))
    ans = sum(b)
    if ans <= k :
        r = m
    else :
        l = m
    print(l,r)

print(r)

         



Fixed code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = 0
r = 10 ** 9

while r-l > 1 :
    m = (l+r)//2 
    b = list(map(lambda x: -(-x//m) - 1 ,a))
    ans = sum(b)
    if ans <= k :
        r = m
    else :
        l = m

print(r)
Buggy code:
from math import ceil
n,k = map(int,input().split())
a = list(map(int,input().split()))

print(n,k,a)

def cut(x):
  t = 0
  for i in a:
    if i <= x:
      continue
    t += ceil(i / x) - 1
  return t <= k

l,r=0,10**9
while l+1 < r:
  x = l + (r-l)//2
  if cut(x):
    r = x
  else:
    l = x
print(r)

Fixed code:
from math import ceil
n,k = map(int,input().split())
a = list(map(int,input().split()))

def cut(x):
  t = 0
  for i in a:
    if i <= x:
      continue
    t += ceil(i / x) - 1
  return t <= k

l,r=0,10**9
while l+1 < r:
  x = l + (r-l)//2
  if cut(x):
    r = x
  else:
    l = x
print(r)
Buggy code:
from math import ceil

m=1
M=10**9

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

def OK(k):
    if sum([ceil(a/k) for a in A])<=K:
        return True
    else:
        return False

while M-m>1:
    p=(m+M)//2
    if OK(p):
        m=p
    else:
        M=p
print(m)

Fixed code:
from math import floor

m=0
M=10**9+1

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

def OK(p):
    if sum([(a-1)//p for a in A])<=K:
        return True
    else:
        return False

while M-m>1:
    p=(m+M)//2
    if OK(p):
        M=p
    else:
        m=p
print(M)
Buggy code:
n, k = map(int, input().split())
arr = np.array(list(map(int, input().split())))

l = 1
r = max(arr)

while l + 1 < r:
    m = (l + r) // 2

    cut = 0
    for x in arr:
        cut += (x - 1) // m

    if cut > k:
        l = m
    else:
        r = m

print(r)

Fixed code:
n, k = map(int, input().split())
arr = list(map(int, input().split()))

l = 0
r = max(arr)

while l + 1 < r:
    m = (l + r) // 2

    cut = 0
    for x in arr:
        cut += (x - 1) // m

    if cut > k:
        l = m
    else:
        r = m

print(r)
Buggy code:
n,k=map(int,input().split())
a=list(map(int,input().split()))

l=0
r=1e9+5

while l+1!=r:
    m = (l+r)//2
    cnt = 0
    for x in a:
        cnt += (x-1)//m
    if cnt <= k:
        r = m
    else:
        l = m
print(r)

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

l=0
r=1e9+5

while l+1!=r:
    m = (l+r)//2
    cnt = 0
    for x in a:
        cnt += (x-1)//m
    if cnt <= k:
        r = m
    else:
        l = m
print(int(r))
Buggy code:
import sys
input = sys.stdin.readline
import math

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

N, K = MI()
mylist = LI()
length = len(mylist)

high = max(mylist)+1
low = 0
mid = (high + low) / 2

while high - low >= 0.001:
    temp = 0
    for i in mylist:
        if i % mid == 0:
            temp += (i // mid) - 1
        else:
            temp += i // mid
    if temp > K:
        low = mid
    else:
        high = mid
    mid = (high + low) / 2


print(math.ceil(low)+1)

Fixed code:
import sys
input = sys.stdin.readline
import math

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

N, K = MI()
mylist = LI()
length = len(mylist)

high = max(mylist)+1
low = 0
mid = (high + low) / 2

while high - low >= 0.001:
    temp = 0
    for i in mylist:
        if i % mid == 0:
            temp += (i // mid) - 1
        else:
            temp += i // mid
    if temp > K:
        low = mid
    else:
        high = mid
    mid = (high + low) / 2

result = math.ceil(low)


num2 = result+10**(-8)
temp= 0
for i in mylist:
    if i % num2 == 0:
        temp += (i // num2) - 1
    else:
        temp += i // num2
if temp > K:
    print(result+1)
else:
    print(result)
Buggy code:
def is_ok(X):
    cnt = 0
    for a in A:
        cnt += a//X
    if cnt <= k:
        return True
    else:
        return False


def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

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

print(meguru_bisect(0,max(A)+1))


Fixed code:
def is_ok(X):
    cnt = 0
    for a in A:
        cnt += (a-1)//X
    if cnt <= k:
        return True
    else:
        return False


def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

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

print(meguru_bisect(0,max(A)+1))
Buggy code:
from math import ceil

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


def is_ok(num):
  t = 0
  for log in logs:
    if log <= num:
      continue
    t += ceil(log / n) - 1
  return t <= k

ng = 0
ok = 10 ** 9

while ok - ng > 1 :
  m = (ok + ng) // 2
  
  if is_ok(m):
    ok = m
  else:
    ng = m

print(ok)

Fixed code:
from math import ceil

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


def is_ok(num):
  t = 0
  for log in logs:
    if log <= num:
      continue
    t += ceil(log / num) - 1
  return t <= k

ng = 0
ok = 10 ** 9

while ok - ng > 1 :
  m = (ok + ng) // 2
  
  if is_ok(m):
    ok = m
  else:
    ng = m

print(ceil(ok))
Buggy code:
"""
AtCoder :: Beginner Contest 174 :: Logs
https://atcoder.jp/contests/abc174/tasks/abc174_e
"""
import sys


def main():
    """Main program."""
    log_count, cut_count = (int(i) for i in sys.stdin.readline().split())
    logs = [int(i) for i in sys.stdin.readline().split()]
    high = max(logs)
    low = 1
    soln = high
    # Binary search for the answer.
    while low < high:
        mid = (low + high) // 2
        cuts_needed = 0
        if mid:
            cuts_needed = sum(log // mid for log in logs)
        if cuts_needed <= cut_count:
            soln = min(soln, mid)
            high = mid - 1
        else:
            low = mid + 1
    print(soln)


if __name__ == '__main__':
    main()


Fixed code:
"""
AtCoder :: Beginner Contest 174 :: Logs
https://atcoder.jp/contests/abc174/tasks/abc174_e
"""
import sys
from math import ceil


def main():
    """Main program."""
    log_count, cut_count = (int(i) for i in sys.stdin.readline().split())
    logs = [int(i) for i in sys.stdin.readline().split()]
    high = max(logs)
    low = 1
    soln = high
    # Binary search for the answer.
    while low <= high:
        mid = (low + high) // 2
        cuts_needed = sum([int(ceil(log / mid)) - 1 for log in logs])
        if cuts_needed <= cut_count:
            soln = min(soln, mid)
            high = mid - 1
        else:
            low = mid + 1
    print(soln)


if __name__ == '__main__':
    main()
Buggy code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
result = max(a)
a = [x * 2 for x in a]
left = 1
right = result
med = left + (right - left) // 2
while left <= right:
    counter = sum([x // med for x in a])
    if counter <= k:
        result = med
        right = med - 1
    else:
        left = med + 1
    med = left + (right - left) // 2
print((result + 1) // 2)


Fixed code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [x * 2 for x in a]
result = max(a)
left = 1
right = result
med = left + (right - left) // 2
while left <= right:
    counter = sum([(x - 1) // med for x in a])
    if counter <= k:
        result = med
        right = med - 1
    else:
        left = med + 1
    med = left + (right - left) // 2
print((result + 1) // 2)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

def is_ok(x):
    cnt = 0
    for a in A:
        cnt += a // x
    if cnt <= K:
        return True
    else:
        return False

ng = 0
ok = 10**9+1
while ok - ng > 1:
    mid = (ok + ng) // 2
    if is_ok(mid):
        ok = mid
    else:
        ng = mid
print(ok)

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

def is_ok(x):
    cnt = 0
    for a in A:
        cnt += a // x
        if a % x == 0:
            cnt -= 1
    return cnt <= K

ng = 0
ok = 10**9
while ok - ng > 1:
    mid = (ok + ng) // 2
    if is_ok(mid):
        ok = mid
    else:
        ng = mid
print(ok)
Buggy code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()

def counter(x):
  cnt=0
  for i in a:
    cnt+=i//x
    
  if cnt>k:
    return True
  
  else:
    return False

high=a[-1]+1
low=0
while (high-low)>1:
  mid =(high+low)//2
  if counter(mid):
    low=mid

  else:
    high=mid

print(high)

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

def counter(x):
  cnt=0
  for i in a:
    cnt+=(i-1)//x
    
  if cnt<=k:
    return True
  
  else:
    return False

high=a[-1]
low=0
while (high-low)>1:
  mid =(high+low)//2
  if counter(mid):
    high=mid

  else:
    low=mid

print(high)
Buggy code:
N, M = input().strip().split()
n, k = [int(N), int(M)]
a = list(map(int, input().strip().split()))
le = 0
ri = 10000000000
while ri - le > 1:
    mid = (le + ri) 
    num = 0
    for i in a:
        num += (i-1) 
    if num <= k:
        ri = mid
    else:
        le = mid
print(ri)


Fixed code:
N, M = input().strip().split()
n, k = [int(N), int(M)]
a = list(map(int, input().strip().split()))
le = 0
ri = 10000000000
while ri - le > 1:
    mid = (le + ri)// 2
    num = 0
    for i in a:
        num += (i-1)// mid
    if num <= k:
        ri = mid
    else:
        le = mid
print(ri)
Buggy code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
if K==0:
  print(max(A))
  exit()
def f(x):
  cnt=0
  for i in A:
    if i%x==0:
      cnt+=i//x-1
    else:
      cnt+=(i//x)
  if cnt<=K:
    return True
  else:
    return False
ok=1
ng=1000000000
for i in range(50):
  mid=(ok+ng)//2
  if f(mid):
    ng=mid
  else:
    ok=mid
print(ok)


Fixed code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
if K==0:
  print(max(A))
  exit()
def f(x):
  cnt=0
  for i in A:
    if i%x==0:
      cnt+=i//x-1
    else:
      cnt+=(i//x)
  if cnt<=K:
    return True
  else:
    return False
ok=1
ng=1000000000
for i in range(50):
  mid=(ok+ng)//2
  if f(mid):
    ng=mid
  else:
    ok=mid
print(ng)
Buggy code:
from heapq import heappush, heappop
from math import ceil, floor
N, K = map(int, input().split())
A = [int(i) for i in input().split()]

ng, ok = 0, 10 ** 9
for _ in range(40) :
  mid = (ng + ok) / 2
  ret = 0
  for a in A :
    ret += floor(a / mid)
  if ret <= K :
    ok = mid
  else :
    ng = mid
  print(ok, ng)
    
print(ceil(ng))

Fixed code:
from heapq import heappush, heappop
from math import ceil, floor
N, K = map(int, input().split())
A = [int(i) for i in input().split()]

ng, ok = 0, 10 ** 9
for _ in range(50) :
  mid = (ng + ok) / 2
  ret = 0
  for a in A :
    ret += floor(a / mid)
  if ret <= K :
    ok = mid
  else :
    ng = mid
    
print(ceil(ng))
Buggy code:
import math
N, K=map(int,input().split())
A=list(map(int,input().split()))

def judge(ans,K,A):
    cut=0
    for i in range(len(A)):
        cut+=A[i]//ans
    if cut>K:
        return False
    else:
        return True


ansp=max(A)
ansm=min(A)
ans=(ansp+ansm)//2
d=1
while ansp-ansm>d:
    
    if judge(ans,K,A):
        ansp=ans
        
    else:
        ansm=ans
        
    ans=(ansp+ansm)//2
print(ansp)

Fixed code:
import math
N, K=map(int,input().split())
A=list(map(int,input().split()))

def judge(ans,K,A):
    cut=0
    for i in range(len(A)):
        cut+=(A[i]-1)//ans
    if cut>K:
        return False
    else:
        return True


ansp=max(A)
ansm=0
ans=(ansp+ansm)//2
d=1
while ansp-ansm>d:
    
    if judge(ans,K,A):
        ansp=ans
        
    else:
        ansm=ans
        
    ans=(ansp+ansm)//2
print(ansp)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

def isOK(x):
    cnt = 0
    for a in A:
        cnt += (a + x - 1) // x - 1
    return cnt <= K

left, right = 1, 10 ** 18
while right - left > 1:
    mid = (left + right) // 2
    if isOK(mid):
        left = mid
    else:
        right = mid
print(left)


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

def isOK(x):
    cnt = 0
    for a in A:
        cnt += (a + x - 1) // x - 1
    return cnt <= K

left, right = 0, 10 ** 10
while right - left > 1:
    mid = (left + right) // 2
    if isOK(mid):
        right = mid
    else:
        left = mid
print(right)
Buggy code:
import math

def can_split(a, t, k):
    m = 0
    for x in a:
        m += math.ceil(x / t) - 1
    return m <= k

n, k = map(int, input().split())
a = list(map(int, input().split()))
if k == 0:
    print(a[0])
    quit()

min_val = 10 ** 9
l, r = 0, max(a)
while l + 1 < r:
    m = (l + r) // 2
    if can_split(a, m, k):
        r = m
    else:
        l = m
print (r)

Fixed code:
import math

def can_split(a, t, k):
    m = 0
    for x in a:
        m += math.ceil(x / t) - 1
    return m <= k

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

min_val = 10 ** 9
l, r = 0, max(a)
while l + 1 < r:
    m = (l + r) // 2
    if can_split(a, m, k):
        r = m
    else:
        l = m
print (r)
Buggy code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
r = max(A)
l = 0
while l + 1 < r:
    mid = (l + r) // 2
    if sum([(a - 1) // mid for a in A]) >= k:
        l = mid
    else:
        r = mid

print(l)

Fixed code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
r=max(A)
l=0
while l+1<r:
    mid=(l+r)//2
    if sum([(a-1)//mid for a in A])>k:
        l=mid
    else:
        r=mid

print(r)