n = int(input())
l=[]
rez=0
buf=0
a=list(map(int, input().split()))
for i in range(max(a)):
    l.append(0)
for i in a:
    l[i-1]+=1
l=l[::-1]
for i in range(max(a)):
    if [i]!=0:
        buf=l[i]
        rez+=l[i]*(max(a)-i)
        l[i]=0
        for j in range(buf):
            l.insert(0,0)
            l.pop
print(rez)

                                                 

