total = int(input())
tall = list(map(int, input().split()))
dif = 0
for i in range(total - 1):
    if tall[i+1] > tall[i]:
        var = tall[i+1] - tall[i]
        tall[i] += var
        dif += var
    else:
        pass
print(dif)
