Buggy python submission (verdict: Runtime Error):
n,m,v,p=map(int,input().split())
a=list(map(int,input().split()))
a.sort()

left=0
right=n-p-1

while  right -left >1:
    mid =(left+right)//2
    if a[mid]+m < a[n-p]:
        left=mid
    else:
        j=v-(mid+1+p-1)
        if j <= 0:
            right=mid
        else:
            sum=0
            for k in range(mid+1,n-p+1,1):
                if m+a[mid]-a[k] < 0:
                    left = mid
                    break
                sum+=a[mid]+m-a[k]
                if sum >= j*m:
                    right=mid
                    break
                if k==n-p:
                    left=mid


p1 = 0
mid = left
if a[mid] + m < a[n - p]:
        p1 = 0
else:
        j = v - (mid + 1+p-1)
        if j <= 0:
            p1 = 1
        else:
            sum = 0
            for k in range(mid + 1, n+p+1, 1):
                if m + a[mid] - a[k] < 0:
                    p1 = 0
                    break
                sum += a[mid] + m - a[k]
                print("s", sum, j * m, a[k])
                if sum >= j * m:
                    p1 = 1
                    break
                if k == n-p:
                    p1 = 0
p2 = 0
mid = right
if a[mid] + m < a[n - p]:
        p1 = 0
else:
        j = v - (mid + 1 + p - 1)
        if j <= 0:
            p2 = 1
        else:
            sum = 0
            for k in range(mid + 1, n + p + 1, 1):
                if m + a[mid] - a[k] < 0:
                    p2 = 0
                    break
                sum += a[mid] + m - a[k]
                print("s", sum, j * m, a[k])
                if sum >= j * m:
                    p2 = 1
                    break
                if k == n - p:
                    p2 = 0

if left==right:
    if p1==1:
        print(n-left)
    else:
        print(n-left-1)
else:
    if p1==1:
        print(n-left)
    elif p2==1:
        print(n-left-1)
    else:
        print(n-left-2)

Fixed submission:
n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

left = 0
right = n - p -1

while right - left >=0:
    mid = (left + right) // 2
    if a[mid] + m < a[n - p]:
        left = mid + 1
    else:
        j = v - (mid + 1 + p - 1)
        if j <= 0:
            right = mid-1
        else:
            sum = 0
            for k in range(mid + 1, n - p + 1, 1):
                if m + a[mid] - a[k] < 0:
                    left = mid + 1
                    break
                sum += a[mid] + m - a[k]
                if sum >= j * m:
                    right = mid-1
                    break
                if k == n - p:
                    left = mid + 1

print(n-left)

Buggy python submission (verdict: Runtime Error):
n,m,v,p=map(int,input().split())
a=list(map(int,input().split()))
a.sort()

left=0
right=n-p-1

while  right -left >1:
    mid =(left+right)//2
    if a[mid]+m < a[n-p]:
        left=mid
    else:
        j=v-(mid+1+p-1)
        if j <= 0:
            right=mid
        else:
            sum=0
            for k in range(mid+1,n-p+1,1):
                if m+a[mid]-a[k] < 0:
                    left = mid
                    break
                sum+=a[mid]+m-a[k]
                if sum >= j*m:
                    right=mid
                    break
                if k==n-p:
                    left=mid

if left==right:
    print(n-left)
elif right-left==1:
    p1 = 0
    mid = left
    if a[mid] + m < a[n - p]:
        p1 = 0
    else:
        j = v - (mid + 1+p-1)
        if j <= 0:
            p1 = 1
        else:
            sum = 0
            for k in range(mid + 1, n+p+1, 1):
                if m + a[mid] - a[k] < 0:
                    p1 = 0
                    break
                sum += a[mid] + m - a[k]
                print("s", sum, j * m, a[k])
                if sum >= j * m:
                    p1 = 1
                    break
                if k == n-p:
                    p1 = 0
    p2 = 0
    mid = right
    if a[mid] + m < a[n - p]:
        p1 = 0
    else:
        j = v - (mid + 1 + p - 1)
        if j <= 0:
            p2 = 1
        else:
            sum = 0
            for k in range(mid + 1, n + p + 1, 1):
                if m + a[mid] - a[k] < 0:
                    p2 = 0
                    break
                sum += a[mid] + m - a[k]
                print("s", sum, j * m, a[k])
                if sum >= j * m:
                    p2 = 1
                    break
                if k == n - p:
                    p2 = 0

    if p1==1:
        print(n-left)
    elif p2==1:
        print(n-left-1)
    else:
        print(n-left-2)

Fixed submission:
n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

left = 0
right = n - p -1

while right - left >=0:
    mid = (left + right) // 2
    if a[mid] + m < a[n - p]:
        left = mid + 1
    else:
        j = v - (mid + 1 + p - 1)
        if j <= 0:
            right = mid-1
        else:
            sum = 0
            for k in range(mid + 1, n - p + 1, 1):
                if m + a[mid] - a[k] < 0:
                    left = mid + 1
                    break
                sum += a[mid] + m - a[k]
                if sum >= j * m:
                    right = mid-1
                    break
                if k == n - p:
                    left = mid + 1

print(n-left)