from sys import stdin def main(): #入力 readline=stdin.readline n=int(readline()) y=list(map(int,readline().split())) i=0 ans=0 while iy[i+1]: ans+=y[i]-y[i+1] y[i]=y[i+1] elif 0y[i]: new_y=y[i-1] ans+=abs(new_y-y[i]) y[i]=new_y elif y[i]>y[i+1]: new_y=max(y[i-1],y[i+1]) ans+=abs(new_y-y[i]) y[i]=new_y elif i==n-1: if y[i-1]>y[i]: new_y=y[i-1] ans+=abs(new_y-y[i]) y[i]=new_y i+=1 print(ans) if __name__=="__main__": main()