N = int(input()) a = list(map(int, input().split())) mn = min(a) for i in a: i -= mn mx = max(a) dp = [[0 for _ in range(mx+1)] for __ in range(N)] for i in range(mx+1): dp[0][i] = abs(a[0]-i) for i in range(1, N): mn = dp[i-1][0] for j in range(mx+1): mn = dp[i-1][j] if dp[i-1][j] < mn else mn dp[i][j] = mn + abs(a[i]-j) print(min(dp[-1]))