Problem:
 Score : 700 points    Problem Statement N problems are proposed for an upcoming contest. Problem i has an initial integer score of A_i points.  M judges are about to vote for problems they like. Each judge will choose exactly V problems, independently from the other judges,
and increase the score of each chosen problem by 1 .  After all M judges cast their vote, the problems will be sorted in non-increasing order of score, and the first P problems will be chosen for the problemset.
Problems with the same score can be ordered arbitrarily, this order is decided by the chief judge.  How many problems out of the given N have a chance to be chosen for the problemset?      Constraints  2 \le N \le 10^5  1 \le M \le 10^9  1 \le V \le N - 1  1 \le P \le N - 1  0 \le A_i \le 10^9         Input Input is given from Standard Input in the following format:  N  M  V  P  A_1  A_2  ...  A_N       Output Print the number of problems that have a chance to be chosen for the problemset.        Sample Input 1 6 1 2 2
2 1 1 3 0 2      Sample Output 1 5  If the only judge votes for problems 2 and 5 , the scores will be 2  2  1  3  1  2 .
The problemset will consist of problem 4 and one of problems 1 , 2 , or 6 .  If the only judge votes for problems 3 and 4 , the scores will be 2  1  2  4  0  2 .
The problemset will consist of problem 4 and one of problems 1 , 3 , or 6 .  Thus, problems 1 , 2 , 3 , 4 , and 6 have a chance to be chosen for the problemset. On the contrary, there is no way for problem 5 to be chosen.       Sample Input 2 6 1 5 2
2 1 1 3 0 2      Sample Output 2 3  Only problems 1 , 4 , and 6 have a chance to be chosen.       Sample Input 3 10 4 8 5
7 2 3 6 1 6 5 4 6 5      Sample Output 3 8

Solution (Python):
N,M,V,P = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
def possible(i):
    A_ = [A[i] for i in range(N)]
    rest = M*(V-1)
    A_[i] += M
    for j in range(i+1,N):
        if A_[j] <= A_[i]:
            d = min(rest,A_[i]-A_[j])
            A_[j] += d
            rest -= d
        else:
            d = min(rest,M)
            A_[j] += d
            rest -= d
        if rest == 0:
            break
    for j in range(i):
        d = min(rest,M)
        A_[j] += d
        rest -= d
        if rest == 0:
            break
    for j in range(N-1,i,-1):
        if A_[j] == A_[i]:
            d = min(rest,A[j]+M-A_[j])
            A_[j] += d
            rest -= d
        if rest == 0:
            break
    c = A_[i]
    A_.sort(reverse=True)
    return A_[P-1] <= c

l = 0
r = N-1
m = (l+r)//2
for i in range(20):
    if possible(m):
        r = m
    else:
        l = m
    m = (l+r)//2
if possible(m):
    ans = N-m
else:
    ans = N-m-1
print(ans)

Solution (Python):
import bisect
N,M,V,P=map(int,input().split())
A=list(map(int,input().split()))
A.sort()

def is_ok(ind):
  bigs = bisect.bisect_right(A,A[ind]+M)
  if N-bigs >= P:
    return False
  votes = M*(V-1)
  j = 0
  for i in range(N-1,-1,-1):
    if i==ind:
      continue
    if j < P-1:
      votes = max(votes-M,0)
    else:
      cur = min(A[ind]+M,A[i]+M,A[i]+votes)
      votes -= max(0,cur-A[i])
    j += 1
  if votes == 0:
    return True
  else:
    return False

lo = 0
hi = N
mid = (lo+hi)//2
while lo < hi:
  mid = (lo+hi)//2
  if is_ok(mid):
    hi = mid
  else:
    lo = mid+1
print(N-hi)

Solution (Python):
n,m,v,p = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
k = (v-n)*m
s = k
x = a[p-1]
for i in range(p,n):
  s += a[i-1]
  if a[i] + m < x or (i-p+1)*a[i] < s:
    print(i)
    exit()
print(n)

Solution (Python):
from bisect import *
n,m,v,p=map(int,input().split())
it=list(map(int,input().split()))
it.sort()
pre=[it[0]]
for i in range(1,n):
    pre.append(pre[i-1]+it[i])
ans=0
par=[i for i in range(n)]
for i in range(n-2,-1,-1):
    if it[i]==it[i+1]:
        par[i]=par[i+1]


for i in range(n):
    
    if it[i]+m<it[-p]:
        continue
    ind=n-(p-1)
    ind2=par[i]
    if ind2>=ind-1:
        ans+=1
        continue

    tot=m*v
    
    tot-=(n-ind)*m
    tot-=(ind2)*m
    tot-=m
    
    co=pre[ind-1]-pre[ind2]
    co=((it[i]+m)*(ind-1-ind2)-co)
    
    
    if co>=tot:
        ans+=1
print(ans)

Solution (Python):
from bisect import bisect_right
N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

A_m = sorted(set(A))
n = len(A_m)
ok = n-1
ng = -1
while ng+1 < ok:
    c = (ok+ng)//2
    a = A_m[c]
    idx = bisect_right(A, a)
    if idx > N-P:
        ok = c
        continue
    if M < A[-P]-a:
        ng = c
        continue
    s = 0
    for aa in A[-P:idx-1:-1]:
        s += aa-a
    if s > M*(N-V):
        ng = c
    else:
        ok = c
a = A_m[ok]
print(sum(a<=aa for aa in A))

Solution (Python):
n,m,v,p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
tg = a[p-1]
l = p-1
r = n
biggest = 0
while l<r:
    x = (l+r)//2
    if a[p-1]-a[x]<=m:
        border = v*m-(n-x+p-1)*m
        biggest = sum([a[x]+m-a[j] for j in range(p-1,x)])
    else:
        r = x
        continue
    if border > biggest:
        r = x
    elif border <= biggest:
        l = x+1
print(l)

Solution (Python):
N,M,V,P=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=0
pre_num=1
dsum=0
th=A[P-1]
for i in range(P,N):
    rest_m=M*(N-V)
    p=A[i]
    if th-p>M:break
    dsum+=(A[i-1]-p)*pre_num
    pre_num+=1
    rest_m-=dsum
    if rest_m<0:break
    ans+=1
print(P+ans)

Solution (Python):
def f(k):
    if k<=P:
        return True
    if A[k]+M<A[P]:
        return False
    num=A[k]+M
    zzz=M*V-(N-k+P)*M
    if zzz<=0:
        return True
    for j in range(P,k):
        zzz-=num-A[j]
    if zzz<=0:
        return True
    else:
        return False

N,M,V,P=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
A=[float("inf")]+A
low=1
high=N
while high-low>=1:
    mid=(high+low)//2
    if f(mid):
        if low==mid:
            if f(high):
                low=high
            else:
                high=low
        else:
            low=mid
    else:
        high=mid
print(low)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,M,V,P = map(int,readline().split())
A = np.array(read().split(),np.int64)

A.sort()

def test(i):
    """
    i 番の人を勝者にできる
    """
    
    if N - i <= P:
        return True
    
    me = A[i] + M
    C = np.zeros(N,np.int64)
    C = me - A
    C = np.minimum(C,M)
    C[N-P+1:] = M 
    if (C < 0).any():
        return False
    C[i] = 0
    x = C.sum()
    return C.sum() >= M * (V-1)

left = -1 
right = N-1 
while left + 1 < right:
    x = (left + right) // 2
    if test(x):
        right = x
    else:
        left = x

answer = N - right
print(answer)

Solution (Python):
N, M, V, P = map(int, input().split())
A = sorted([int(a) for a in input().split()])
t = A[-P]
def chk(k):
    if A[k] >= t: return 1
    u = A[k] + M
    if u < t:
        return 0
    s = sum([min(M, u - A[i]) for i in range(N-P+1)])
    if s >= M * (V - P + 1):
        return 1
    return 0

l, r = -1, N-P
while r - l > 1:
    m = (l+r) // 2
    if chk(m):
        r = m
    else:
        l = m

print(N - r)

Solution (Python):
N, M, V, P = map(int, input().split())
As = sorted(list(map(int, input().split())))[::-1]

capacity = M * (N-V)
def isOK(i):
    Ai = As[i]
    s = 0
    for j in range(P-1, i):
        d = As[j] - Ai
        if d > M:
            return False
        s += d
        if s > capacity:
            return False
    return True

ok = -1
ng = N
while ng - ok > 1:
    mid = ok + (ng - ok) // 2
    if isOK(mid):
        ok = mid
    else:
        ng = mid
print(ok+1)

Solution (Python):
N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

ok = N-1
ng = -1
mid = (ok+ng)//2
while ok - ng > 1:
    if mid >= N - P:
        ok = mid
        mid = (ok+ng)//2
        continue
    K = A[mid] + M
    if K < A[N - P]:
        ng = mid
        mid = (ok+ng)//2
        continue
    p = M * (V-P)
    flg = 0
    for i in range(mid):
        p -= M
        if p <= 0:
            flg = 1
            break
    if flg:
        ok = mid
        mid = (ok+ng)//2
        continue
    for i in range(mid+1, N - (P-1)):
        p -= K - A[i]
        if p <= 0:
            flg = 1
            break
    if flg:
        ok = mid
        mid = (ok+ng)//2
    else:
        ng = mid
        mid = (ok+ng)//2

print(N - ok)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()

def bisection(l,r,f,left=True,discrete=True):
    eps=1 if(discrete) else 10**-8
    if((not left)^f(r)): return r if(left) else r+1
    elif(left^f(l)): return l-1 if(left) else l
    while(r-l>eps):
        h=(l+r)//2 if(discrete) else (l+r)/2
        if((not left)^f(h)): l=h
        else: r=h
    return (l+r)/2 if(not discrete) else l if(left) else r

def resolve():
    n,m,v,p=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()

    
    def check(i):
        
        
        
        rest=(v-(i+1)-(p-1))*m
        s=A[i]+m
        for j in range(i+1,n-p+1):
            if(s<A[j]): return False
            rest-=s-A[j]

        return rest<=0

    print(n-bisection(0,n-1,check,left=False))
resolve()

Solution (Python):
import sys
readline = sys.stdin.readline

N, M, V, P = map(int, readline().split())
A = list(map(int, readline().split()))

A.sort(reverse = True)


ok = P-1
ng = N

while abs(ok-ng) > 1:
    med = (ok+ng)//2
    R = A[med] + M
    J = max(0, V - (N-med))*M
    res = 0
    for i in range(P-1):
        res += M
    for i in range(P-1, med):
        if R-A[i] < 0:
            res = -1
            break
        res += R-A[i]
    if res >= J:
        ok = med
    else:
        ng = med
print(ok+1)

Solution (Python):
N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
def C(x):
    a = A[x]
    m = a+M
    if m<A[N-P]:
        return False
    v = V-P
    if v<0:
        return True
    cnt = 0
    for ai in A[x+1:N-P+1]:
        cnt+=max(0, min(m-ai, M))
    for ai in A[:x]:
        cnt+=max(0, min(m-ai, M))
    if cnt>=M*v:
        return True
    else:
        return False
ok = N-1
ng = -1
while ok-ng>1:
    mid = (ok+ng)//2
    if C(mid):
        ok = mid
    else:
        ng = mid
print(N-ok)

Solution (Python):
from sys import stdin
def main():
    n, m, v, p = map(int, stdin.readline().split())
    a = map(int, stdin.readline().split())
    a.sort()
    s = [0] * (n + 1)
    for i, x in enumerate(a):
        s[i+1] = s[i] + x
    ans = 0
    for i in xrange(n):
        x = a[i] + m
        if x < a[-p]:
            continue
        r = v - p - i
        if r > 0:
            u = r * m
            if u <= x * (n - p - i) - s[n-p+1] + s[i+1]:
                ans += 1
        else:
            ans += 1
    print ans
main()