n = int(input()) y = list(map(int,input().split())) ans = 0 dp = [[0]*(10**4 + 5) for i in range(n+1)] for i in range(1,n+1): dp[i][-1] = y[i-1] + dp[i-1][-1] for i in range(1,n+1): for j in range(10**4+1): dp[i][j] = min(abs(y[i-1]-j) + dp[i-1][j],dp[i][j-1]) print(dp[n][10**4])