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

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)

    while True:
        a_max = max(a_list[start_i:end_i])
        # print("a_max", a_max)

        max_index = a_list[start_i:end_i].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]))
        if len(a_list[start_i:end_i]) == 0:
            print(result)
            break
        # break

else:
    print(0)
