#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++) #define ALL(x) (x).begin(), (x).end() const double PI = acos(-1); int n; int y[1000]; int dp[1000][10000+1]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; REP (i, n) cin >> y[i]; for (int h = 0; h <= 10000; h++) dp[0][h] = abs(h - y[0]); for (int i = 1; i < n; i++) { for (int h = 0; h <= 10000; h++) { dp[i][h] = dp[i-1][h] + abs(h - y[i]); if (h > 0) { if (y[i] > h-1) dp[i][h] = min(dp[i][h], dp[i][h-1] - 1); else dp[i][h] = min(dp[i][h], dp[i][h-1] + 1); } } } int ret = 1<<30; for (int h = 0; h <= 10000; h++) ret = min(ret, dp[n-1][h]); cout << ret << endl; return 0; }