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

ans = 0
maxV = max(list)
maxI = list.index(maxV)

sumV = sum(list[maxI:len(list)])
maxM = maxV * (len(list)-maxI)
# print(sumV, maxM)
ans += maxM - sumV

if maxI != 0:
    for i in range(0,maxI):
        if i != maxI:
            x = list[i] - list[i+1]
            if x > 0:
                ans += x
print(ans)
