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

tall = a_L[0]
ans = 0
diff = 0
for i in range(1,len(a_L)):
    if tall < a_L[i]:
        diff = abs(tall - a_L[i])
        tall = a_L[i]
    else:
        tall = a_L[i]
        diff = 0
    ans += diff
print(ans)