N = int(input())
A = list(map(int,input().split()))

keep = 0
cnt =0
cnt2=0


for a in A:
    if keep > a:
        cnt += keep -a
        cnt2=0
        cnt2 += keep -a
    keep = a + cnt2
    
print(cnt)