n = int(input()) Q = list(map(int, input().split())) if n < 2: print(0) else: max_e = Q[0] * Q[1] dp_prev = max_e max_pos = -float('inf') max_pos_val = 0 min_neg = float('inf') min_neg_val = 0 if Q[1] > 0: max_pos = Q[1] max_pos_val = max(dp_prev, 0) else: min_neg = Q[1] min_neg_val = max(dp_prev, 0) for i in range(2, n): current_q = Q[i] candidate_prev = current_q * Q[i-1] candidate_cont = dp_prev + candidate_prev if current_q > 0: candidate = current_q * max_pos + max_pos_val if max_pos != -float('inf') else -float('inf') else: candidate = current_q * min_neg + min_neg_val if min_neg != float('inf') else -float('inf') current_dp = max(candidate, candidate_cont, candidate_prev) max_e = max(max_e, current_dp) max_contrib = max(current_dp, 0) if current_q > max_pos or (current_q == max_pos and max_contrib > max_pos_val): max_pos = current_q max_pos_val = max_contrib if current_q < min_neg or (current_q == min_neg and max_contrib > min_neg_val): min_neg = current_q min_neg_val = max_contrib dp_prev = current_dp print(max_e)