#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define All(v) (v).begin(), (v).end() #define pb push_back #define MP(a, b) make_pair((a), (b)) using ll = long long; using pii = pair; using pll = pair; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; template vector make_vec(size_t a) { return vector(a); } template auto make_vec(size_t a, Ts... ts) { return vector(ts...))>(a, make_vec(ts...)); } int main() { int N; cin >> N; vector Y(N); rep(i, N) cin >> Y[i]; auto dp = make_vec(N + 1, 10001); rep(i, N + 1) { rep(j, 10001) dp[i][j] = LINF; } dp[0][0] = 0; // dp[i][j]:=i-1個目まで見て、高さjの時の最小コスト for(int i = 0; i < N; i++) { ll mi = LINF; for(int j = 0; j < 10001; j++) { mi = min(mi, dp[i][j]); dp[i + 1][j] = min(dp[i + 1][j], mi + llabs(Y[i] - j)); } } ll res = LINF; rep(i, 10001) { res = min(dp[N][i], res); } cout << res << endl; return 0; }