N = int(input()) Y = [int(zz) for zz in input().split()] ans = 0 for i in range(1,N): if Y[i] < Y[i-1]: left = 0 for j in range(i): left += max(Y[j]-Y[i],0) right = 0 for j in range(i,N): right += max(Y[i]-Y[j],0) if left < right: ans += Y[i-1]-Y[i] Y[i-1] = Y[i] else: ans += Y[i-1]-Y[i] Y[i] = Y[i-1] print(ans)