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 = 0
        max_index = 0
        temp_sum =0
        for i in range(0, len(heights)):
            if max_value < heights[i]:
                max_value = heights[i]
                max_index = i
                temp_sum = 0
            temp_sum= temp_sum + heights[i]

        sum = sum + (len(heights) - max_index ) * max_value - temp_sum
        if max_index == 0 :
            break

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