def main():
#    N, X, T = map(int, input().split())
    N = input()
    heights = [int(s) for s in input().split()]

    sum = 0
    while True:
        max_value = max(heights)
        max_index = heights.index(max_value)

        for i in range(max_index,len(heights)):
            sum = sum + max_value - heights[i]

        if max_index == 0 :
            break

        heights = heights[0:max_index]
    print(sum)
main()
