INF = float('inf') N = int(input()) Y = list(map(int, input().split())) a = sorted(set(Y)) maxY = max(Y) dp = [[INF] * len(a) for _ in range(N)] for i in range(len(a)): dp[0][i] = abs(Y[0] - a[i]) for i in range(1, N): for j in range(len(a)): for k in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(Y[i] - a[j])) print(min(dp[N - 1]))