from sortedcontainers import SortedList from itertools import accumulate N=int(input()) A=list(map(int, input().split())) to_right=list(accumulate(A)) to_left=list(accumulate(A[::-1]))[::-1] if N == 2: print(A[0]*A[1]) exit() left=SortedList([]) right=SortedList([]) mem1=[A[0]] mem2=[A[0]] rmem1=[A[-1]] rmem2=[A[-1]] left.add(A[0]) right.add(A[-1]) for i in range(1,N-1): mem1.append(max(to_right[i]-left[0], to_right[i])) mem2.append(min(to_right[i]-left[-1],to_right[i])) left.add(to_right[i]) for i in range(-2,-N,-1): rmem1.append(max(to_left[i]-right[0], to_left[i])) rmem2.append(min(to_left[i]-right[-1], to_left[i])) right.add(to_left[i]) to_rightmax = list(accumulate(mem1, lambda x,y: max(x,y))) to_rightmin = list(accumulate(mem2, lambda x,y: min(x,y))) to_leftmax = list(accumulate(rmem1, lambda x,y: max(x,y)))[::-1] to_leftmin = list(accumulate(rmem2, lambda x,y: min(x,y)))[::-1] ans=0 for i in range(N-1): ans = max(ans, to_rightmax[i]*to_leftmax[i]) ans = max(ans, to_rightmax[i]*to_leftmin[i]) ans = max(ans, to_rightmin[i]*to_leftmax[i]) ans = max(ans, to_rightmin[i]*to_leftmin[i]) print(ans)