100 atcoder#ABC128D. [ABC128D] equeue

[ABC128D] equeue

Score : 400400 points

Problem Statement

Your friend gave you a dequeue DD as a birthday present.

DD is a horizontal cylinder that contains a row of NN jewels.

The values of the jewels are V1,V2,...,VNV_1, V_2, ..., V_N 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 KK operations on DD, chosen from the following, at most KK times (possibly zero):

  • Operation A: Take out the leftmost jewel contained in DD and have it in your hand. You cannot do this operation when DD is empty.
  • Operation B: Take out the rightmost jewel contained in DD and have it in your hand. You cannot do this operation when DD is empty.
  • Operation C: Choose a jewel in your hands and insert it to the left end of DD. 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 DD. 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.
  • 1N501 \leq N \leq 50
  • 1K1001 \leq K \leq 100
  • 107Vi107-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

NN KK

V1V_1 V2V_2 ...... VNV_N

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 88 and 66 in your hands for a total of 1414, which is the maximum result.

  • Do operation A. You take out the jewel of value 10-10 from the left end of DD.
  • Do operation B. You take out the jewel of value 66 from the right end of DD.
  • Do operation A. You take out the jewel of value 88 from the left end of DD.
  • Do operation D. You insert the jewel of value 10-10 to the right end of DD.
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.