# import random
n = int(input())
a_list = list(map(int, input().split()))

# n = 2 * (10 ** 5)
# # a_list = [random.randrange(10 ** 9) for i in range(n)]
# a_list = [i for i in range(n)]
# a_list = a_list[:-1]

is_break = False
for i in range(len(a_list) - 1):
    if a_list[i] <= a_list[i + 1]:
        continue
    else:
        cur_i = i
        is_break = True
        break

if is_break:
    result = 0
    start_i = cur_i
    end_i = len(a_list)

    # print("start_i", start_i)
    # print("end_i", end_i)
    count = 0

    for i in range(len(a_list[start_i:end_i])):
        a_max = max(a_list[start_i:end_i])
        # print("a_max", a_max)

        max_index = a_list.index(max(a_list[start_i:end_i]))
        # print("max_index", max_index)

        max_sum = a_max * len(a_list[max_index:end_i])
        # print("max_sum", max_sum)

        a_sum = sum(a_list[max_index:end_i])
        # print("a_sum", a_sum)

        result += max_sum - a_sum

        end_i = max_index
        # print("len", len(a_list[start_i:end_i]))
        # print("")
        if len(a_list[start_i:end_i]) == 0:
            break

        # break
    print(result)

else:
    print(0)
