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

maxi = max(A)
index_maxi = A.index(maxi)

sum = 0
if index_maxi != 0:
    mini_max = max(list(A[:index_maxi]))

    for i in A[index_maxi:]:
        sum += (maxi - i)

    for j in A[:index_maxi]:
        sum += (mini_max - j)

print(sum)