N=int(input())
A=list(map(int,input().split()))
c=1000000000
for i in range(len(A)):
  base=A[i]
  cc=0
  p=8
  for t in range(0, i):
    if base<A[t]:
      p=6
      break
  if p==6:
    continue
  for t in range(i, len(A)):
    if base>A[t]:
      cc+=base-A[t]
  if c > cc:
    c = cc
print(c)