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

ans = 0

for i in range(1,n):
    print(a[i-1], a[i])
    if a[i-1] <= a[i]:
        continue
    else:
        ans += a[i-1] - a[i]
        a[i] = a[i-1]

print(ans)