#K83757. Maximum Sum of Left and Right Maximums

    ID: 36268 Type: Default 1000ms 256MiB

Maximum Sum of Left and Right Maximums

Maximum Sum of Left and Right Maximums

You are given an array of N integers. For each index i (1-indexed), define:

\(LeftMax(i)=\begin{cases}\max\{a_1,a_2,\dots,a_{i-1}\} & \text{if } i > 1,\\ -1 & \text{if } i=1,\end{cases}\)

\(RightMax(i)=\begin{cases}\max\{a_{i+1},a_{i+2},\dots,a_{N}\} & \text{if } i < N,\\ -1 & \text{if } i=N.\end{cases}\)

Define \(MaxSum(i)=LeftMax(i)+RightMax(i)\). Your task is to compute the maximum value of \(MaxSum(i)\) for all valid indices i.

If no valid index exists to compute both maximums (for example, when N is 1), output -1.

inputFormat

The first line of input consists of a single integer N representing the number of elements.

The second line contains N space-separated integers representing the array.

outputFormat

Output a single integer which is the maximum value of \(MaxSum(i)\) computed over all indices.

## sample
6
2 4 6 1 3 7
13

</p>