Problem: 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?

Submission attempts (in order):

# 3 2 2 1 1 0






















# V = N - V
# 3 2 2 1 1 0




# [3 2 1][3 1 2]

# [5 4 3 2 1]

# [3 2 1]











# [5 [4 3 2 1]]->[5 [1 3 2 1]]->[5 [1 1 2 1]]->[5 [1 1 1 1]]




N,M,V,P = map(int,input().split())
A = sorted(list(map(int,input().split())), reverse = True)

if V <= N//2:
  
  border = A[P-1]
  print("border",border)
  
  ind = -1
  for i in range(P-1,N):
    if A[i] < border:
      ind = i
      break
  if ind == -1: 
    print(N)
    exit(0)
  B = A[ind:] 

  allvote = V * M
  newborder = border
  for i in range(len(B)): 
    if (border - B[i]) > M:
#      print("border - B[i]",border - B[i])
      
      break

    if border - B[i] >= M and border - B[i] <= allvote: 

      vote = border - B[i]
      allvote -= vote
      newborder = B[i]
    else:
      
#      print("not vote")
      break
  ans = N - len(B)
  for i in range(len(B)):
    if B[i] >= newborder:
      ans += 1
  print(ans)

else:
  

  V = N - V 
  border = A[P-1] 
#  print("border",border)
  ok_ind = -1
  for i in range(len(A)):
    if A[i] == border:
      ok_ind = i
      break
  if ok_ind == -1: 
    print(N)
    exit(0)
  
  B = A[ok_ind:]
  Bsum = [[B[0],1]]
  for i in range(1,len(B)):
    if B[i] == Bsum[-1][0]:
      Bsum[-1][1] += 1
    else:
      Bsum += [[B[i],1]]
#  print(Bsum)
  allvote = V * M
  
  
  ok = Bsum[0][0] - 0
  ng = Bsum[0][0] - (M + 1)
  while abs(ng - ok)>1:
    x = abs(ng + ok)//2
    

    can = True
    alls = allvote
    for i in range(len(Bsum)):
      if Bsum[i][0] <= x:
        break
      else:
        need = (Bsum[i][0] - x) * Bsum[i][1]

        alls -= need

        if alls < 0:

          can = False
          break
    if can:
      ok = x

    else:
      ng = x

  
  newborder = ok
#  print("ok",ok,"newborder",newborder)
  ans = N - len(B)

  for i in range(len(B)):
    if B[i] >= newborder:
      ans += 1
  print(ans)












import sys
readline = sys.stdin.readline

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


border = A[P - 1]



ok = -1
ng = N

def isOk(x):
  
  if (x + 1) <= P:
    return True
  if A[x] + M < border:
    return False
  
  base = A[x] + M
  
  
  vote = (P - 1) * M
  
  for i in range(P - 1,len(A)):
    limit = max(base - A[i], 0)
    can = min(limit, M)
    vote += can
  if vote >= M * V:
    return True
  return False

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


Verdicts: Wrong Answer

Accepted