N = int(input())
A = list(map(int,input().split()))
ans = 0
def seek(LIST:list):
    global ans
    max_num = max(LIST)
    index = LIST.index(max(LIST))
    if(index == 0):
        for i in LIST[index:]:
            ans += max_num-i
    else:
        for i in LIST[index:]:
            ans += max_num-i
        new_list = LIST[0:index]
        seek(new_list)
seek(A)
print(ans)
