N = int(input()) Y = list(map(int,input().split())) miny = min(Y) Y = [i - miny for i in Y] INF = 10 ** 8 maxy = max(Y) dp = [INF] * (maxy + 1) dp[0] = 0 for i in range(N): dp2 = [INF] * (maxy + 1) for j in range(maxy + 1): for k in range(j,maxy + 1): dp2[k] = min(dp2[k], dp[j] + abs(Y[i] - k)) dp = dp2 print(min(dp))