Buggy python submission (verdict: Time Limit Exceeded):
def compare(a, A):
    R=0
    for i in range(0, len(A)):
        if(a==A[i]):
            R+=1
        elif(a<A[i]):
            break
    return R

def result(R,a):
    if(R==a):
        return 0
    elif(a<R):
        return R-a
    return R

N = int(input())

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

R=0
C=compare(a[0],a)
R+=result(C,a[0])
T=C

for i in range(1, N):
    if(a[i] != a[i-1]):
        C=compare(a[i],a[T:])
        R+=result(C,a[i])
        T+=C


print(R)


Fixed submission:
N=int(input())
a = list(map(int, input().split()))

a.sort()
a.append(0)

T=[]
j=0
t=0
s=0
for i in range(N):
    j+=1
    if a[i] != a[i+1]:
        if j-s > a[i]:
            t+=(j-s)-a[i]
        elif j-s < a[i]:
            t += j-s
        s=j

print(t)

Buggy python submission (verdict: Time Limit Exceeded):
def compare(a, A):
    R=0
    for i in range(0, len(A)):
        if(a==A[i]):
            R+=1
        elif(a<A[i]):
            break
    return R

def result(R,a):
    if(R==a):
        return 0
    elif(a<R):
        return R-a
    return R

N = int(input())

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

R=0
T=compare(a[0],a)
R+=result(T,a[0])

for i in range(1, N):
    if(a[i] != a[i-1]):
        C=compare(a[i],a[T:])
        R+=result(C,a[i])
        T+=C

print(R)


Fixed submission:
N=int(input())
a = list(map(int, input().split()))

a.sort()
a.append(0)

T=[]
j=0
t=0
s=0
for i in range(N):
    j+=1
    if a[i] != a[i+1]:
        if j-s > a[i]:
            t+=(j-s)-a[i]
        elif j-s < a[i]:
            t += j-s
        s=j

print(t)

Buggy python submission (verdict: Time Limit Exceeded):
def compare(a, A):
    R=0
    for i in range(0, len(A)):
        if(a==A[i]):
            R+=1
        elif(a<A[i]):
            break
    return R

def result(R,a):
    if(R==a):
        return 0
    elif(a<R):
        return R-a
    return R

N = int(input())

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

R=0
R+=result(compare(a[0],a),a[0])
T=compare(a[0],a)

for i in range(1, N):
    if(a[i] != a[i-1]):
        C=compare(a[i],a[T:])
        R+=result(C,a[i])
        T+=C


print(R)


Fixed submission:
N=int(input())
a = list(map(int, input().split()))

a.sort()
a.append(0)

T=[]
j=0
t=0
s=0
for i in range(N):
    j+=1
    if a[i] != a[i+1]:
        if j-s > a[i]:
            t+=(j-s)-a[i]
        elif j-s < a[i]:
            t += j-s
        s=j

print(t)

Buggy python submission (verdict: Time Limit Exceeded):
def compare(a, A):
    R=0
    for i in range(0, len(A)):
        if(a==A[i]):
            R+=1
        elif(a<A[i]):
            break
    return R

def result(R,a):
    if(R==a):
        return 0
    elif(a<R):
        return R-a
    return R

N = int(input())

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

R=0
R+=result(compare(a[0],a),a[0])
T=compare(a[0],a)

for i in range(1, N):
    if(a[i] != a[i-1]):
        R+=result(compare(a[i],a[T:]),a[i])
        T+=compare(a[i],a)


print(R)


Fixed submission:
N=int(input())
a = list(map(int, input().split()))

a.sort()
a.append(0)

T=[]
j=0
t=0
s=0
for i in range(N):
    j+=1
    if a[i] != a[i+1]:
        if j-s > a[i]:
            t+=(j-s)-a[i]
        elif j-s < a[i]:
            t += j-s
        s=j

print(t)

Buggy python submission (verdict: Time Limit Exceeded):
def compare(a, A):
    R=0
    for i in range(0, len(A)):
        if(a==A[i]):
            R+=1
        elif(a<A[i]):
            break
    return R

def result(R,a):
    if(R==a):
        return 0
    elif(a<R):
        return R-a
    return R

N = int(input())

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

R=0
T=1
R+=result(compare(a[0],a),a[0])

for i in range(1, N):
    if(a[i] != a[i-1]):
        R+=result(compare(a[i],a[T:]),a[i])
        T+=compare(a[i],a)


print(R)


Fixed submission:
N=int(input())
a = list(map(int, input().split()))

a.sort()
a.append(0)

T=[]
j=0
t=0
s=0
for i in range(N):
    j+=1
    if a[i] != a[i+1]:
        if j-s > a[i]:
            t+=(j-s)-a[i]
        elif j-s < a[i]:
            t += j-s
        s=j

print(t)

Buggy python submission (verdict: Time Limit Exceeded):
def compare(a, A):
    R=0
    for i in range(0, len(A)):
        if(a==A[i]):
            R+=1

    if(R==a):
        return 0
    elif(a<R):
        return R-a
    return R

N = int(input())

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

R=0
R+=compare(a[0],a)

for i in range(1, N):
    if(a[i] != a[i-1]):
        R+=compare(a[i],a)

print(R)

Fixed submission:
N=int(input())
a = list(map(int, input().split()))

a.sort()
a.append(0)

T=[]
j=0
t=0
s=0
for i in range(N):
    j+=1
    if a[i] != a[i+1]:
        if j-s > a[i]:
            t+=(j-s)-a[i]
        elif j-s < a[i]:
            t += j-s
        s=j

print(t)