N=int(input()) A=list(map(int, input().split())) D=[0]*N for i in range(N): D[A[i]-1]=i+1 d=sum(A) ans=0 for i in range(N): e=d-2*D[-1-i] ans=max(ans,e) d-=D[-1-i] print(ans)