N,*Y=map(int,open(0).read().split()) a=sorted({*Y}) t=[0]*len(a) for i in range(N): m=t[0] for j in range(len(a)): m=min(m,t[j]) t[j]=m+abs(Y[i]-a[j]) print(min(t))