CSES - Shared codeLink to this code: https://cses.fi/paste/b08757275bc1f3fcab886/
#4
n=int(input())
lst=list(map(int,input().split()))
count=0
for i in range(1,n):
    t=lst[i]-lst[i-1]
    if t<0:
        count+=(-1*t)
        lst[i]=lst[i-1]
print(count)