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

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

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

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)
