# distutils: language=c++
# cython: language_level=3, boundscheck=False, wraparound=False
# cython: cdivision=True

import sys
input = sys.stdin.readline
import math

def INT(): return int(input())
def MAPINT(): return map(int, input().split())
def LMAPINT(): return list(map(int, input().split()))
f_inf = float('inf')


def main():

    N = INT()
    A = LMAPINT()
    
    count = 0
    for i in range(1, N):
      if max(A[:i]) > A[i]:
        count += (max(A[:i]) - A[i])

    print(count)

if __name__ == "__main__":
    main()
