import sys input=sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) mod=10**9+7 def main(): N=I() Y=LI() inf=10**10 M=10**4 dp=[[inf]*(M+1)for _ in range(N+1)] #dp[i][j]はi個まで見て,最後の高さがjの時の時の最小値 #dp[i][j]はdp[i-1][0~j]のmin+cost dp[0][0]=0 for i in range(N): Min=[inf]*(M+1) Min[0]=dp[i][0] for j in range(M): Min[j+1]=min(Min[j],dp[i][j+1]) for j in range(M): dp[i+1][j]=Min[j]+abs(Y[i]-j) ans=inf for j in range(M+1): ans=min(ans,dp[-1][j]) print(ans) main()