Contiguous Sum 2

Find the maximum contiguous subarray sum after optionally deleting at most one element from the sequence.

Medium5Dynamic programmingArrayInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a sequence of nn integers. Choose one contiguous block of the sequence and add up the numbers in it. Find the largest sum you can get. The block must contain at least one number.

You may remove one number from the sequence. You may also remove nothing. After a removal the remaining numbers close up in order into a new sequence, and the block is chosen from that new sequence.

For example, take the sequence 10, -4, 3, 1, 5, 6, -35, 12, 21, -1. If you remove nothing, the largest sum is 12+21, which is 33. If you remove -35, the sequence becomes 10, -4, 3, 1, 5, 6, 12, 21, -1, and the largest sum is 10-4+3+1+5+6+12+21, which is 54.

Input

The first line contains an integer nn (1n1000001 \le n \le 100000).

The second line contains the nn integers of the sequence, separated by spaces. Each number aia_i satisfies 1000ai1000-1000 \le a_i \le 1000.

Output

Print the largest sum on the first line.