def abc176_c():
    n = int(input())
    a = list(map(int, input().split(' ')))
    ans = 0
    current_h = 0

    # 前の人が大きい時はa[i-1] - a[i]分の台が必要
    for i in range(1, len(a)):
        if i == 1:
            current_h = a[i]

        if a[i - 1] > a[i]:
            current_h = max(a[i - 1], current_h)
            ans += current_h - a[i]

    print(ans)

if __name__ == '__main__':
    abc176_c()
