def main():
  x = 0
  n = int(input())
  a = list(map(int,(input().split())))
  for i in range(n):
#    print(i)
    if a[i:i+1] < a[i+1:i+2]:
#      print(a[i:i+1],a[i+1:i+2])
      a1 = a[i:i+1][0]
      a2 = a[i+1:i+2][0]
      x +=(a2-a1)
  print(x)
  
if __name__ == '__main__':
  main()