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())
a = list(map(int, input().split()))
a.sort()

def solve(ind):
    if a[ind]+m < a[-p]:
        return False
    thr = a[ind] + m
    votes=[]
    for i in range(n):
        if i<=ind or i>n-p:
            votes.append(m)
        else:
            votes.append(thr-a[i])
    sm = sum(votes)
    return sm >= m*v

ok=n-1
ng=0
while abs(ng - ok) > 1:
    mid = (ng + ok)//2
    if solve(mid):
        ok = mid
    else:
        ng = mid

print(n-ok)

Submission status: Wrong Answer

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

def solve(ind):
    thr = a[ind] + m
    votes=[]
    for i in range(n):
        if i<=ind or i>n-p:
            votes.append(m)
        else:
            votes.append(thr-a[i])
    sm = sum(votes)
    return sm >= m*v

ok=n-1
ng=0
while abs(ng - ok) > 1:
    mid = (ng + ok)//2
    if solve(mid):
        ok = mid
    else:
        ng = mid

print(n-ok)

Submission status: Wrong Answer

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

def solve(ind):
    thr = a[ind] + m
    if thr < a[p-1]:
        return False

    votes=[]
    for num in a:
        tmp = min(m,thr-num)
        votes.append(tmp)

    for i in range(n-p+1,n):
        votes[i] = m

    for vo in votes:
        if vo<0:
            return False

    return sum(votes) >= m*v

l=0
r=n
while r-l>1:
    mid=(l+r)//2

    if solve(mid):
        r=mid
    else:
        l=mid
print(n-r)

Submission status: Wrong Answer

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

#print(a)

def solve(ind):
    thr = a[ind] + m

    votes=[]
    for num in a:
        tmp = min(m,thr-num)
#        tmp = max(0,tmp)
        votes.append(tmp)

    for i in range(n-p+1,n):
        votes[i] = m

#    print(ind,thr,votes,sum(votes), sum(votes) > m*v)

    for vo in votes:
        if vo<0:
            return False

    return sum(votes) >= m*v

#for i in range(n):
#    solve(i)
#exit()


l=0
r=n
while r-l>1:
    mid=(l+r)//2

    if solve(mid):
        r=mid
    else:
        l=mid
#print(l,mid,r)
print(n-r)

Submission status: Wrong Answer

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

def solve(ind):
    thr = a[ind] + m

    votes=[]
    for num in a:
        tmp = min(m,thr-num)
        tmp = max(0,tmp)
        votes.append(tmp)

    for i in range(n-p+1,n):
        votes[i] = m
    return sum(votes) > m*v

l=0
r=n
while r-l>1:
    mid=(l+r)//2

    if solve(mid):
        r=mid
    else:
        l=mid
print(n-r)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M, V, P = mapint()
As = list(mapint())
As.sort(reverse=True)

threshold = As[P-1]
l, r = P, N
while l+1<r:
    half = (l+r)//2
    a = As[half]+M
    if a<threshold:
        r = half
    else:
        rest_sum = 0
        for i in range(P-1, N):
            if As[i]>=a:
                continue
            if i==half:
                continue
            rest_sum += min(M, a-As[i])
        if M*V > rest_sum+P*M:
            r = half
        else:
            l = half
print(l+1)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M, V, P = mapint()
As = list(mapint())
As.sort(reverse=True)

threshold = As[P-1]
l, r = P, N
while l+1<r:
    half = (l+r)//2
    a = As[half]+M
    if a<threshold:
        r = half
    else:
        rest_sum = 0
        for i in range(P-1, N):
            if As[i]>=a:
                continue
            rest_sum += min(M, a-As[i])
        if M*V > rest_sum+P*M:
            r = half
        else:
            l = half
print(l+1)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M, V, P = mapint()
As = list(mapint())
As.sort(reverse=True)

threshold = As[P-1]
l, r = P, N
while l+1<r:
    half = (l+r)//2
    a = As[half]+M
    if a<threshold:
        r = half
    else:
        rest_sum = 0
        for i in range(P-1, N):
            if As[i]>=a:
                continue
            rest_sum += min(M, a-As[i])
        if M*V > rest_sum+(P-1)*M:
            r = half
        else:
            l = half
print(l+1)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M, V, P = mapint()
As = list(mapint())
As.sort(reverse=True)

threshold = As[P]
l, r = P, N
while l+1<r:
    half = (l+r)//2
    a = As[half]+M
    if a<threshold:
        r = half
    else:
        rest_sum = 0
        for i in range(P, N):
            if As[i]>=a:
                continue
            rest_sum += min(M, a-As[i])
        if M*V > rest_sum+(P-1)*M:
            r = half
        else:
            l = half
print(l+1)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

"""
# 問題:N問、ジャッジ:M人
# M人のジャッジがそれぞれV問を選び、問題のスコアを１ずつあげる
# M人の投票の後、大きい方からP問が選ばれる
# 問題セットに選ばれる可能性があるのは何問あるか

M人全員が投票すれば選ばれやすくなる
選ばれるとは？
可能性がないものを数えた方が早いのでは
P番目以内にあれば無条件で通過
現在のP番目 <= A[i] + M
V <= Pなら
上からP - 1番目までのどれか + A[i]を加算させることでA[i]を強くできる　
V > Pなら？
上からP - 1番目までとA[i]を強化するとして、残りのV - P個は小さいものから順に選ぶ
A[i]を抜かせないようにしたい
A[i]より大きい数字も一緒に足される場合にはP以内に入れない
"""
N, M, V, P = getNM()
A = getList()
A.sort(reverse = True)

def judge(x):
    if x <= P - 1:
        return True
    if A[P - 1] > A[x] + M:
        return False
    # P - 1番目まで + 自身以降の数字についてはM個足す
    left = (V - (P - 1) - (N - x)) * M
    # P個目からx-1まで A[x] + Mを超えない分足す
    for i in range(P, x):
        left -= A[x] + M - A[i]

    return left <= 0

ok = -1
ng = N

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

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
A = sorted(list(map(int,input().split())))
A.insert(0,0)
if V>P:
    low = 0
    high = N
    while high-low>1:
        mid = (high+low)//2
        B = A[:]
        x = A[mid]+M
        d = 0
        for i in range(1,mid):
            d += min(x-A[i],M)
        cnt = (V-1)*M-d
        for i in range(N,mid+1):
            if cnt>=M:
                B[i] += M
                cnt -=M
            else:
                B[i] += cnt
                cnt = 0
                break
        num = 0
        for i in range(N,mid+1,-1):
            if B[i]>x:
                num += 1
        if num<P:
            high = mid
        else:
            low = mid
    print(N-high+1)
else:
    cnt = 0
    for i in range(1,N-P+1):
        if A[i]+M<A[N-P+1]:
            cnt += 1
    print(N-cnt)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M, V, P = mapint()
As = list(mapint())

r_As = As[:]
As.sort(reverse=True)
thre = As[P]

from bisect import bisect_right

if V<=P:
    ans = N
    for i in range(P+1, N):
        if thre>As[i]+M:
            ans -= 1
    print(ans)
else:
    ans = N
    rest = V-(P-1)
    for i in range(P+1, N):
        r_idx = bisect_right(r_As, As[i])
        if r_idx<rest:
            ans -= 1
        elif thre>As[i]+M:
            ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
n, m, v, p = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
thr = arr[p - 1]

ok, ng = 0, n

while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    # midに全力投票した時のスコア
    x = arr[mid] + m
    # 残りの投票数
    rest = m * (v - 1)

    # 全力投票で届かない場合は探索を続行
    if x < thr:
        ng = mid
        continue

    # 全力投票で届く場合、残りの票を上手く処理できるか
    for i, a in enumerate(arr):
        # idxがp未満の場合は最大まで積める
        if i < p and arr[i] > thr:
            rest -= m
        if i == mid:
            continue
        # idxがpより後ろの場合はmかx-aまで積める
        else:
            rest -= min(m, x - a)

    # 消化しきれていれば勝ち、そうでなければ負け
    if rest <= 0:
        ok = mid
    else:
        ng = mid

print(ok + 1)

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 judge(i):
	ok=min((n-i)+(p-1),n)
	if ok>=v:
		return a[i]+m>=a[p-1]
	border=a[i]+m
	lis=[x for x in a[p-1:i]]
	cnt=0
	for i in range(len(lis)):
		cnt+=min(border-lis[i],m)
	return cnt>=(v-ok)*m

l=0
r=n
while r-l>1:
	mid=(l+r)//2
	if judge(mid):
		l=mid
	else:
		r=mid
print(l+1)

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 judge(i):
	ok=(n-i-1)+(p-1)
	if ok>=v:
		return ok+m>=a[p-1]
	border=a[i]+m
	lis=[x for x in a[p-1:i]]
	cnt=(v-ok-1)*m
	for i in range(len(lis)):
		if border-lis[i]<0:
			return False
		r=min(border-lis[i],m)
		cnt-=r
	return cnt<=0

l=0
r=n
while r-l>1:
	mid=(l+r)//2
	if judge(mid):
		l=mid
	else:
		r=mid
print(l+1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect as list_bisect
max2 = lambda x,y: x if x > y else y
min2 = lambda x,y: x if x < y else y

def bisect(ng, ok, judge, eps=1):
    while abs(ng-ok) > eps:
        m = (ng+ok)//2
        if judge(m):
            ok = m
        else:
            ng = m
    return ok

N,M,V,P = map(int,input().split())
A = sorted(map(int,input().split()))
def judge(i):
    if i >= N-P:
        return True
    a = A[i]
    if a+M < A[-P]:
        return False

    r = 0
    for v in A[i+1:-P+1]:
        r += a+M-v
    return r >= (V-P-i)*M

i = bisect(-1,N-1,judge)
print(N-i)

Submission status: Wrong Answer

Solution code:
from bisect import bisect as list_bisect
max2 = lambda x,y: x if x > y else y
min2 = lambda x,y: x if x < y else y

def bisect(ng, ok, judge, eps=1):
    while abs(ng-ok) > eps:
        m = (ng+ok)//2
        if judge(m):
            ok = m
        else:
            ng = m
    return ok

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

def judge(i):
    if N <= P:
        return True
    a = A[i]
    if a+M < A[-P]:
        return False

    r = 0
    for v in A[:-P+1]:
        r += max2(min2(M,a+M-v),0)
    return r >= (V-P+1)*M

i = bisect(-1,N-1,judge)
print(N-i)

# 1:07

Submission status: Wrong Answer

Solution code:
# 【考察】
# 0-indexedで考える。Aは降順ソートして考える。
# まず、初期状態でTop Pに含まれる問題はすべて出題されうる。
# なので初期でTop q (q > P)の問題が出題されるか否かを考えていく。
# 最終的にTop Pに入れるか否かが問題なので、Top 1 ~ P-1の問題は無視。
# Top qの問題に毎回投票しつつ、Top P ~ q-1の問題になるべく投票しないようにしたい。
# そこで数列 A[P], A[P+1], ..., A[q-1] を考える。
# この中から最大でN-V個の数字を選んで1減らすことをM回繰り返し、
# すべてA[q]以下にすることができればTop qの問題は出題されうる。
# 当然A[P]-A[q] > Mだったら無理（出題されない）。
# 以後A[P]-A[q] <= Mとする。
# この時、(A[P]-A[q]) + (A[P+1]-A[q]) + ... + (A[q-1]-A[q]) <= M * (N-V) であればTop qのは出題可能。
# 整理すると A[P] + A[P+1] + ... + A[q-1] <= M * (N-V) + A[q] * (q-P) となるので、
# Aの累積和のデータを保持しておけば各qにおいてO(1)で出題可否が判定可能。


def main():
    N, M, V, P = list(map(int, input().split()))
    P -= 1  # to 0-indexed
    A = list(map(int, input().split()))
    A.sort(reverse=True)  # 降順ソート
    cumsum = [0] * N
    cumsum[0] = A[0]
    for i in range(1, N):
        cumsum[i] = cumsum[i - 1] + A[i]
    q = P + 1
    while q < N:
        left = cumsum[q - 1]
        if P > 0:
            left -= cumsum[P - 1]
        right = M * (N - V) + A[q] * (q - P)
        if left > right:
            print(q)
            return
        q += 1
    print(q)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("YES") if fl else print("NO")
import collections
import math
import itertools
import heapq as hq
def isok(a,idx):##適宜変更
    if a+m<ap:
        return False
    elif idx<=p:
        return True
    tmp = 0 #a+mを超えないように割り振れる最大値
    for i in range(n-1,-1,-1):
        if a+m<=A[i]:break
        tmp+= min(a+m - A[i],m)
    tmp-=m#自分自身
    #print(a,tmp)
    return  tmp>=(v-p)*m

def bisect(ls): ##valの関数isok(x,val)がTrueとなる一番右のindex を返す 全部Falseなら-1,Trueならlen(ls)-1
    ok = -1
    ng = len(ls)
    x = (ok+ng)//2
    while ng-ok>1:
        num = ls[x]
        if isok(num,x):
            ok = x
        else:
            ng = x
        x = (ok+ng)//2
    return ok ##一番右のTrueのindex  Trueの個数はok+1こ

n,m,v,p = ma()
A = lma();A.sort(reverse=True)
#print(A)
ap = A[p-1]
ans = bisect(A)+1
print(ans)

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("YES") if fl else print("NO")
import collections
import math
import itertools
import heapq as hq
def isok(a):##適宜変更
    if a+m<ap:
        return False
    tmp = 0 #a+mを超えないように割り振れる最大値
    for i in range(n-1,-1,-1):
        if a+m<=A[i]:break
        tmp+= min(a+m - A[i],m)
    tmp-=m#自分自身
    #print(a,tmp)
    return  tmp>(v-p)*m

def bisect(ls): ##valの関数isok(x,val)がTrueとなる一番右のindex を返す 全部Falseなら-1,Trueならlen(ls)-1
    ok = -1
    ng = len(ls)
    x = (ok+ng)//2
    while ng-ok>1:
        num = ls[x]
        if isok(num):
            ok = x
        else:
            ng = x
        x = (ok+ng)//2
    return ok ##一番右のTrueのindex  Trueの個数はok+1こ

n,m,v,p = ma()
A = lma();A.sort(reverse=True)
#print(A)
ap = A[p-1]
ans = bisect(A)+1
print(ans)

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("YES") if fl else print("NO")
import collections
import math
import itertools
import heapq as hq
def isok(a):##適宜変更
    if a+m<ap:
        return False
    tmp = 0 #a+mを超えないように割り振れる最大値
    for i in range(n-1,-1,-1):
        if a+m<=A[i]:break
        tmp+= min(a+m - A[i],m)
    tmp-=m#自分自身
    #print(a,tmp)
    return  tmp>=(v-p)*m

def bisect(ls): ##valの関数isok(x,val)がTrueとなる一番右のindex を返す 全部Falseなら-1,Trueならlen(ls)-1
    ok = -1
    ng = len(ls)
    x = (ok+ng)//2
    while ng-ok>1:
        num = ls[x]
        if isok(num):
            ok = x
        else:
            ng = x
        x = (ok+ng)//2
    return ok ##一番右のTrueのindex  Trueの個数はok+1こ

n,m,v,p = ma()
A = lma();A.sort(reverse=True)
#print(A)
ap = A[p-1]
ans = bisect(A)+1
print(ans)

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("YES") if fl else print("NO")
import collections
import math
import itertools
import heapq as hq
def isok(a):##適宜変更
    if a+m<ap:
        return False
    tmp = 0 #a+mを超えないように割り振れる最大値
    for i in range(n-1,-1,-1):
        if a+m<=A[i]:break
        tmp+= min(a+m - A[i],m)
    tmp-=m#自分自身
    print(a,tmp)
    return  tmp>=(v-p)*m

def bisect(ls): ##valの関数isok(x,val)がTrueとなる一番右のindex を返す 全部Falseなら-1,Trueならlen(ls)-1
    ok = -1
    ng = len(ls)
    x = (ok+ng)//2
    while ng-ok>1:
        num = ls[x]
        if isok(num):
            ok = x
        else:
            ng = x
        x = (ok+ng)//2
    return ok ##一番右のTrueのindex  Trueの個数はok+1こ

n,m,v,p = ma()
A = lma();A.sort(reverse=True)
#print(A)
ap = A[p-1]
ans = bisect(A)+1
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def check(k, A, N, M, V, P):
    #print(k, 'start')
    VAL = A[k] + M
    if VAL < A[P]:
        return False
    if P + N-k >= V:
        return True
    #print('check1')
    checked_arr = A[P:k]
    members = V - P - N + k
    MM = M
    for i in range(len(checked_arr)):
        if MM == 0: return True
        fil_val = min(VAL - checked_arr[i], MM)
        MM -= fil_val
        if i + members >= len(checked_arr):
            return False
        for j in range(i, i+members):
            checked_arr[j] += fil_val
    if MM == 0:
        return True
    else:
        return False

def run():
    N, M, V, P, *A = map(int, read().split())

    A = sorted(A)
    A.append(-INF)
    A = A[::-1]
    l = 1
    r = N
    while r-l > 1:
        #print(l, r)
        x = (r + l) // 2
        if check(x, A, N, M, V, P):
            l = x
        else:
            r = x

    #print('last, ', l, r)
    if check(r, A, N, M, V, P):
        print(r)
    else:
        print(l)





if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve():
    n, m, v, p = getList()
    nums = [0] + getList()
    nums.sort()
    acc = []
    tmp = 0
    for i in range(n+1):
        tmp += nums[i]
        acc.append(tmp)

    for i in range(1, n+1):
        if i >= n - p:
            tgt = nums[i]
            break
        cur = nums[i] + m
        if cur < nums[n-p]:
            continue
        vv = v - (p-1) - i
        if vv <= 0:
            tgt = nums[i]
            break
        rr = n - p + 1
        ll = i + 1
        sum1 = acc[rr] - acc[ll-1]
        # print(ll, rr, sum1)
        if cur * (rr - ll + 1) - sum1 < vv * m:
            continue
        tgt = nums[i]
        break
    # print(nums)
    # print(acc)
    # print(tgt)
    print(n - bisect_left(nums, tgt) + 1)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
def is_ok(index):
  s=0
  for i in range(p,index):
    s+=a[index]+m-a[i]
  return s+m*(n-index+p-1)>=m*v and a[index]+m>=a[p-1]
def binary_search(a):
  ok=-1
  ng=len(a)
  while abs(ng-ok)>1:
    mid=(ng+ok)//2
    if is_ok(mid):ok=mid
    else:ng=mid
  return ok+1
n,m,v,p=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
print(binary_search(a))

Submission status: Wrong Answer

Solution code:
def is_ok(index):
  s=0
  for i in range(p,index):
    s+=a[index]+m-a[i]
  return s+m*(n-index+p-1)>=m*v and a[index]+m>=a[p]
def binary_search(a):
  ok=-1
  ng=len(a)
  while abs(ng-ok)>1:
    mid=(ng+ok)//2
    if is_ok(mid):ok=mid
    else:ng=mid
  return ok+1
n,m,v,p=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
print(binary_search(a))

Submission status: Wrong Answer

Solution code:
def is_ok(index):
  s=0
  for i in range(p,index):
    s+=a[index]+m-a[i]
  return s+m*(n-index+p-1)>=m*v and a[index]+m>=a[p]
def binary_search(a):
  ok=-1
  ng=len(a)
  while abs(ng-ok)>1:
    mid=(ng+ok)//2
    if is_ok(mid):ok=mid
    else:ng=mid
  return ng
n,m,v,p=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
print(binary_search(a))

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m, v, p = map(int, readline().split())
    a = list(map(int, readline().split()))

    a.sort()

    ng = -1
    ok = n - 1

    while abs(ng - ok) > 1:
        mid = (ng + ok) // 2
        cur = a[mid] + m
        rem = v - (mid + 1 + p - 1)
        score = m * rem
        cnt = 0
        for x in a[mid + 1:n - p + 1]:
            if x > cur:
                cnt += 1
                score -= m
            else:
                score -= cur - x
        if cnt < p and score <= 0:
            ok = mid
        else:
            ng = mid

    print(n - ok)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

As = [10**9+1] + list(map(int, input().split()))

As.sort(reverse=True)


rlt = P
memo = [0]*(N+1)

for i in range(P+1,N+1):
  memo[i] = memo[i-1] + As[i-1]

for i in range(P+1,N+1):
  if N - i >= V - P:
    if As[i] + M >= As[P]:
      rlt += 1
  else:
    t = i - P
    if memo[i] + (t-N+V)*M <= (As[i] + M)*t:
      rlt += 1
      
print(rlt)

Submission status: Wrong Answer

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

As = [10**9+1] + list(map(int, input().split()))

As.sort(reverse=True)


rlt = P
memo = [0]*(N+1)

for i in range(P+1,N+1):
  memo[i] = memo[i-1] + As[i-1]

for i in range(P+1,N+1):
  if As[i] == As[i-1]:
    rlt += 1
    continue
  if N - i >= V - P:
    if As[i] + M >= As[P]:
      rlt += 1
    else:
      break
  else:
    t = i - P
    if memo[i] + (t-N+V)*M <= (As[i] + M)*t:
      rlt += 1
    else:
      break
      
print(rlt)

Submission status: Wrong Answer

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

As = list(map(int, input().split()))

As.sort(reverse=True)

rlt = P
memo = [0]*N

for i in range(P,N):
  memo[i] = memo[i-1] + As[i-1]

for i in range(P,N):
  if As[i] == As[i-1]:
    rlt += 1
    continue
  if N - i -1 >= V - P:
    if As[i] + M >= As[P-1]:
      rlt += 1
    else:
      break
  else:
    t = i - P + 1
    if memo[i] + (t-N+V)*M <= (As[i] + M)*t:
      rlt += 1
    else:
      break
      
print(rlt)

Submission status: Wrong Answer

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

As = list(map(int, input().split()))

As.sort(reverse=True)

rlt = P
memo = [0]*N

for i in range(P,N):
  memo[i] = memo[i-1] + As[i-1]

for i in range(P,N):
  if V <= P or N - i -1 >= V - P:
    if As[i] + M >= As[P-1]:
      rlt += 1
    else:
      break
  else:
    t = i - P + 1
    if memo[i] + (t-N+V)*M <= (As[i] + M)*t:
      rlt += 1
    else:
      break
      
print(rlt)

Submission status: Wrong Answer

Solution code:
n, m, v, p = map(int, input().split())
alst = list(map(int, input().split()))
alst.sort(reverse = True)
left = p - 1
right = n
while right - left > 1:
    pos = (left + right) // 2
    if alst[pos] + m < alst[p - 1]:
        right = pos
        continue
    cnt = (p - 1) * m
    base = alst[pos] + m
    for i in range(p, n):
        cnt += min(m, base - alst[i])
    if cnt >= m * v:
        left = pos
    else:
        right = pos
print(left + 1)

Submission status: Wrong Answer

Solution code:
n, m, v, p = map(int, input().split())
alst = list(map(int, input().split()))
alst.sort(reverse = True)
left = 0
right = n
while right - left > 1:
    pos = (left + right) // 2
    if alst[pos] + m < alst[p - 1]:
        right = pos
        continue
    cnt = (p - 1 + n - pos) * m
    base = alst[pos] + m
    for i in range(p, pos):
        cnt += min(m, base - alst[i])
    if cnt >= m * v:
        left = pos
    else:
        right = pos
print(left + 1)

Submission status: Wrong Answer

Solution code:
n, m, v, p = map(int, input().split())
alst = list(map(int, input().split()))
alst.sort(reverse = True)
left = p - 1
right = n
while right - left > 1:
    pos = (left + right) // 2
    if alst[pos] + m < alst[p - 1]:
        right = pos
        continue
    cnt = (p - 1 + n - pos) * m
    base = alst[pos] + m
    for i in range(p, pos):
        cnt += min(m, base - alst[i])
    if cnt >= m * v:
        left = pos
    else:
        right = pos
print(left + 1)

Submission status: Wrong Answer

Solution code:
n, m, v, p = map(int, input().split())
alst = list(map(int, input().split()))
alst.sort(reverse = True)
left = p - 1
right = n
while right - left > 1:
    pos = (left + right) // 2
    if alst[pos] + m < alst[p - 1]:
        right = pos
        continue
    cnt = (p - 1 + n - pos) * m
    base = alst[pos] + m
    for i in range(p, pos):
        cnt += base - alst[i]
    if cnt >= m * v:
        left = pos
    else:
        right = pos
print(right)

Submission status: Wrong Answer

Solution code:
n, m, v, p = map(int, input().split())
alst = list(map(int, input().split()))
alst.sort(reverse = True)
left = p - 1
right = n
while right - left > 1:
    pos = (left + right) // 2
    if alst[pos] + m < alst[p - 1]:
        right = pos
        continue
    cnt = (p - 1 + n - pos) * m
    base = alst[pos] + m
    for i in range(p, pos):
        cnt += min(m, base - alst[i])
    if cnt >= m * v:
        left = pos
    else:
        right = pos
print(right)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)

def f(x):
    if x < p:
        return True
    else:
        res = m * v
        res -= m * (n-x)
        res -= m * (p-1)
        for i in range(p-1, x):
            res -= min(m, max(0,a[x]+m-a[i]))
        return res <= 0

ok = 0
ng = n
while ng - ok > 1:
    nw = (ok+ng) // 2
    if f(nw):
        ok = nw
    else:
        ng = nw
print(ng)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)

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

ok = 0
ng = n
while ng - ok > 1:
    nw = (ok+ng) // 2
    if f(nw):
        ok = nw
    else:
        ng = nw
print(ng)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)

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

ok = 0
ng = n
while ng - ok > 1:
    nw = (ok+ng) // 2
    if f(nw):
        ok = nw
    else:
        ng = nw
print(ng)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)

def f(x):
    if x < p:
        return True
    if a[p-1] > a[x] + m:
        return False
    else:
        res = v * m
        res -= m * (p-1) #large
        res -= m * (n-x) #small
        for i in range(p-1, x):
            res -= min(m, max(0,a[x]+m-a[i]))
        return res < 0

ok = 0
ng = n
while ng - ok > 1:
    nw = (ok+ng) // 2
    if f(nw):
        ok = nw
    else:
        ng = nw
print(ng)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)

def f(x):
    if x < p:
        return True
    else:
        res = v * m
        res -= m * (p-1) #large
        res -= m * (n-x) #small
        for i in range(p-1, x):
            res -= min(m, max(0,a[x]+m-a[i]))
        return res < 0

ok = 0
ng = n
while ng - ok > 1:
    nw = (ok+ng) // 2
    if f(nw):
        ok = nw
    else:
        ng = nw
print(ng)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, m, v, p = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
sm = 0
ret = p
for i in range(n-p-1,-1,-1):
    if a[i+1]==a[i]:
        ret += 1
        sm += a[i+1]
#        print(i, ret)
    else:
        pi = a[i] + m
        sm += a[i+1]
        res = max(0, m*(v-p-i)) + sm
        pj = (res + (n-p-i-1))//(n-p-i)
#        print(i, pi , sm , res , pj, ret)
        if pi >= pj:
            ret += 1
        else:
            break
print(ret)