Business Magic
InterviewTime limit2sMemory limit1024 MB
Pick one interval to double, and negate each negative store outside it, to maximize the total profit for next month.
- Level
Medium5 of 10
- Topics
- Array, Dynamic programming
- Solved
- No attempts yet
Problem
There are stores along a street, numbered from to from nearest to farthest. Last month, store had a net profit of . If is positive, it is a profit of dollars. If is negative, it is a loss of dollars.
You have two kinds of spells that change the net profits for next month.
- Blue Magic: Choose one continuous interval . The net profit of every store from to (inclusive) is doubled. That is, if , store has net profit next month.
- Green Magic: Choose any store and cast the spell on it. The store's net profit for next month becomes the negative of its net profit last month.
A store that is not affected by either spell has the same net profit next month as last month.
There are restrictions. You can cast Blue Magic only once, and it must come before Green Magic. Green Magic cannot be cast on any store already affected by Blue Magic. Find the maximum possible total net profit of all stores for next month after casting the spells optimally.
Input
The first line contains , the number of stores. The second line contains space-separated integers , where is the net profit of store last month.
Output
Output a single integer, the maximum possible total net profit of all stores for next month.
Constraints
- for