def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(0, n-1):
        if a[i] < a[i+1]:
            step = a[i+1] - a[i]
            a[i] = a[i] + step
            cnt = cnt+step

    print(cnt)
main()