n = int(input())
H = list(map(int, input().split()))
mh = 0
cnt = 0
for i in range(n - 1):
    mh = max(mh, H[i])
    if H[i] <= H[i + 1]:
        continue
    else:
        if H[i + 1] < H[i]:
            cnt += mh - H[i + 1]
print(cnt)
