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(reverse=True)
X = A.index(A[P-1])
C = A[X:]
V = V-X
P = P-X
D = list(set(C))
D = [i for i in D if i >= D[len(D)-1]-M]
Y = []
for i in D:
    F = M*(V-(len(C)-C.index(i)))
    E = C[:C.index(i)]
    G = (i+M)*len(E)-sum(E)
    if F <= G :
        break
B = A.index(i)+C.count(i)
print(B)

Submission status: Wrong Answer

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

W=N-V
z=A[P-1]

count=0
for i in range(P,N):
    if i<=P+N-V and A[i]-z<=M:
        count+=1
print(P+count)

Submission status: Wrong Answer

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

li = []

  # print(lis)
cnt = 1
now = 0
ans = 0

border = lis[p-1]
li.append(0)
ind = 0
for i in range(n-1):
    if lis[i]!=lis[i+1]:
        now += cnt
        cnt = 1
    else:
        cnt += 1
    if now <= p:
        ind += 1
    li.append(now)

print(li)
sa = 0
keykey = 1
key = 0

for i in range(n):
    if i >= p-1:
        sa += lis[i]
        if keykey:
            key = i
        keykey = 0

    if li[i]+1 <= p:
        ans += 1
        print(i,1)

    else:
        if lis[i] + m >= border:
            num = (p-1)+(n-li[i]-1)+1
            if num >= v:
                ans += 1
                print(i,2)
            else:
                nu = v-num
                if (sa -lis[i])-lis[i]*(i-key) <= (key-nu-1)*m:
                    ans += 1
                    print(i,3)
                    print(sa,lis[i],i,key,key-nu,m)
                else:
                    print(i,4)
                    print(sa, lis[i], i, key, key - nu, m)
        else:
            print(i,5)
    if i != n-1:
        if sa != 0 and lis[i] == lis[i+1]:
            sa -= lis[i]
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n,m,v,p=map(int,input().split())
A=list(map(int,input().split()))
a=sorted(A,reverse=True)
key_num=a[p-1]
k=a.count(key_num)
key_idx=a.index(key_num)+k
c=a[key_idx:]
d=collections.Counter(c)
D=list(d.items())
l=len(set(c))
cnt=0
for i in range(l):
  idx=i+1
  if key_num<=int(D[i][0])+m:
    if v<key_idx+idx:
      cnt+=D[i][1]
  if cnt>n-v:
    break
print(cnt+key_idx)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split(' '))
An = list(map(int,input().split(' ')))
An.sort(reverse=True)
threadhold_list = [0] * N
theard_num = An[P-1]
theard_idx = 0
sum_An = [0] * N
sum_An[0] = An[0]
for idx,val in enumerate(An):
    if theard_num == val:
        theard_idx = idx
    threadhold_list[idx] = max(0,theard_num-val)
for idx in range(1,N):
    sum_An[idx] = sum_An[idx-1] + An[idx]

#print(An)
#print(threadhold_list)
#print(sum_An)

min_num = 10**10
min_goal_num = 10**10
must_allocation_num = V- (P - 1)
for idx,val in enumerate(An):
    if idx + 1 <= P:
        min_num = min(min_num,val)
        min_goal_num = min(min_goal_num,val)
        continue
    if val == min_num:
        continue
    if threadhold_list[idx] <= M and must_allocation_num <= (N-idx):
        min_num = min(min_num,val)
        continue
    if M < threadhold_list[idx]:
        break 
    if (N-idx) < must_allocation_num:
        sum_foword_nums = sum_An[idx-1] - sum_An[(P - 1-1)]
        allocation_diff =  must_allocation_num - (N-idx)
        diveide_allocation_num = (idx-(P - 1))
        plus_sum = (allocation_diff*M)
        last_num = (sum_foword_nums + plus_sum)/diveide_allocation_num
        candidate_num = An[idx] + M
        if last_num <= candidate_num:
            min_num = min(min_num,val)
            continue

        
        sum_foword_nums = sum_An[idx-1]
        allocation_diff = V - (N-idx)
        plus_sum = (allocation_diff*M)
        last_num = (sum_foword_nums + plus_sum)/diveide_allocation_num
        candidate_num = An[idx] + M
        if last_num <= candidate_num:
            min_num = min(min_num,val)
            continue
        
    break
print(idx)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
A = [int(i) for i in input().split()]
A = sorted(A)
# 関係のある問題
A_q = A[0:N-P+1]
# 実質投票数
V_q = V - P + 1
ruikei = [A_q[-1]-a for a in A_q]
ruikei_sum = sum(ruikei)
for i,a in enumerate(A_q):
    if a + M >= A_q[-1]:
        plus = A_q[-1] - a
        yoyuu = ruikei_sum - ruikei[i] - (plus*(V_q-1))
        if yoyuu > 0:
            break
    else:
        pass
print(N-i)

Submission status: Wrong Answer

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

A_list = list(map(int, input().split()))
A_list.sort()

def check(x):
	if x >= N - P:
		return True
	
	a = A_list[x] + M
	
	if a < A_list[N - P]:
		return False
	
	v = V - (P - 1)
	m = 0
	for i in range(N - P):
#		print(i, A_list[i], ">", v, m)
		if i == x:
			next
		if v < 0 or v == 0 and m == 0:
			return True
		a2 = A_list[i]
		if a2 + M < a:
			v -= 1
		else:
			if a2 + m < a:
				m = a2 + m + M - a
				v -= 1
			else:
				m -= a - a2
	
	return v < 0 or v == 0 and m == 0

s = 0
l = N - 1

while s < l:
	mid = (s + l) // 2
	if check(mid):
		l = mid - 1
	else:
		if s == mid:
			l = s
			break
		s = mid

#for i in range(N):
#	print(i, A_list[i], check(i))

#print(l)

print(N - (l + 1))

Submission status: Wrong Answer

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


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

    B = sorted(A)
    for i in range(V-1):
        B[i] += M
    import collections
    d = collections.Counter(B)
    d = list(d.items())
    d.sort(key=lambda x: x[0], reverse=True)

    p = max(A)
    for k, v in d:
        P -= v
        p = k
        if P <= 0:
            break
    p = p - M

    ans = 0
    for a in A:
        if a >= p:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

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)
            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:
i = input().split()
n = int(i[0])
m = int(i[1]) # ジャッジの数
v = int(i[2]) # ジャッジの選ぶ問題数
p = int(i[3]) # 最終的な問題数
a = input().split()
a = [int(i) for i in a]

s = sorted(a, reverse = True)
print(s)

def jdgTF(X, t): # 上からX番目のtという数に可能性はあるか？
  tmp = s[:(X-1)]
  s_tmp = sum(tmp)
  nzero = (n-v) * m
  minp = max((X-1) * m - nzero, 0)
  s_tmp_post = s_tmp + minp
  jd = s_tmp_post - (sum(s[:p-1]) + m * (p-1))
  jd = jd / (X - 1 - (p - 1))
  if jd > (t + m):
    return(0)
  else:
  	return(1)

tf = [1 for i in range(len(s)) if s[i] >= s[p-1]]
tf.extend([jdgTF(i+1, s[i]) for i in list(range(p,len(s)))])
print(tf)

Submission status: Wrong Answer

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

alist.sort(reverse=True)
#print(alist)

border_init=alist[P-1]
#print(border_init)

dic_a_head={}
dic_a_tail={}
for i,ai in enumerate(alist):
  dic_a_tail[ai]=i
  if not ai in dic_a_head:
    dic_a_head[ai]=i
    
#print(dic_a_head)
#print(dic_a_tail)

answer=0
P1=P-1
P2=dic_a_tail[border_init]
for ai in dic_a_head:
  if ai+M<border_init:
    continue
  
  hd=dic_a_head[ai]
  tl=dic_a_tail[ai]
  if hd<P2:
    #print(0,hd,tl)
    answer+=tl-hd+1
  elif N-hd>=V:
    if ai+M>=border_init:
      #print(1,hd,tl)
      answer+=tl-hd+1
  elif N-hd+P1>=V:
    if ai+M>=border_init:
      #print(2,hd,tl)
      answer+=tl-hd+1
  else:
    restV=V-(N-hd+P1)
    #print(restV,V,-N,+hd,-P1)
    allV=restV*M
    #allV-=(border_init-ai)*(P2-P1+1)    
    restP=hd-P1
    #print(-1,allV,restV,restP)
    if ai+M>=border_init+allV//restP:
      #print(3,hd,tl)
      answer+=tl-hd+1
    
print(answer)

Submission status: Wrong Answer

Solution code:

n,m,v,p = map(int,input().split())
a = list(map(int,input().split()))
num = m * v
c= 0
ans = 0
tmp = 0

while num > 0:
    a.sort()
#    print(a)
    a[0] += 1
    num -= 1
#    print(a)
#    print(num)

a.sort(reverse = True)
#print(a)

while ans < p:
#    print(a[0])
#    print(a)
    c = a.count(a[0])
#    print(c)
    ans += c
    tmp = a[0]
#    print(a[0])
    while tmp in a:a.remove(tmp)
#    print(a)

print(ans+1)

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 + [x + M]
  isit = 0
  for i in range(V):
    if isit == 0:
      b[i] = -float("inf")
      isit = 1
    b[i] += M
  b.sort()
  c = 0
  t = 0
  for i in range(N, -1, -1):
    if b[i] != t:
      if c >= P: break
      if b[i] <= x + M: return True
      t = b[i]
    c += 1
  return False
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

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

print(res)

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
    for i in range(P, N):
        if AH_min == A[i]:
            V = max(V - 1, 0)
            AH.append(A[i])
        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)

    if len(AT) <= V - len(AH) + 1:
        # print(len(AT), V - len(AH) + 1)
        print(ans)
    else:
        _V = V - len(AH) + 1
        # print(_V, V, AH_min)
        for i in range(AT_len):
            if AH_min <= (AT[i] + M):
                remaining = AT_len - (MAP[AT[i]] + 1)
                # print(i, remaining, _V, 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().split())
A = list(map(int, input().split()))
A = sorted(A)

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

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

A.sort()

# convert
n = N - (P - 1)
m = M
v = max(1, V - (P - 1))
p = 1
a = A[:n]

cum_a = [0]
for ai in reversed(a):
    cum_a.append(cum_a[-1] + ai)
cum_a.reverse()


i = 0
for i in range(n):
    if (a[i] + m) * (n - 1 - i) - cum_a[i + 1] >= m * (v - 1 - i):
        break
else:
    i += 1

ans = N - i

print(ans)

Submission status: Wrong Answer

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

if p+v<=n:
    for i in range(n-p):
        idx=bisect.bisect_right(a,a[i]+m)
        #print(i,a[i],idx,n-p)
        if idx>=n-p:
            count+=1
    print(count)
    sys.exit()

s=[0]*n
s[0]=a[0]
for i in range(1,n):
    s[i]=s[i-1]+a[i]

def search(res,i):
    mn=-1
    mx=n-1
    buf=0
    while mx-mn>1:
        buf=(mx+mn)//2
        #print(res,s[buf]-s[i]-(buf-i)*a[i],i)
        if res>=s[buf]-s[i]-(buf-i)*a[i]:
            mn=buf
        else:
            mx=buf
    return mn


for i in range(n-p):
    k=0
    idx=bisect.bisect_right(a,a[i]+m)-1
    if idx>=v:
        buf=(a[i]+m)*(idx-v+1)-s[idx]+s[v-1]
        k=search(buf,i)-i
        #print(buf,k)
    idx1=bisect.bisect_left(a,a[i],0,v)
    #print(a[i]+m,max([idx-v+1,0]),idx1,k,max([idx-v+1,0])+idx1+k)
    if max([idx-v+1,0])+idx1+k>=n-p:
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:

#n,m,v,p = map(int,input().split())
n,m,v,p = 10,4,8,5

#a = list(map(int,input().split()))
a = [7,2,3,6,1,6,5,4,6,5]
num = m * v
c= 0
ans = 0
tmp = 0

while num > 0:
    a.sort()
#    print(a)
    a[0] += 1
    num -= 1
#    print(a)
#    print(num)

a.sort(reverse = True)
#print(a)

while ans < p:
#    print(a[0])
#    print(a)
    c = a.count(a[0])
#    print(c)
    ans += c
    tmp = a[0]
#    print(a[0])
    while tmp in a:a.remove(tmp)
#    print(a)

print(ans+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)
ans=p
k=-1
l=0
for i in range(p,n):
  if a[p-1]==a[i]:
    ans+=1
    l+=1
  else:
    k=i-1
    break
l=k+1-(v-(n-1-k))
j=n-v
if k==-1:
  print(ans)
else:
  if v-(n-1-k)>=p:
    print(ans)
  else:
    for i in range(k+1,n):
      if a[i]>=a[p-1]-m:
        if l>0:
          ans+=1
          l-=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(reverse=True)
X = A.index(A[P-1])
C = A[X:]
V = V-X
P = P-X
D = list(set(C))
D = [i for i in D if i >= D[len(D)-1]-M]
Y = []
for i in D:
    F = M*(V-(len(C)-C.index(i)))
    E = C[:C.index(i)]
    G = (i+M)*len(E)-sum(E)
    if F <= G :
        Y.append(i)
B = A.index(Y[0])+C.count(Y[0])
print(B)

Submission status: Wrong Answer

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

alist.sort(reverse=True)
#print(alist)

border_init=alist[P-1]
#print(border_init)

dic_a_head={}
dic_a_tail={}
for i,ai in enumerate(alist):
  dic_a_tail[ai]=i
  if not ai in dic_a_head:
    dic_a_head[ai]=i
    
#print(dic_a_head)
#print(dic_a_tail)

answer=0
P1=P-1
P2=dic_a_tail[border_init]
for ai in dic_a_head:
  if ai+M<border_init:
    continue
  
  hd=dic_a_head[ai]
  tl=dic_a_tail[ai]
  if hd<P2:
    #print(0,hd,tl)
    answer+=tl-hd+1
  elif N-hd>=V:
    if ai+M>=border_init:
      #print(1,hd,tl)
      answer+=tl-hd+1
  elif N-hd+P1>=V:
    if ai+M>=border_init:
      #print(2,hd,tl)
      answer+=tl-hd+1
  else:
    restV=V-(N-hd+P1)
    #print(restV,V,-N,+hd,-P1)
    allV=restV*M
    #allV-=(border_init-ai)*(P2-P1+1)    
    restP=hd-P1
    #print(-1,allV,restV,restP)
    if ai+M>=border_init+(allV+restP-1)//restP:
      #print(3,hd,tl)
      answer+=tl-hd+1
    
print(answer)

Submission status: Wrong Answer

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

if p+v<=n:
    for i in range(n-p):
        idx=bisect.bisect_right(a,a[i]+m)
        print(i,a[i],idx,n-p)
        if idx>=n-p:
            count+=1
    print(count)
    sys.exit()

s=[0]*n
s[0]=a[0]
for i in range(1,n):
    s[i]=s[i-1]+a[i]

def search(res,i):
    mn=-1
    mx=n-1
    buf=0
    while mx-mn>1:
        buf=(mx+mn)//2
        #print(res,s[buf]-s[i]-(buf-i)*a[i],i)
        if res>=s[buf]-s[i]-(buf-i)*a[i]:
            mn=buf
        else:
            mx=buf
    return mn


for i in range(n-p):
    k=0
    idx=bisect.bisect_right(a,a[i]+m)-1
    if idx>=v:
        buf=(a[i]+m)*(idx-v+1)-s[idx]+s[v-1]
        k=search(buf,i)-i
        #print(buf,k)
    idx1=bisect.bisect_left(a,a[i],0,v)
    #print(a[i]+m,max([idx-v+1,0]),idx1,k,max([idx-v+1,0])+idx1+k)
    if max([idx-v+1,0])+idx1+k>=n-p:
        count+=1
print(count)

Submission status: Wrong Answer

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

if n == p:
	print(n)
	exit(0)

now = 1
get = 0
while now < p:
	if a[now] != a[get]:
		get = now
	now += 1

limit = n
t = sum(a[get:now])
while now < n:
	vv = max(v - get - (n-now), 0)
	total = t + m*vv
	if (total//(now-get)) > a[now]+m:
		limit = now
		break
	elif (total//(now-get)) == a[now]+m:
		if (total - (total//(now-get))*(now-get)) >= p-get:
			limit = now
			break
	t += a[now]
	now += 1
print(limit)

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
memo = {}
sum_memo = {}
su = 0
b = 0
n_rank = 0
b_rank = 0
for n, i in enumerate(S): # Loop O(N)
    rank = n_rank
    if b != i:
        n_rank = n+1
    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
        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
print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,v,p = map(int,input().split())
mem = {}
a = list(map(int,input().split()))
for va in a:
    t = str(va)
    if t in mem:
        mem[t] = mem.get(t)+1
    else:
        mem[t] = 1
arr = []
for key in mem:
    arr.append([int(key),mem.get(key)])
arr = sorted(arr,key= lambda x:x[0] ,reverse=True)
# print (arr)
l = len(arr)
idex = 0
run = v
if p >= l :
    idex = l
else:
    idex = p
total = 0
start = idex
v = arr[idex-1][0]
# print(start,run)
while start<l and run>0:
    arr[start][0] += 1
    run -= 1
    start += 1
idex-=1
# print(idex,run)
while idex>-1 and run >0:
    arr[idex][0]+=1
    run-=1
    idex -=1
arr = sorted(arr,key= lambda x:x[0] ,reverse=True)
score =-1
idex = 0
total =0
while idex<l and p >0:
    p -=1
    score = arr[idex][0]
    while True:
        total+= arr[idex][1]
        idex +=1
        if idex<l and arr[idex][0] == score:
            pass
        else:
            break

# print (arr)
print(total)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split())
A = [int(i) for i in input().split()]
A = sorted(A)
# 関係のある問題
A_q = A[0:N-P+1]
# 実質投票数
V_q = V - P + 1
ruikei = [A_q[-1]-a for a in A_q]
ruikei_sum = sum(ruikei)
for i,a in enumerate(A_q):
    if a + M >= A_q[-1]:
        plus = A_q[-1] - a
        yoyuu = ruikei_sum - ruikei[i] - (plus*(V_q-1))
        if yoyuu >= 0:
            break
    else:
        pass
print(N-i)

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
    for i in range(P, N):
        if AH_min == A[i]:
            V = min(V - 1, 0)
            AH.append(A[i])
        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)

    if len(AT) <= V - len(AH) + 1:
        # print(len(AT), V - len(AH) + 1)
        print(ans)
    else:
        _V = V - len(AH) + 1
        # print(_V, V, AH_min)
        for i in range(AT_len):
            if AH_min <= (AT[i] + M):
                remaining = AT_len - (MAP[AT[i]] + 1)
                # print(i, remaining, _V, 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().split(' '))
An = list(map(int,input().split(' ')))
An.sort(reverse=True)
threadhold_list = [0] * N
theard_num = An[P-1]
theard_idx = 0
sum_An = [0] * N
sum_An[0] = An[0]
for idx,val in enumerate(An):
    if theard_num == val:
        theard_idx = idx
    threadhold_list[idx] = max(0,theard_num-val)
for idx in range(1,N):
    sum_An[idx] = sum_An[idx-1] + An[idx]

#print(An)
#print(threadhold_list)
#print(sum_An)

min_num = 10**10
min_goal_num = 10**10
must_allocation_num = V- (P - 1)
for idx,val in enumerate(An):
    if idx + 1 <= P:
        min_num = min(min_num,val)
        min_goal_num = min(min_goal_num,val)
        continue
    if val == min_num:
        continue
    if threadhold_list[idx] <= M and must_allocation_num <= (N-idx):
        min_num = min(min_num,val)
        continue
    if M < threadhold_list[idx]:
        break 
    if (N-idx) < must_allocation_num:
        sum_foword_nums = sum_An[idx-1] - sum_An[(P - 1-1)]
        allocation_diff =  must_allocation_num - (N-idx)
        diveide_allocation_num = (idx-(P - 1))
        plus_sum = (allocation_diff*M)
        last_num = (sum_foword_nums + plus_sum)/diveide_allocation_num
        candidate_num = An[idx] + M
        if last_num <= candidate_num:
            min_num = min(min_num,val)
            continue

        '''
        sum_foword_nums = sum_An[idx-1]
        allocation_diff = P - (N-idx)
        plus_sum = (allocation_diff*M)
        last_num = (sum_foword_nums + plus_sum)/diveide_allocation_num
        candidate_num = An[idx] + M
        if last_num <= candidate_num:
            min_num = min(min_num,val)
            continue
        '''
    break
print(idx)

Submission status: Wrong Answer

Solution code:
import collections
n,m,v,p=map(int,input().split())
A=list(map(int,input().split()))
a=sorted(A,reverse=True)
key_num=a[p-1]
k=a.count(key_num)
key_idx=a.index(key_num)+k
c=a[key_idx:]
d=collections.Counter(c)
D=list(d.items())
l=len(set(c))
cnt=0
for i in range(l):
  idx=i+1
  if key_num<=int(D[i][0])+m:
    if v<key_idx+idx:
      cnt+=D[i][1]
print(cnt+key_idx)

Submission status: Wrong Answer

Solution code:
import sys
import bisect


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()
    mi = (n - v) * m
    cor_v = 10 ** 20
    inc_v = -1
    while cor_v - inc_v > 1:
        bin_v = (cor_v + inc_v) // 2
        cost = 0
        #条件を満たすcostを全検索
        pp = bisect.bisect_right(a, bin_v)
        b = True
        for i in range(pp + 1, n):
            if a[i] > bin_v:
                if cost + a[i] - bin_v <= mi:
                    cost += a[i] - bin_v
                else:
                    if n - i + 1 >= p:
                        b = False
                    break
        if b:
            if v - pp < p:
                cor_v = bin_v
            else:
                inc_v = bin_v
        else:
            inc_v = bin_v

    cnt = 0
    for v in a:
        if v >= cor_v:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

A_list = list(map(int, input().split()))
A_list.sort()

def check(x):
	if x >= N - P:
		return True
	
	a = A_list[x] + M
	
	if a < A_list[N - P]:
		return False
	
	v = V - (P - 1)
	m = 0
	for i in range(N - P):
#		print(i, A_list[i], ">", v, m)
		if i == x:
			next
		if v < 0 or v == 0 and m == 0:
			return True
		a2 = A_list[i]
		if a2 + M < a:
			v -= 1
		else:
			if a2 + m <= a:
				m = a2 + m + M - a
				v -= 1
			else:
				m -= a - a2
	
	return v < 0 or v == 0 and m == 0

s = 0
l = N - 1

while s < l:
	mid = (s + l) // 2
	if check(mid):
		l = mid - 1
	else:
		if s == mid:
			l = s
			break
		s = mid

#for i in range(N):
#	print(i, A_list[i], check(i))

#print(l)

print(N - (l + 1))

Submission status: Wrong Answer

Solution code:
import sys
import bisect
n,m,v,p=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
count=p
'''
if p+v<=n:
    for i in range(n-p):
        idx=bisect.bisect_right(a,a[i]+m)
        print(i,a[i],idx,n-p)
        if idx>=n-p:
            count+=1
    print(count)
    sys.exit()
'''
s=[0]*n
s[0]=a[0]
for i in range(1,n):
    s[i]=s[i-1]+a[i]

def search(res,i):
    mn=-1
    mx=n-1
    buf=0
    while mx-mn>1:
        buf=(mx+mn)//2
        #print(res,s[buf]-s[i]-(buf-i)*a[i],i)
        if res>=s[buf]-s[i]-(buf-i)*a[i]:
            mn=buf
        else:
            mx=buf
    return mn


for i in range(n-p):
    k=0
    idx=bisect.bisect_right(a,a[i]+m)-1
    if idx>=v:
        buf=(a[i]+m)*(idx-v+1)-s[idx]+s[v-1]
        k=search(buf,i)-i
        #print(buf,k)
    idx1=bisect.bisect_left(a,a[i],0,v)
    #print(a[i]+m,max([idx-v+1,0]),idx1,k,max([idx-v+1,0])+idx1+k)
    if max([idx-v+1,0])+idx1+k>=n-p:
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from bisect import bisect_right

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

a = sorted(a)

sm = sum(a[:n-p+1])
mxcnt = a[:n-p+1].count(a[n-p])
mx = a[n-p]

if v <= p:
    k = bisect_left(a,mx - m)
    print(n-k)
    exit()

for k in range(n-p):
    sm -= a[k]
    if (n-p-k)*(a[k]+m) - sm < max((v-k-p),0)*m:
        continue
    elif v-k-p <= 0:
        if a[k]+m >= mx:
            print(n-k)
            exit()
        else:
            continue
    else:
        if v-k-p <= 0:
            if a[k]+m >= mx:
                kk = bisect_left(a,a[k])
                print(n-kk)
                exit()
        else:
            if a[k]+m >= (sm + (v-k-p)*m)/(n-k-p):
                print(n-k)
                exit()

print(p)

Submission status: Wrong Answer

Solution code:
N,M,V,P = map(int,input().split(' '))
An = list(map(int,input().split(' ')))
An.sort(reverse=True)
threadhold_list = [0] * N
theard_num = An[P-1]
theard_idx = 0
sum_An = [0] * N
sum_An[0] = An[0]
for idx,val in enumerate(An):
    if theard_num == val:
        theard_idx = idx
    threadhold_list[idx] = max(0,theard_num-val)
for idx in range(1,N):
    sum_An[idx] = sum_An[idx-1] + An[idx]

#print(An)
#print(threadhold_list)
#print(sum_An)

min_num = 10**10
min_goal_num = 10**10
must_allocation_num = V- (P - 1)
for idx,val in enumerate(An):
    if idx + 1 <= P:
        min_num = min(min_num,val)
        min_goal_num = min(min_goal_num,val)
        continue
    if val == min_num:
        continue
    if threadhold_list[idx] <= M and must_allocation_num <= (N-idx):
        min_num = min(min_num,val)
        continue
    if M < threadhold_list[idx]:
        break 
    if (N-idx) < must_allocation_num:
        sum_foword_nums = sum_An[idx-1] - sum_An[(P - 1-1)]
        allocation_diff =  must_allocation_num - (N-idx)
        diveide_allocation_num = (idx-(P - 1))
        plus_sum = (allocation_diff*M)
        last_num = (sum_foword_nums + plus_sum)/diveide_allocation_num
        candidate_num = An[idx] + M
        if last_num <= candidate_num:
            min_num = min(min_num,val)
            continue


        sum_foword_nums = sum_An[idx-1]
        allocation_diff = P - (N-idx)
        plus_sum = (allocation_diff*M)
        last_num = (sum_foword_nums + plus_sum)/diveide_allocation_num
        candidate_num = An[idx] + M
        if last_num <= candidate_num:
            min_num = min(min_num,val)
            continue
    break
print(idx)

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)
b = list(reversed(a))

def ok(i):
  if i < p:
    return True
  else:
    can = a[i]+m
    c = bisect.bisect_right(b,a[i])
    must = m*(v-(p-1+c))
    for j in range(p-1,n-c):    
      must -= min(can-a[j] ,m)
    if must > 0:
      return False
    else:
      return True
    
L = -1
U = n
while U-L > 1:
  mid = (L+U)//2
  if ok(mid):
    L = mid
  else:
    U = mid

print(U)