def solve():
    n = int(input())
    a = list(map(int, input().split()))
    mi = 0
    le = len(a)
    cost = 0
    for i in range(le-1):
        if a[i] > a[i+1]:
            mi = max(a[i], mi)
            cost += mi - a[i+1]
            print(mi - a[i+1], mi)
    cost += max(0, mi - a[-1])
    print(cost)
    return 0

if __name__ == "__main__":
    solve()