N = int(input()) lsY = list(map(int,input().split())) dp = [0]*(10**4+1) inf = float('INF') for i in range(N): y = lsY[i] dp2 = [0]*(10**4+1) m = inf for j in range(10**4+1): m = min(m,dp[j]) dp2[j] = m + abs(j-y) dp = dp2 print(min(dp))