Buggy python submission (verdict: Runtime Error):
import bisect

N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

cnt = 0
A.sort()
    
if V <= P:
    for i in range(N-P):
        if A[i]+M < A[N-P]:
            cnt += 1
        else:
            break
else:
    for i in range(N-P):
        if i+P >= V:
            if A[i]+M < A[N-P]:
                cnt += 1
            else:
                break
        else:
            num = M*V-M*(i+P)#さばかないといけない数
            j = bisect.bisect_left(A, A[i]+M)
            if A[j] == A[i]+M:
                j -= 1
            j = min(j, N-P)
            if j > V-P:
                cnt += 1
                break
            L = A[i+1:j+1]
            t = 0
            while L[V-P-i-1] < A[i]+M and num > 0:
                s = A[i]+M-L[V-P-i-1]
                for k in range(V-P-i):
                    L[k] += s
                L.sort()
                num -= s*(V-P-i)
            if num > 0:
                cnt += 1
            else:
                break
print(N-cnt)

Fixed submission:
N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort(reverse=True)

S = [0]*N
t = 0
for i in range(N):
    t += A[i]
    S[i] = t

s = M*(N-V)
l = P
r = N
if P == 1:
    a = 0
else:
    a = S[P-2]
while l < r:
    m = (r-l)//2 + l
    if A[P-1]-M > A[m]:
        r = m
        continue
    if S[m-1]-a-(m-P+1)*A[m] > s:
        r = m
        continue
    l = m+1
    
print(l)

Buggy python submission (verdict: Runtime Error):
import bisect

N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

cnt = 0
A.sort()
    
if V <= P:
    for i in range(N-P):
        if A[i]+M < A[N-P]:
            cnt += 1
        else:
            break
else:
    for i in range(N-P):
        if i+P >= V:
            if A[i]+M < A[N-P]:
                cnt += 1
            else:
                break
        else:
            num = M*V-M*(i+P)#さばかないといけない数
            j = bisect.bisect_left(A, A[i]+M)
            if A[j] == A[i]+M:
                j -= 1
            j = min(j, N-P)
            if j > V-P:
                cnt += 1
                break
            L = A[i+1:j+1]
            t = 0
            while L[V-P-i-1] < A[i]+M and num > 0:
                s = A[i]+M-L[V-P-i-1]
                for k in range(V-P-i):
                    L[i] += s
                L.sort()
                num -= s*(V-P-i)
            if num > 0:
                cnt += 1
            else:
                break
print(N-cnt)

Fixed submission:
N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort(reverse=True)

S = [0]*N
t = 0
for i in range(N):
    t += A[i]
    S[i] = t

s = M*(N-V)
l = P
r = N
if P == 1:
    a = 0
else:
    a = S[P-2]
while l < r:
    m = (r-l)//2 + l
    if A[P-1]-M > A[m]:
        r = m
        continue
    if S[m-1]-a-(m-P+1)*A[m] > s:
        r = m
        continue
    l = m+1
    
print(l)

Buggy python submission (verdict: Runtime Error):
import bisect

N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

cnt = 0
A.sort()
S = [0]*(N+1)
for i in range(N):
    S[i+1] = S[i]+A[i]
    
if V <= P:
    for i in range(N-P):
        if A[i]+M < A[N-P]:
            cnt += 1
        else:
            break
else:
    for i in range(N-P):
        if i+P >= V:
            if A[i]+M < A[N-P]:
                cnt += 1
            else:
                break
        else:
            num = M*V-M*(i+P)#さばかないといけない数
            j = bisect.bisect_left(A, A[i]+M)
            if A[j] == A[i]+M:
                j -= 1
            j = min(j, N-P)
            
            t = (j-i)*(A[i]+M)-(S[j+1]-S[i+1])#空いてる数
            t -= t%(V-P-i)
            if num > t:
                cnt += 1
            else:
                break
                
print(N-cnt)

Fixed submission:
N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort(reverse=True)

S = [0]*N
t = 0
for i in range(N):
    t += A[i]
    S[i] = t

s = M*(N-V)
l = P
r = N
if P == 1:
    a = 0
else:
    a = S[P-2]
while l < r:
    m = (r-l)//2 + l
    if A[P-1]-M > A[m]:
        r = m
        continue
    if S[m-1]-a-(m-P+1)*A[m] > s:
        r = m
        continue
    l = m+1
    
print(l)