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

cnt = 0
for i in range(1,5):
    #print(cnt)
    if a[i] < a[i-1]:
        cnt = cnt + (a[i-1] - a[i])
        a[i] = a[i-1]
        #print(cnt)
print(cnt)