Problem p02598 (python)

Fixed submission:
#import numpy as np
#import collections
#import math

#f = open('./input_E.txt')
#
#N,K = map(int, f.readline().split())
#A = list(map(int, f.readline().split()))

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

A.sort(reverse=True)

def nibu(small,big,KK,AA):
    mid = (small+big)//2
    k_list = [(x-1)//mid for x in AA]
    sum_k = sum(k_list)
    if sum_k <= KK: #達成可能 -> 答えはもっと小さい
        return small,mid # 不可能,可能
    else:
        return mid,big # 不可能,可能


if K==0:
    ans = A[0]
else:    
    lmin = 0
    lmax = A[0]
    
    while lmax - lmin >= 2:
        lmin, lmax = nibu(lmin,lmax,K,A)
    
    ans = lmax
    
print(ans)

Buggy submission (verdict: Runtime Error):
#import numpy as np
#import collections
#import math

f = open('./input_E.txt')

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

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

A.sort(reverse=True)

def nibu(small,big):
    mid = (small+big)//2
#    k_list = [x//mid for x in A2]
    k_list = [x//mid for x in A]
    sum_k = sum(k_list)
    if sum_k <= K: #達成可能 -> 答えはもっと小さい
        return small,mid # 不可能,可能
    else:
        return mid,big # 不可能,可能


if N==1 or K==0:
    ans = A[0]
else:    
#    All_length = sum(A)
#    KK = K+N-1
#    lmin = int(All_length/(KK+1))
    lmin = 0
    
#    A2 = [x for x in A if x > lmin]
    
#    Kper1 = K//len(A2)
    
#    lmax = int(A[0]/(Kper1+1)) + 1
    lmax = A[1]
    
    while lmax - lmin >= 2:
        lmin, lmax = nibu(lmin,lmax)
    
    ans = lmax
    
print(ans)

Buggy submission (verdict: Runtime Error):
#import numpy as np
#import collections
#import math

#f = open('./input_E.txt')
#
#N,K = map(int, f.readline().split())
#A = list(map(int, f.readline().split()))

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

if K == 0:
    ans = max(A)
else:
    A.sort(reverse=True)
    
    All_length = sum(A)
    KK = K+N-1
    lm = All_length/(KK+1)
    
    k_list = [int(Ai//lm) for Ai in A]
    X_list = [Ai%lm for Ai in A]
    Xmax = max(X_list)

    zero_k = 0
    for i in range(1,N+1):
        if k_list[-i] == 0:
            zero_k +=1
        else:
            break
    
    sum_k = sum(k_list)-(N-zero_k)
    res_cut = K-sum_k
    
    
    if res_cut > 0:
#        think = min(7*(N//res_cut)+1,res_cut)
        ans_kouho = []
#        for i in range(-zero_k,0):
#            ans_kouho.append(A[i])
#        ans_kouho.extend(X_list)
        for i in range(res_cut):
#        for i in range(think):
            if Xmax >= i*lm:
                for j in range(N-zero_k):
#                    ans_kouho.append((X_list[j]-i*lm)/k_list[j]+1)
                    ans_kouho.append(A[j]/(k_list[j]+i))
        
        ans_kouho.sort(reverse=True)
        ans = ans_kouho[res_cut]
        ans = int(ans+1)
    else:
        ans = int(lm+1)
print(ans)

Buggy submission (verdict: Runtime Error):
#import numpy as np
#import collections
#import math

#f = open('./input_E.txt')
#
#N,K = map(int, f.readline().split())
#A = list(map(int, f.readline().split()))

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

if K == 0:
    ans = max(A)
else:
    A.sort(reverse=True)
    
    All_length = sum(A)
    KK = K+N-1
    lm = All_length/(KK+1)
    
    k_list = [int(Ai//lm) for Ai in A]
    X_list = [Ai%lm for Ai in A]
    
    sum_k = sum(k_list)
    res_cut = KK-sum_k
    
    zero_k = 0
    for i in range(1,N+1):
        if k_list[-i] == 0:
            zero_k +=1
        else:
            break
    think = N//res_cut+1
    
    if res_cut > 0:
        ans_kouho = []
        for i in range(-zero_k,0):
            ans_kouho.append(X_list[i])
#        for i in range(res_cut):
        for i in range(think):
            for j in range(N-zero_k):
                ans_kouho.append((X_list[j]-i*lm)/k_list[j])
        
        ans_kouho.sort(reverse=True)
        ans = ans_kouho[res_cut] + lm
        ans = int(ans)+1
    else:
        ans = int(lm)+1
print(ans)

Buggy submission (verdict: Runtime Error):
#import numpy as np
#import collections
#import math

#f = open('./input_E.txt')
#
#N,K = map(int, f.readline().split())
#A = list(map(int, f.readline().split()))

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

A.sort(reverse=True)

All_length = sum(A)
KK = K+N-1
lm = All_length/KK

k_list = [int(Ai//lm) for Ai in A]
X_list = [Ai%lm for Ai in A]

sum_k = sum(k_list)
res_cut = KK-sum_k

zero_k = 0
for i in range(1,N+1):
    if k_list[-i] == 0:
        zero_k +=1
    else:
        break

if res_cut > 0:
    ans_kouho = []
    for i in range(-zero_k,0):
        ans_kouho.append(X_list[i])
    for i in range(res_cut):
        for j in range(N-zero_k):
            ans_kouho.append((X_list[j]-i*lm)/k_list[j])
    
    ans_kouho.sort(reverse=True)
    ans = ans_kouho[res_cut] + lm
    ans = int(ans)+1
else:
    ans = int(lm)+1
print(ans)