N = int(input()) Y = list(map(int, input().split())) a = sorted(set(Y)) dp = [[0] * 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): t = float('inf') for j in range(len(a)): t = min(t, dp[i - 1][j]) dp[i][j] = t + abs(Y[i] - a[j]) print(min(dp[N - 1]))