Buggy python submission (verdict: Runtime Error):
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]


for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break

    if i == n-1:
        ans = n

print(ans)

Fixed submission:
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]


for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    if l[i] + m < l[p - 1]:
        ans = i
        break
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break

    if i == n-1:
        ans = n

print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]

for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break
print(ans)


Fixed submission:
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]


for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    if l[i] + m < l[p - 1]:
        ans = i
        break
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break

    if i == n-1:
        ans = n

print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]

for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break
print(ans)


Fixed submission:
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]


for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    if l[i] + m < l[p - 1]:
        ans = i
        break
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break

    if i == n-1:
        ans = n

print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]

for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break
print('ans', ans)


Fixed submission:
n, m, v, p = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]

l = sorted(l, reverse=True)

# print(l)

s = [0]


for i in range(1, n+1):
    s.append(s[-1] + l[-i])
s = sorted(s, reverse=True)

# print(s)

for i in range(p, n):
    if l[i] + m < l[p - 1]:
        ans = i
        break
    x = (l[i] + m) * (i - p + 1) - (s[p - 1] - s[i])
    # print('i',i)
    # print('(l[i] + m)', (l[i] + m))
    # print('(i - p + 1)', (i - p + 1))
    # print('(s[p - 1] - s[i])', (s[p - 1] - s[i]))
    # print('x',x)
    y = 0
    if v >= (n - i) + (p - 1):
        y = v - (n - i) - (p - 1)

    # print('y * m', y * m)

    if x < y * m:
        ans = i
        break

    if i == n-1:
        ans = n

print(ans)