import sys input=lambda:sys.stdin.readline().rstrip() N=int(input()) Y=list(map(int,input().split())) dp=[[0 for i in range(10001)] for j in range(N)] for i in range(10001): dp[0][i]=max(0,Y[0]-i) for i in range(1,N): dp[i][0]=dp[i-1][0]+Y[i] for j in range(1,10001): dp[i][j]=min(dp[i][j-1],abs(Y[i]-j)+dp[i-1][j]) print(dp[N-1][10000])