Contiguous Sum 2
InterviewTime limit2sMemory limit512 MB
Find the maximum contiguous subarray sum after optionally deleting at most one element from the sequence.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
You are given a sequence of 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 ().
The second line contains the integers of the sequence, separated by spaces. Each number satisfies .
Output
Print the largest sum on the first line.