#include #include #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INF 1<<30; int N; int A[1000]; int dp[1000]; _Bool exist[10001]; int cx[1000]; int main() { scanf("%d", &N); REP(i, N) scanf("%d", &A[i]), exist[A[i]] = 1; int cxs = 0; REP(i, 10001) if (exist[i]) cx[cxs++] = i; REP(i, cxs) dp[i] = abs(A[0] - cx[i]); FOR(i, 1, N) { int mn = INF; REP(j, cxs) { mn = mn > dp[j] ? dp[j] : mn; dp[j] = mn + abs(cx[j] - A[i]); } } int ans = INF; REP(i, cxs) ans = ans > dp[i] ? dp[i] : ans; printf("%d\n", ans); }