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



Sample input:
6 1 2 2
2 1 1 3 0 2


Sample output:
5

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
import numpy as np
A = list(map(int,input().split()))
A = np.array(sorted(A,reverse=True))
ans = P
x = A[P-1]
y = N-1
for i in range(P,N):
  a = A[i]
  if x==a:
    ans+=1
  else:
    y=i
    break

if y==N-1:
  print(ans)
z = sum(A[P-1:y])
for i in range(y,N):
  a = A[i]
  d = a+M
  if x<=d:
    b = N-i+P-1
    c = M*(V-b)
    e = z+c
    f = d*(i-P+1)
    if f>=e:
      ans+=1
      
  z+=a
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
import numpy as np
A = list(map(int,input().split()))
A = np.array(sorted(A,reverse=True))
ans = P
x = A[P-1]
y = N-1
for i in range(P,N):
  a = A[i]
  if x==a:
    ans+=1
  else:
    y=i
    break

if y==N-1:
  print(N)
  exit()
z = sum(A[P-1:y])
for i in range(y,N):
  a = A[i]
  d = a+M
  if x<=d:
    b = N-i+P-1
    c = M*(V-b)
    e = z+c
    f = d*(i-P+1)
    if f>=e:
      ans+=1
      
  z+=a
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, V, P = map(int, input().strip().split())
A = sorted(list(map(int, input().strip().split())),reverse=True)
ans=P

sum_=[0]*N
sum_[0]=A[0]
for i in range(1,N):
    sum_[i]=(sum_[i-1]+A[i])

for i in range(P,N):
    thre=A[i]+M
    if thre < A[P-1]:
        continue

    remain=V-(P-1)-(N-i)
    if remain <0:
        ans+=1
    # elif sum(A[P-1:i])+M*remain <= thre*(i-1-(P-1)+1):
    elif sum_[i-1]-sum_[P-2]+M*remain <= thre*(i-1-(P-1)+1):
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


def main():
    N, M, V, P = MI()
    A = MI()
    # N, M, V, P = 10**5, 10*19, 10**4, 10**4
    # A = [i for i in range(N)]
    MAP = {}
    A = sorted(A, reverse=True)
    AH = A[:P]
    AH_min = AH[-1]
    AT = []
    cur = 0
    sent_c = 0
    for i in range(P, N):
        if AH_min == A[i]:
            sent_c += 1
        # else:
        if A[i] not in MAP:
            MAP[A[i]] = cur
        cur += 1
        AT.append(A[i])
    # print(AH, AT)
    # print('M=', M, 'V=', V)
    ans = len(AH)
    AT_len = len(AT)
    # print(AT, MAP)
    # show(len(AT), V - len(AH) + 1)

    _V = V - len(AH) + 1
    # print('_V', _V, V, AH_min)
    for i in range(AT_len):
        if AH_min == AT[i]:
            ans += 1
        elif AH_min <= (AT[i] + M):
            remaining = AT_len - (MAP[AT[i]] + 1)
            # print('ans=', ans, 'i=', i, 'remain=', remaining + 1, 'V=', _V, 'AH_MIN=', AH_min, 'MAP=', MAP[AT[i]])
            if remaining + 1 >= _V:
                ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M, V, P = map(int, input().strip().split())
A = sorted(list(map(int, input().strip().split())),reverse=True)
ans=P

sum_=[0]*N
sum_[0]=A[0]
for i in range(1,N):
    sum_[i]=(sum_[i-1]+A[i])

for i in range(P,N):
    thre=A[i]+M
    if thre < A[P-1]:
        continue

    remain=V-(P-1)-(N-i)
    if remain <=0:
        ans+=1
    # elif sum(A[P-1:i])+M*remain <= thre*(i-1-(P-1)+1):
    elif sum_[i-1]-sum_[P-2]+M*remain <= thre*(i-1-(P-1)+1):
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


def main():
    N, M, V, P = MI()
    A = MI()
    # N, M, V, P = 10**5, 10*19, 10**4, 10**4
    # A = [i for i in range(N)]
    MAP = {}
    A = sorted(A, reverse=True)
    AH = A[:P]
    AH_min = AH[-1]
    AT = []
    cur = 0
    sent_c = 0
    for i in range(P, N):
        if AH_min == A[i]:
            sent_c += 1
        # else:
        if A[i] not in MAP:
            MAP[A[i]] = cur
        cur += 1
        AT.append(A[i])
    # print(AH, AT)
    # print('M=', M, 'V=', V)
    ans = len(AH)
    AT_len = len(AT)
    # print(AT, MAP)
    # show(len(AT), V - len(AH) + 1)

    if len(AT) <= V - len(AH) + 1:
        # print(len(AT), V - len(AH) + 1)
        print(ans + sent_c)
    else:
        _V = V - len(AH) + 1
        # print('_V', _V, V, AH_min)
        for i in range(AT_len):
            if AH_min == (AT[i] + M):
                ans += 1
            elif AH_min <= (AT[i] + M):
                remaining = AT_len - (MAP[AT[i]] + 1)
                # print('i=', i, 'remain=', remaining + 1, 'V=', _V, 'AH_MIN=', AH_min, 'MAP=', MAP[AT[i]])
                if remaining + 1 >= _V:
                    ans += 1

        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


def main():
    N, M, V, P = MI()
    A = MI()
    # N, M, V, P = 10**5, 10*19, 10**4, 10**4
    # A = [i for i in range(N)]
    MAP = {}
    A = sorted(A, reverse=True)
    AH = A[:P]
    AH_min = AH[-1]
    AT = []
    cur = 0
    sent_c = 0
    for i in range(P, N):
        if AH_min == A[i]:
            sent_c += 1
        # else:
        if A[i] not in MAP:
            MAP[A[i]] = cur
        cur += 1
        AT.append(A[i])
    print(AH, AT)
    # print('M=', M, 'V=', V)
    ans = len(AH)
    AT_len = len(AT)
    # print(AT, MAP)
    # show(len(AT), V - len(AH) + 1)

    if len(AT) <= V - len(AH) + 1:
        # print(len(AT), V - len(AH) + 1)
        print(ans + sent_c)
    else:
        _V = V - len(AH) + 1
        # print('_V', _V, V, AH_min)
        for i in range(AT_len):
            if AH_min == (AT[i] + M):
                ans += 1
            elif AH_min <= (AT[i] + M):
                remaining = AT_len - (MAP[AT[i]] + 1)
                print('i=', i, 'remain=', remaining + 1, 'V=', _V, 'AH_MIN=', AH_min, 'MAP=', MAP[AT[i]])
                if remaining + 1 >= _V:
                    ans += 1

        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M, V, P = map(int, input().strip().split())
A = sorted(list(map(int, input().strip().split())),reverse=True)
ans=0

sum_=[0]*N
sum_[0]=A[0]
for i in range(1,N):
    sum_[i]=(sum_[i-1]+A[i])

for i in range(N):
    thre=A[i]+M
    if thre < A[P-1]:
        continue

    remain=V-(P-1)-(N-i)
    if remain <=0:
        ans+=1
    # elif sum(A[P-1:i])+M*remain <= thre*(i-1-(P-1)+1):
    elif sum_[i-1]-sum_[P-2]+M*remain <= thre*(i-1-(P-1)+1):
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, V, P = map(int, input().strip().split())
A = sorted(list(map(int, input().strip().split())),reverse=True)
ans=0

sum_=[0]*N
sum_[0]=A[0]
for i in range(1,N):
    sum_[i]=(sum_[i-1]+A[i])

for i in range(N):
    thre=A[i]+M
    if thre < A[P-1]:
        continue

    remain=V-(P-1)-(N-i)
    if remain <=0:
        ans+=1
    # elif sum(A[P-1:i])+M*remain <= thre*(i-1-(P-1)+1):
    elif sum_[i-1]-sum_[P-2]+M*remain <= thre*(i-1-(P-1)+1):
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
input = sys.stdin.readline
N, M, V, P = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)
d = dd(int)
for x in a: d[x] += 1
#print(d)
ks = sorted(d.keys())
#print(ks)
def check(x):
  b = a + []
  isit = 0
  t = 0
  for i in range(V):
    if isit == 0 and x == b[i]:
      b[i] = -float("inf")
      isit = 1
  b.sort()
  for k in ks:
    if k > x: break
    t += d[k] * M
  #t += M * (P - 1)
  p = 0
  c = 0
  for i in range(N - 1, 0, -1):
    if x <= b[i] < x + M: 
      if p <= P: t += M
      else: t += max(M, x + M - b[i])
    if c != b[i]:
      c = b[i]
      p += 1
      if p >= P and (c > x + M):
        return False
  #print(x, t, b, V * M)

  return t >= V * M
ok = N
ng = -1
m = (ok + ng) // 2
while ok - ng > 1:
  m = (ok + ng) // 2
  if check(a[m]):
    ok = m
  else: ng = m
res = N
ng -= 1
if N <= 10:
  for i in range(N - 1):
    if check(a[i]) == False:
      ng = max(ng, a[i])
    #print(a[i], check(a[i]), ng)

for k in ks:
  if k <= ng: res -= d[k]
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N,M,V,P = map(int, input().split())
a = list(map(int, input().split()))
A = sorted(a)[::-1]
           
right = P
left = N-1

while left - right !=1:
    i = (left+right)//2
    b = (V-((P-1)+(N-i)))
    target = A[i]+M
        
    if b<=0:
        if A[P-1]<=target:
            right = i
    else:
        if target<A[P-1]:
            left = i
        else:
            Q_sum=sum(A[P-1:i])
            len_Q =i-(P-1)
            if target*len_Q-Q_sum>=b*M:
                right = i
            else:
                left =i


print(right+1)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

N,M,V,P = LI()
A = LI()
A.sort()

def check(x):
    if (N-x)<=P:
        return True
    x_score = A[x] + M
    if (V-x-P) <= 0:
        return(A[N-P]<=x_score)

    total_v = (V-x-P)*M

    if(A[N-P]>x_score):
        return False

    for i in range(x+1,N-P+1):
        total_v -= min(M,(x_score - A[i]))
        if(total_v<0):
            return True
    return False

def main():
    if(check(0)):
        print(N)
        return

    isOK = N-1
    isNG = 0
    while(isOK-isNG>1):
        mid = (isOK+isNG)//2
        if(check(mid)):
            isOK = mid
        else:
            isNG = mid
    print(N-isOK)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
input = sys.stdin.readline
N, M, V, P = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)
d = dd(int)
for x in a: d[x] += 1
#print(d)
ks = sorted(d.keys())
#print(ks)
def check(x):
  b = a + []
  isit = 0
  t = 0
  for i in range(V):
    if isit == 0 and x == b[i]:
      b[i] = -float("inf")
      isit = 1
  b.sort()
  for k in ks:
    if k > x: break
    t += d[k] * M
  #t += M * P
  p = 0
  c = 0
  for i in range(N - 1, 0, -1):
    if x <= b[i] < x + M: t += max(M, x + M - b[i])
    if c != b[i]:
      c = b[i]
      p += 1
      if p >= P and (c > x + M):
        return False
  #print(t, b)

  return t >= V * M
ok = N
ng = -1
m = (ok + ng) // 2
while ok - ng > 1:
  m = (ok + ng) // 2
  if check(a[m]):
    ok = m
  else: ng = m
res = N
ng -= 1
if N <= 10:
  for i in range(N - 1):
    if check(a[i]) == False:
      ng = max(ng, a[i])
    #print(a[i], check(a[i]), ng)

for k in ks:
  if k <= ng: res -= d[k]
print(res)

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, m, v, p = MAP()
a = LIST()
a = sorted(a, reverse=True)

def hantei(i):
    if i < p: return True
    if a[p-1] > a[i] + m: return False
    if ((i-p+1)*(a[i]+m)-sum(a[p-1:i]))+m*(p-1+n-i) < m*v: return False
    return True

def check(x):
    if (n-x)<=p:
        return True
 
    x_score = a[x]+m
    total_v = (v-x-p)*m
 
    if(a[n-p]>x_score):
        return False
 
    for i in range(x+1,n-p+1):
        total_v -= min(m,(x_score -a[i]))
    if total_v <= 0:
        return True
    else:
        return False

right = n-1
left = 0

while right-left > 1:
    mid = (right+left)//2
    if check(mid):
        left = mid
    else:
        right = mid

print(right)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
A = [int(i) for i in input().split()]

#A = [[int(i),x]for x,i in enumerate(input().split())]

#順位を得るため重複をなくし降順

target = P

con = 0

for i in range(N):
  x = A[i]
  x = x + M
  Y = [k for k in A if k < x]
  y = len(Y)

  #振り分け範囲より、xの上限を下回る数が多い場合
  ##…xがAのなかでP番目に入れればカウントする
  if y > V - 1:
    bigger = len([b for b in (A) if b > x])
    if target >= bigger:
      con += 1
  #そうでなければ
  ##xの上限数を下回るYの数y個分振り分けた後
  ##xの上限数を同等または上回る値の個数zが、
  ##入っているべき順位より小さければカウントする
  else:
    Z = [j for j in A if j >= x]
    z = len(Z)

    if V - y - 1 < z and target - 1 > z:
      con += 1


print(con)

Submission status: Wrong Answer

Solution code:
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

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]
            if sum >= j * m:
                p1 = 1
                break
            if k == n - p:
                p1 = 0

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

Submission status: Wrong Answer

Solution code:
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

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

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

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, m, v, p = MAP()
a = LIST()
a = sorted(a, reverse=True)

def hantei(i):
    if i < p: return True
    if a[p-1] > a[i] + m: return False
    if ((i-p+1)*(a[i]+m)-sum(a[p-1:i]))+m*(p-1+n-i) < m*v: return False
    return True

right = n-1
left = 0

while right-left > 1:
    mid = (right+left)//2
    if hantei(mid):
        left = mid
    else:
        right = mid

print(right)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

N,M,V,P = LI()
A = LI()
A.sort()

def check(x):
    if (N-x)<=P:
        return True

    x_score = A[x]+M
    total_v = (V-x-P)*M

    if(A[N-P]>x_score):
        return False

    for i in range(x+1,N-P+1):
        total_v -= min(M,(x_score - A[i]))
        if(total_v<0):
            return True
    return False

def main():
    if(check(0)):
        print(N)
        return

    isOK = N-1
    isNG = 0
    while(isOK-isNG>1):
        mid = (isOK+isNG)//2
        if(check(mid)):
            isOK = mid
        else:
            isNG = mid
    print(N-isOK)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m, v, p = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    acm = list(itertools.accumulate(a))
    target = bisect.bisect_right(a, a[-p]) - 1
    for i in range(n - 1):
        bl = bisect.bisect_left(a, a[i])
        br = bisect.bisect_right(a, a[i])
        if br < n:
            dif = acm[target] - acm[br-1] - a[i] * (target - br + 1)
            print(a[i], target, br, dif, a, acm)
            if dif <= min((n - v), (target - br) + 1) * m and a[target] - a[i] <= m:
                print(n - i)
                exit()
    print(n - bisect.bisect_left(a, a[target]))


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, m, v, p = MAP()
a = LIST()
a = sorted(a, reverse=True)

def hantei(i):
    if i < p: return True
    if a[p-1] > a[i] + m: return False
    if i <= n-v+p-1: return True
    if ((i-p)*(a[i]+m)-sum(a[p-1:i])) >= m*(v-p-n+i+1): return True
    return False

right = n-1
left = 0

while right-left > 1:
    mid = (right+left)//2
    if hantei(mid):
        left = mid
    else:
        right = mid

print(right)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m, v, p = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    acm = list(itertools.accumulate(a))
    target = bisect.bisect_right(a, a[-p]) - 1
    for i in range(n - 1):
        bl = bisect.bisect_left(a, a[i])
        br = bisect.bisect_right(a, a[i])
        if br < n:
            dif = acm[target] - acm[br-1] - a[i] * (target - br + 1)
            if dif <= min((n - v), (target - br) + 1) * m:
                print(n - i)
                exit()
    print(n - bisect.bisect_left(a, a[target]))


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m, v, p = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    acm = list(itertools.accumulate(a))
    target = bisect.bisect_right(a, a[-p]) - 1
    for i in range(n - 1):
        bl = bisect.bisect_left(a, a[i])
        br = bisect.bisect_right(a, a[i])
        if br < n and target > br - 1:
            dif = acm[target] - acm[br-1] - a[i] * (target - br + 1)
            if dif <= min((n - v), (target - br) + 1) * m:
                print(n - i)
                exit()
    print(n - bisect.bisect_left(a, a[target]))


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n,m,v,p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
def binary_search(left, right):
    if right - left <= 1:
        return left
    middle = (left + right) // 2
    if check(middle):
        return binary_search(middle, right)
    else:
        return binary_search(left, middle)

def check(target):
    used_vote = (p + n - target - 1) * m
    sum_vote = 0
    for i in range(target-1, p-2, -1):
        sum_vote += (a[target] + m) - a[i]
    if sum_vote >= (m * v - used_vote): 
        return True
    else:
        return False
print(binary_search(0,n) + 1)

Submission status: Wrong Answer

Solution code:
n,m,v,p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
p-=1

a.sort(reverse=True)

ans=0
s = sum(a)

def safe(index):
    if a[p]>a[index]+m:
        return False
    i=0
    while a[i]>=a[index]+m:
        i+=1
    if i>index:
        return True
    intg = 0
    i = max(i,p)
    while i<index:
        intg += a[index] + m - a[i]
        i+=1
    intg += (len(a)-1-index)*m
    print(intg,m*(v-1))
    return intg >= m*(v-1) - p*m

left = 0
right = len(a)-1
ans = int((right - left)/2)
if safe(right):
    ans = right
else:
    j = safe(ans)
    while not(j and not safe(ans+1)):
        if j:
            left = ans
        else:
            right = ans
        ans = left + int((right - left)/2)
        j = safe(ans)
print(ans+1)

for i in range(len(a)):
    print(a[i],safe(i))

Submission status: Wrong Answer

Solution code:
import math
N, M, V, P = tuple(map(int, input().split()))
A = list(map(int, input().split()))

S = tuple(reversed(sorted(A))) #O(N)

cnt = 0
sum_memo = {}
su = 0
b = 0
b_rank = 0

for n, i in enumerate(S): # Loop O(N)
    if b != i:
        n_rank = n+1
        rank = n
    else: 
        rank = b_rank
    b = i
    b_rank = rank
    if n >= P - 1:
        su += S[n]
        sum_memo[n] = su
    
    if P > rank:
        cnt += 1
    else:
        xx = P-1 + N - rank +1
        if xx < V:
            if rank in sum_memo.keys():
                sum_val = sum_memo[rank]
            else:
                sum_val = sum(S[P-1:rank])
                sum_memo[rank] = sum_val
            x = max(math.ceil((sum_val+(V-xx)*M)/(rank-P+1)), S[P-1])
        else:
            x = S[P-1]
                    
        y = i + M
        if x <= y:
            cnt += 1
        else:
            break
print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,v,p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
p-=1

a.sort(reverse=True)

ans=0
s = sum(a)
v -= p*m

def safe(index):
    if a[p]>a[index]+m:
        return False
    i=0
    while a[i]>=a[index]+m:
        i+=1
    if i>index:
        return True
    intg = 0
    i = max(i,p)
    while i<index:
        intg += a[index] + m - a[i]
        i+=1
    intg += (len(a)-1-index)*m
    return intg >= m*(v-1)

left = 0
right = len(a)-1
ans = int((right - left)/2)
if safe(right):
    ans = right
else:
    j = safe(ans)
    while not(j and not safe(ans+1)):
        if j:
            left = ans
        else:
            right = ans
        ans = left + int((right - left)/2)
        j = safe(ans)
print(ans+1)
"""
for i in range(len(a)):
    print(a[i],safe(i))
"""

Submission status: Wrong Answer

Solution code:
import sys
bbn=1000000007
	
#+++++

class mmm:
	def __init__(self):
		self.n, self.m, self.v ,self.p = map(int, input().split())
		self.al=list(map(int, input().split()))
		self.al.sort(reverse=True)
		r=0
		m=self.al[0]
		self.diffl=[0]*self.n
		for i,v in enumerate(self.al):
			r+=m-v
			self.diffl[i]=r
		self.border=self.al[self.p-1]
			
		pa(self.diffl)
		pa(self.al)
	
	def is_ok(self,ni):
		if ni <= self.p-1:
			return True
		vv=self.al[ni]
		if vv + self.m < self.border:
			return False
		
		nv=self.v-((self.p-1)+(self.n-ni))
		if nv <= 0:
			return True
		
		ss=nv*self.m
		di=self.diffl[ni-1]-self.diffl[self.p-1]

		div=self.al[0]-self.border
		din=di-(div*(ni-1-(self.p-1)))+(vv+self.m-self.border)*(ni-1-(self.p-1))
		pa((ni,di,din,div,ss,nv))
		
		if din > ss:
			return True
		return False
	
	
	def solve(self):
		ok=0
		if self.is_ok(self.n-1):
			return self.n
		ng=self.n-1
		
		while ng-ok>=2:
			mm=(ng+ok)//2
			if self.is_ok(mm):
				ok=mm
			else:
				ng=mm
			#print(ok,ng,mm)
		return ok+1

		
def main():
	a=mmm()
	ret=a.solve()
	print(ret)
	
	
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, m, v, p = MAP()
a = LIST()
a = sorted(a, reverse=True)
def hantei(i):
    if i < p: return True
    if a[p-1] > a[i] + m: return False
    if i <= n-v+p-1: return True
    if ((i-p+1)*(a[i]+m)-sum(a[p-1:i])) >= m*(v-p-n+i+1): return True
    return False

right = n-1
left = 0

while right-left > 1:
    mid = (right+left)//2
    if hantei(mid):
        left = mid
    else:
        right = mid

print(right)

Submission status: Wrong Answer

Solution code:
N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A = list(reversed(sorted(A)))

def ok(x):
    if x + M < A[P]:
        return False
    sc = M * V
    for a in A[:P-1]:
        sc = sc - M
    for a in A[P-1:]:
        sc = sc - min(M, x + M - a)
    return sc <= 0

l, r = -1, N
while r - l > 1:
    m = l + (r - l) // 2
    if not ok(A[m]):
        r = m
    else:
        l = m
print(r)

Submission status: Wrong Answer

Solution code:
n,m,v,p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
p-=1

a.sort(reverse=True)

ans=0
s = sum(a)

def safe(index):
    if a[p]>a[index]+m:
        return False
    i=0
    while a[i]>=a[index]+m:
        i+=1
    if i>index:
        return True
    intg = 0
    while i<index:
        intg += a[index] + m - a[i]
        i+=1
    intg += (len(a)-1-index)*m + min(v-1,p)*m
    return intg >= m*(v-1)

left = 0
right = len(a)-1
ans = int((right - left)/2)
if safe(right):
    ans = right
else:
    j = safe(ans)
    while not(j and not safe(ans+1)):
        if j:
            left = ans
        else:
            right = ans
        ans = left + int((right - left)/2)
        j = safe(ans)
print(ans+1)
"""
for i in range(len(a)):
    print(a[i],safe(i))
"""

Submission status: Wrong Answer

Solution code:
import itertools
n,m,v,p = map(int, input().split())
a = list(map(int, input().split()))
ans = p
a.sort(reverse=True)
def binary_search(left, right):
    if right - left <= 1:
        return left
    middle = (left + right) // 2
    if check(middle):
        return binary_search(middle, right)
    else:
        return binary_search(left, middle)

def check(target):
    used_vote = (p + n - target - 1) * m
    sum_vote = 0
    for i in range(target-1, p-2, -1):
        sum_vote += (a[target] + m) - a[i]
    if sum_vote >= m * v - used_vote: 
        return True
    else:
        return False

print(binary_search(0,n) + 1)

Submission status: Wrong Answer

Solution code:
N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A = list(reversed(sorted(A)))

def ok(x):
    if x + M < A[P]:
        return False
    sc = M * V
    for a in A[:P-1]:
        sc = sc - M
    for a in A[P:]:
        sc = sc - min(M, x + M - a)
    return sc <= 0

l, r = -1, N
while r - l > 1:
    m = l + (r - l) // 2
    if not ok(A[m]):
        r = m
    else:
        l = m
print(r)