# Problem C - Step

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
ans = 0

# calc
tmp_cost = a_nums[0]
for i in range(N):
    if tmp_cost-a_nums[i]>0:
        ans += tmp_cost - a_nums[i]
    else:
        tmp_cost = a_nums[i]

# output
print(ans)

1d6ccd1a8e411c118c451a1fb3e6ed3750c92ac3
<mask token>

<mask token>
for i in range(N):
    if tmp_cost - a_nums[i] > 0:
        ans += tmp_cost - a_nums[i]
    else:
        tmp_cost = a_nums[i]
print(ans)

N = int(input())
a_nums = list(map(int, input().split()))
ans = 0
tmp_cost = a_nums[0]
for i in range(N):
    if tmp_cost - a_nums[i] > 0:
        ans += tmp_cost - a_nums[i]
    else:
        tmp_cost = a_nums[i]
print(ans)

# Problem C - Step

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
ans = 0

# calc
tmp_cost = a_nums[0]
for i in range(N):
    if tmp_cost-a_nums[i]>0:
        ans += tmp_cost - a_nums[i]
    else:
        tmp_cost = a_nums[i]

# output
print(ans)
