from copy import copy N=int(input()) Y=[0]+list(map(int,input().split()))+[10**10] Z=copy(Y) for i in range(N+1): if Z[i]>Z[i+1]: while Z[i]>Z[i+1]: Z[i+1]+=1 if Z[i-1]<Z[i]: Z[i]-=1 C=0 for i in range(1,N+1): C+=abs(Y[i]-Z[i]) print(C)