def run(x, a_list):
    '''

    '''
    if x == 1:
        print(0)
    else:
        out = 0
        for i,v in enumerate(a_list):
            if i == 0:
                continue
            if i == x-1:
                break
            out += 0 if v < a_list[i-1] else v - a_list[i-1]
            
        print(out)
            


    



if __name__ == '__main__':
    x = int(input())
    a_list = list(map(int, input().split()))
    run(x, a_list)