100 atcoder#ABC128D. [ABC128D] equeue
[ABC128D] equeue
Score : points
Problem Statement
Your friend gave you a dequeue as a birthday present.
is a horizontal cylinder that contains a row of jewels.
The values of the jewels are from left to right. There may be jewels with negative values.
In the beginning, you have no jewel in your hands.
You can perform at most operations on , chosen from the following, at most times (possibly zero):
- Operation A: Take out the leftmost jewel contained in and have it in your hand. You cannot do this operation when is empty.
- Operation B: Take out the rightmost jewel contained in and have it in your hand. You cannot do this operation when is empty.
- Operation C: Choose a jewel in your hands and insert it to the left end of . You cannot do this operation when you have no jewel in your hand.
- Operation D: Choose a jewel in your hands and insert it to the right end of . You cannot do this operation when you have no jewel in your hand.
Find the maximum possible sum of the values of jewels in your hands after the operations.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the maximum possible sum of the values of jewels in your hands after the operations.
6 4
-10 8 2 1 2 6
14
After the following sequence of operations, you have two jewels of values and in your hands for a total of , which is the maximum result.
- Do operation A. You take out the jewel of value from the left end of .
- Do operation B. You take out the jewel of value from the right end of .
- Do operation A. You take out the jewel of value from the left end of .
- Do operation D. You insert the jewel of value to the right end of .
6 4
-6 -100 50 -2 -5 -3
44
6 3
-6 -100 50 -2 -5 -3
0
It is optimal to do no operation.