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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

ans = 0
tmp = -1
for x in range(N):
    if A[x] == tmp:
        ans = x+1
        continue
    tmp = A[x]

    if x < P:
        ans = x+1
        continue
    else:
        Smid = S[x]-S[P-1]
        C = V - (N-x + P-1)
        if C < 0:
            ans = x+1
            continue
        else:
            if -(-(Smid + C*M)//(x-P+1)) <= A[x] + M:
                ans = x+1
                continue
            else:
                break


print(ans)

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

import itertools

#import bisect

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
def main():
    n,m,v,p=map(int,input().split())
    a=list(map(int,input().split()))
    a=sorted(a,reverse=True)
    border=a[p-1]
    notac=0
#    print(a)
 #   print(border)
    if v<p:
        for i in range(p,n):
            if border-a[i]>m:
                notac=i
                break
        ans=[a[i] for i in range(notac,n)]
    else:
        for i in range(p,n):
            total=sum(a[p-1:i])
            if m*(n-v)<total-(i-p+1)*a[i]:
                notac=i
                break
        ans=[a[i] for i in range(notac,n)]
 #   print(ans)
    print(n-len(ans))











if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

As.sort(reverse=True)
Ss = [0]
for i in range(N):
    Ss.append(Ss[-1] + As[i])
cnt = P
for i in range(P, N):
    dist = V - (P + N - i - 1)
    if dist <= 0 and As[i] + M >= As[P - 1]:
        cnt += 1
    elif M * dist + Ss[i] - Ss[P - 1] <= (As[i] + M) * (i - P + 1):
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import math
import copy
import bisect
n,m,v,p = map(int,input().split())
ls_a = list(map(int,input().split()))

ls_a.sort()
ls_ac =  copy.deepcopy(ls_a)
for i in range(0,v):
    # print(ls_ac[-i-1])
    ls_ac[i] += m
ls_ac.sort()
# print(ls_ac)
lim = max(ls_ac[n-p-1]-m,0)
ans = n-bisect.bisect_left(ls_a,lim)
# print(ls_a)
# print(lim)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

a.sort()

border = a[-p]

res = bisect.bisect_left(a, border - m)

if res + p < v:
    zan = v - (res + p - 1)
    if p != 1:
        a2 = a[res : 1 - p]
    else:
        a2 = a[res:]
    t = 0
    for sa2 in a2:
        t += border - sa2
    if t > zan * m:
        print(n - res)
    else:
        ten = zan * m - t
        j = 0
        l = len(a2)
        while j < l:
            ten -= (a2[j] + m - border) * (l - j - 1)
            if ten <= 0:
                print(n - res - j)
                break
            j += 1
        if j == l:
            print(n - bisect.bisect_left(a, border) + 1)
else:
    print(n - res)

Submission status: Wrong Answer

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

A.sort()

K = A[-P]
R = bisect.bisect_left(A, K)
L = bisect.bisect_left(A, K - M)
ans = N - R
D = N-P-R + 1
for i in range(L, R):
    if V > P+i:
        v = V - (P+i)
        if (-((-v*M) // D)) + K <= A[i]+M:
            ans += 1

    else:
        ans += 1

print(ans)

Submission status: Wrong Answer

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

a = sorted(a)[::-1]

minp = a[p-1]
result = 0
td = 0
count = 0
tpcount = 1
tptd = 0
pre = minp
for i in range(p, n):
    if pre != a[i]:
        td += tptd
        count += tpcount
        tptd = 0
        tpcount = 0
    if m * min(n-v, i-p+1) >= (minp - a[i]) * count - td:
        result+=1
        tpcount+=1
        tptd+= minp - a[i]
    else:
        break
print(result+p)

Submission status: Wrong Answer

Solution code:
n,m,v,p=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
ct=0
for i in range(n-p):
    if a[i]+m>=a[n-p-1]:
        ct=i+1
        break
print(n-ct+1)

Submission status: Wrong Answer

Solution code:
N, M, V, P = [int(x) for x in input().split()]
A = sorted([int(x) for x in input().split()], reverse=True)
maxA = A[0]
cnt = 0
for a in A:
    cnt += (a >= maxA-M)
print(cnt)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,v,q = LI()
    a = LI()
    a.sort()
    p = m*v
    l = -1
    r = n-1
    while r-l > 1:
        x = (l+r) >> 1
        ai = a[x]
        i = bisect.bisect_left(a,ai)
        res = p-m*n
        for j in range(i,n):
            res += a[j]-ai
        for j in range(min(n-i-1,q-1)):
            k = n-j-1
            res -= a[k]-ai
        if res <= 0:
            r = x
        else:
            l = x
    print(n-r)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

cnt = 0
A.sort()
S = [0]*(N+1)
for i in range(N):
    S[i+1] = S[i]+A[i]
    
if V <= P:
    for i in range(N-P):
        if A[i]+M < A[N-P]:
            cnt += 1
        else:
            break
else:
    for i in range(N-P):
        if i+P >= V:
            if A[i]+M < A[N-P]:
                cnt += 1
            else:
                break
        else:
            num = M*V-M*(i+P)#さばかないといけない数
            t = (N-P-i)*(A[i]+M)-(S[N-P+1]-S[i+1])
            if num > t:
                cnt += 1
            else:
                break
                
print(N-cnt)

Submission status: Wrong Answer

Solution code:
n,m,v,p = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
a = a[::-1]
tmp = a[p-1]
ans = p
imos = [0]
for i in a:
    imos.append(imos[-1]+i)
if v<=p:
    for i in range(p,n):
        if a[i]+m>=tmp:
            ans+=1
    print(ans)
    exit()
else:
    for i in range(p, n):
        if a[i]+m >= tmp:
            t = max(0,v-p- (n-1-i))
            if a[i]==a[i-1]:
                continue
            if (a[i]+m)*(i-p)-(imos[i]-imos[p])>=t*m:
                continue
            else:
                print(i)
                exit()
    print(n)

Submission status: Wrong Answer

Solution code:
def check(n):
    x = A[n] + M
    B = A[:n] + A[n+1:-(P-1)]
    scores = M * (V - P)
    if len(B) == 0:
        return True
    if x < B[-1]:
        return False
    for b in B:
        if b + M <= x:
            scores -= M
        else:
            scores -= x - b
        if scores <= 0:
            return True
    return False


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

A.sort()
left = 0
right = N - P
while left < right:
    mid = (left+right) // 2
    if check(mid):
        right = mid
    else:
        left = mid + 1
print(N - left)

Submission status: Wrong Answer

Solution code:
n,m,v,p = map(int, input().split())
a = sorted(list(map(int, input().split())))
border = a[-p]
pos = sum([max(0,border-i) for i in a[:-(p+1)]])
for key,i in enumerate(a):
  border_n = border
  pos_n = pos
  if p < v:
    must = (v - p) * m
    pos_n -= max(0,border_n-i)
    border_n += max(0,(must - pos_n + n - p + 1) // (n-p))
  if i+m >= border_n:
    print(n-key)
    break

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

ans = 0
for x in range(N):
    if x < P:
        ans = x+1
        continue
    else:
        Smid = S[x]-S[P-1]
        C = V - (N-x + P-1)
        # print(Smid, C, Smid + C*M, (x-P+1), A[x] + M)
        if C < 0:
            ans = x+1
            continue
        else:
            if -(-(Smid + C*M)//(x-P+1)) <= A[x] + M:
                ans = x+1
                continue
            else:
                break

print(ans)

Submission status: Wrong Answer

Solution code:
N,M,V,P=[int(a) for a in input().split()]
A=[int(a) for a in input().split()]
effect=N-V
A.sort(reverse=True)
#print(A)
ans=P
target_score=A[P-1]
target_vote=0
#print(target_score)
for i in range(P,N):
    if A[i-1]>A[i]:
        #print(i,target_vote)
        target_vote+=(A[i-1]-A[i])*(i-P+1)
        #print(i,target_vote)
    real_effect=min(effect,i-P+1)
    #print(i,target_vote,real_effect)
    if (target_vote+1)//real_effect<=M and target_score-A[i]<=M:
        ans+=1
    else:
        break
    #print(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

ans = 0
for x in range(N):
    if x < P:
        ans = x+1
        continue
    else:
        Smid = S[x]-S[P-1]
        C = V - (N-x + P-1)
        print(Smid, C, Smid + C*M, (x-P+1), A[x] + M)
        if C < 0:
            ans = x+1
            continue
        else:
            if -(-(Smid + C*M)//(x-P+1)) <= A[x] + M:
                ans = x+1
                continue
            else:
                break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

ans = 0
for x in range(N):
    if x < P:
        ans = x+1
        continue
    else:
        Smid = S[x]-S[P-1]
        C = V - (N-x + P-1)
        # print(Smid, C, Smid + C*M, (x-P+1), A[x] + M)
        if C < 1:
            ans = x+1
            continue
        else:
            if -(-(Smid + C*M)//(x-P+1)) <= A[x] + M:
                ans = x+1
                continue
            else:
                break

print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,v,q = LI()
    a = LI()
    a.sort()
    p = m*v
    l = -1
    r = n
    while r-l > 1:
        x = (l+r) >> 1
        ai = a[x]
        i = bisect.bisect_left(a,ai)
        res = p-m*n
        for j in range(i,n):
            res += a[j]-ai
        for j in range(min(n-i-1,q-1)):
            k = n-j-1
            res -= a[k]-ai
        if res <= 0:
            r = x
        else:
            l = x
    print(n-r)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

A.sort()

K = A[-P]
R = bisect.bisect_left(A, K)
L = bisect.bisect_left(A, K - M)
ans = N - R
# print(ans)
for i in range(L, R):
    # print("i:", i, P+i, V > P+i)
    if V > P+i:
        v = V - (P+i)
        # print(v*M, N-P-R + 1)
        if math.ceil((v*M) / (N-P-R + 1)) + K <= A[i]+M:
            ans += 1

    else:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter


def solve(n, m, v, p, aaa):
    aaa.sort(reverse=True)
    count = Counter(aaa)
    scores = sorted(count.keys(), reverse=True)

    htm_cnt = 1  # higher than me excluding top p-1
    htm_sum = aaa[p - 1]
    rival = aaa[p - 1]
    ans = 0

    tmp_cnt = 0
    pi = 0
    for pi, s in enumerate(scores):
        c = count[s]
        if tmp_cnt + c >= p:
            ans = tmp_cnt + c
            rival = s
            htm_cnt = tmp_cnt + c - (p - 1)
            htm_sum = s * htm_cnt
            break
        tmp_cnt += c

    # print(ans)
    # print(rival)
    # print(htm_cnt, htm_sum)

    for s in scores[pi + 1:]:
        c = count[s]
        upper = s + m

        v4mtm = v - (n - htm_cnt - (c - 1))  # remaining vote for higher than s excluding top p-1
        if v4mtm <= 0:
            if upper >= rival:
                ans += c
            else:
                break
        else:
            space = upper * (htm_cnt + (c - 1)) - (htm_sum + s * (c - 1))
            if space >= m * v4mtm:
                ans += c

        htm_cnt += c
        htm_sum += s * c

    return ans


n, m, v, p = map(int, input().split())
aaa = list(map(int, input().split()))
print(solve(n, m, v, p, aaa))

Submission status: Wrong Answer

Solution code:

def judge(ind):

    maxi = A[ind] + M

    left = M * (V-1)
    over = 0
    np = P-1

    for i in range(N):
        
        if i == ind:
            continue
        
        if np > 0:
            left -= M
            np -= 1
        else:
            left -= min(M, maxi - A[i])

    if left > 0:
        return False
    else:
        return True


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

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

A.sort()
A.reverse()

l = 0
r = N

while r - l != 1:

    mid = (l+r)//2

    if judge(mid):
        l = mid
    else:
        r = mid

print (r)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
S = sum(A)
for i in range(P-1):
    x = A.pop()
    S -= x
total_score = M*(V-P+1)
kazu = N - P + 1
for i in range(N-P+1):
    a = A.pop(0)
    total_score -= M
    S -= a
    kazu -= 1
    T = (M+a)*kazu - S
    if T >= total_score:
        ans = kazu + P
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,v,q = LI()
    a = LI()
    a.sort()
    p = m*v
    l = -1
    r = n
    while r-l > 1:
        x = (l+r) >> 1
        ai = a[x]
        i = bisect.bisect_right(a,ai)
        res = p-m*n
        for j in range(i,n):
            res += a[j]-ai
        for j in range(min(n-i-1,q-1)):
            k = n-j-1
            res -= a[k]-ai
        if res <= 0:
            r = x
        else:
            l = x
    print(n-r)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
N,M,V,P = map(int,input().split())
cntA = Counter(list(map(int,input().split())))
keyA = sorted(cntA.keys(),reverse=True)

selected_num = 0
for key in keyA:
  if selected_num < P:
    selected_num += cntA[key]
    if selected_num>=P:
      last_val = key
      last_val_num = cntA[key]
  else:
    if V>=N-last_val_num:
      if key + M >= last_val+(M-1)//last_val_num + 1:
        selected_num += cntA[key]
    else:
      if key + M >= last_val:
        selected_num += cntA[key]
    #print(last_val, key, M, selected_num)
print(selected_num)

Submission status: Wrong Answer

Solution code:
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 = -1
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)

Submission status: Wrong Answer

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

a = sorted(a)[::-1]

minp = a[p-1]
result = 0
td = 0
count = 0
tpcount = 1
tptd = 0
pre = minp
for i in range(p, n):
    if pre != a[i]:
        td += tptd
        count += tpcount
        tptd = 0
        tpcount = 0
    if m * (n-v) >= (minp - a[i]) * count - td :
        result+=1
        tpcount+=1
        tptd+= minp - a[i]
    else:
        break
print(result+p)

Submission status: Wrong Answer

Solution code:
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]
#print(it)

for i in range(n):
    
    if it[i]+m<it[-p]:
        continue
    ind=n-(p-1)
    ind2=par[i]
    tot=m*v
    
    tot-=(n-ind)*m
    tot-=(ind2)*m
    tot-=m
    #print(tot)
    co=pre[ind-1]-pre[ind2]
    co=((it[i]+m)*(ind-1-ind2)-co)
    
    #print(ind,ind2,tot,co)
    if co>=tot:
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,v,p = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
a = a[::-1]
tmp = a[p-1]
ans = p
imos = [0]
for i in a:
    imos.append(imos[-1]+i)
if v<=p:
    for i in range(p,n):
        if a[i]+m>=tmp:
            ans+=1
    print(ans)
    exit()
else:
    for i in range(p, n):
        if a[i]+m >= tmp:
            t = v-p- (n-1-i)
            if (a[i]+m)*(i-p)-(imos[i]-imos[p])>=t*m:
                ans+=1
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
def solve(n, m, v, p, a_list):
    a_list_s = sorted(a_list, reverse=True)
    # member
    # vote: v
    # problem: p
    base_bar = a_list_s[p - 1]
    res = p
    space = 0
    for i in range(p, n):
        space_now = space + (a_list_s[i] + m - base_bar) * (i - p + 1)
        v_now = max(v - (n - i) - (p - 1), 0)
        if space_now >= v_now * m:
            res += 1
        space += base_bar - a_list_s[i]

    return res


def main():
    n, m, v, p = map(int, input().split())
    a_list = list(map(int, input().split()))
    res = solve(n, m, v, p, a_list)
    print(res)


def test():
    assert solve(6, 1, 2, 2, [2, 1, 1, 3, 0, 2]) == 5
    assert solve(6, 1, 5, 2, [2, 1, 1, 3, 0, 2]) == 3
    assert solve(10, 4, 8, 5, [7, 2, 3, 6, 1, 6, 5, 4, 6, 5]) == 8


if __name__ == "__main__":
    test()
    main()

Submission status: Wrong Answer

Solution code:

def judge(ind):

    maxi = A[ind] + M

    left = M * (V-1)
    over = 0
    np = P-1

    for i in range(N):
        
        if i == ind:
            continue
        
        if np > 0:
            left -= M
            over += 1
            np -= 1
        else:
            left -= min(M, maxi - A[i])

        #print (A[i] , left)

    #print (A[ind] , left , over , maxi)

    if left < 0:
        if over < P:
            return True
        else:
            return False
    else:
        if over + left < P:
            return True
        else:
            return False


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

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

A.sort()
A.reverse()

l = 0
r = N

while r - l != 1:

    mid = (l+r)//2

    if judge(mid):
        l = mid
    else:
        r = mid

print (r)

Submission status: Wrong Answer

Solution code:
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
print(a)
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)

Submission status: Wrong Answer

Solution code:
import os
import sys

import bisect

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

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

A.sort(reverse=True)
RA = A[::-1]


def can_choose(k):
    # A[k]が選ばれるか
    ak = A[k] + M
    greaters = N - bisect.bisect_right(RA, ak)
    if greaters >= P:
        return False
    # 自分より上位 P - 1 人には全部配る
    rem = (V - P) * M

    for i, a in reversed(list(enumerate(A))):
        if i == k:
            continue
        if i < P:
            break
        if rem <= 0:
            break
        rem -= min(M, ak - a)
    return rem <= 0


ok = P - 1
ng = N
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if can_choose(mid):
        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 = sorted(a)[::-1]

minp = a[p-1]
result = 0
td = 0
count = 1
for i in range(p, n):
    if m * (n-v) >= (minp - a[i]) * count - td :
        result+=1
        count+=1
        td+= minp - a[i]
    else:
        break
print(result+p)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right as br
n,m,v,p=map(int,input().split())
a=sorted(list(map(int,input().split())))
l,r=n,0
while l-r>1:
    t=(l+r)//2
    s=a[t]+m
    w=a[:]
    k=m*v
    for i in range(n):
        if i!=t:
            if i<t:
                k-=m
                w[i]+=m
            else:
                if w[i]>s:
                    break
                else:
                    k-=s-w[i]
                    w[i]=s
            if k==0:
                break
    if k>=0:
        for j in range(n-1,-1,-1):
            if j!=t:
                w[j]+=min(k,m-w[j]+a[j])
                k-=min(k,m-w[j]+a[j])
            if k==0:
                break
    w.pop(t)
    w=sorted(w)
    if n-br(w,s)<=p:
        l=t
    else:
        r=t
print(n-r+1)

Submission status: Wrong Answer

Solution code:
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 N-V < 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))

Submission status: Wrong Answer

Solution code:
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
for i in range(P,N):
    rest_m=M*(N-V)
    p=A[i]
    dsum+=(A[i-1]-p)*pre_num
    pre_num+=1
    rest_m-=dsum
    if rest_m<0:break
    ans+=1
print(P+ans)

Submission status: Wrong Answer

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

K = N - V #減らせる数
A.sort(reverse = True)
for i in range(P,N):
    tot = sum(A[(P-1):i]) - A[i]*(i-(P-1))
    if K*M <tot:
        break
print(i)

Submission status: Wrong Answer

Solution code:
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 N-V < A[-P]-a:
        ng = c
        continue
    s = 0
    for aa in A[-P:idx-1:-1]:
        s += (A[-P]-a)
    if s > M*(N-V):
        ng = c
    else:
        ok = c
a = A_m[ok]
print(sum(a<=aa for aa in A))

Submission status: Wrong Answer

Solution code:
import bisect
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
    border = v*m-(n-x+p-1)*m
    biggest = sum([a[x]+m-a[j] for j in range(p-1,x)])
    if border > biggest:
        r = x
    elif border <= biggest:
        l = x+1
print(l)

Submission status: Wrong Answer

Solution code:
def f(k):
    if k<=P:
        return True
    num=A[k]+M
    zzz=M*V-(N-k+P)*M
    cnt=0
    for j in range(P,k):
        zzz-=max(0,num-A[j])
        if num<A[j]:
            cnt+=1
    if zzz<=0 and cnt<P:
        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)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right as br
n,m,v,p=map(int,input().split())
a=sorted(list(map(int,input().split())))
l,r=n,0
while l-r>1:
    t=(l+r)//2
    s=a[t]+m
    w=a[:]
    k=m*v
    for i in range(n-1):
        if i!=t:
            if s>=w[i]:
                r=min(s-w[i],m)
                k-=r
                w[i]+=r
            if k==0:
                break
    if k>=0:
        for j in range(n-1,-1,-1):
            if j!=t:
                w[j]+=min(k,m-w[j]+a[j])
                k-=min(k,m-w[j]+a[j])
            if k==0:
                break
    if n-br(w,s)<=p:
        l=t
    else:
        r=t
print(n-r+1)

Submission status: Wrong Answer

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

K = N - V #減らせる数
A.sort(reverse = True)
for i in range(P,N):
    tot = sum(A[(P-1):i]) - A[i]*(P-1)
    if K*M <tot:
        break
print(i)

Submission status: Wrong Answer

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


ans = P
S = A[P-1]
for i in range(P, N):
    if A[i]+M >= A[P-1] and V <= P-1+N-i:
        ans += 1
    else:
        if S+M*(V-(P-1)-(N-i)) <= (A[i]+M)*(i-(P-1)):
            ans += 1
    S += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N,M,V,P = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
m = max(A)
c = 0
for a in A:
    if a+M >= m:
        c += 1
print(c)

Submission status: Wrong Answer

Solution code:
n,m,v,p = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
a = a[::-1]
tmp = a[p-1]
ans = p
imos = [0]
for i in a:
    imos.append(imos[-1]+i)
if v<=p:
    for i in range(p,n):
        if a[i]+m>=tmp:
            ans+=1
    print(ans)
    exit()
else:
    t = v-p
    for i in range(p, n):
        if a[i]+m >= tmp:
            if (a[i]+m)*(i-p+1)-(imos[i]-imos[p])<t*m:
                ans+=1
    print(ans)
    exit()