output_description: Print the maximum possible sum of the values of jewels in your hands after the
  operations.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N K
      V_1 V_2 ... V_N
attempt: import unittest
  
  
  class TestD(unittest.TestCase):
      def test_1(self):
          self.assertEqual(think(4, [-10, 8, 2, 1, 2, 6]), 14)
  
      def test_2(self):
          self.assertEqual(think(4, [-6, -100, 50, -2, -5, -3]), 44)
  
      def test_3(self):
          self.assertEqual(think(3, [-6, -100, 50, -2, -5, -3]), 0)
  
  
  def solve():
      k, v = read()
      result = think(k, v)
      write(result)
  
  
  def read():
      n, k = read_int(2)
      return k, read_int(n)
  
  
  def read_int(n):
      return read_type(int, n)
  
  
  def read_float(n):
      return read_type(float, n)
  
  
  def read_type(t, n):
      return list(map(lambda x: t(x), read_line().split(" ")))[:n]
  
  
  def read_line(n=0):
      if n == 0:
          return input().rstrip()
      else:
          return input().rstrip()[:n]
  
  
  def think(k, v):
      max_value = -(10**10)
  
      max_dequeue = min(len(v), k)
      for dequeue in range(max_dequeue + 1):
          max_enqueue = min(k - dequeue, dequeue)
          for left in range(dequeue + 1):
              dequeued_v = []
              right = dequeue - left
              dequeued_v += v[0:left]
              dequeued_v += [] if right == 0 else v[-right:]
              dequeued_v.sort()
              for i in range(max_enqueue + 1):
                  max_value = max(max_value, sum(dequeued_v[i:]))
      return max_value
  
  
  def write(result):
      print(result)
  
  
  if __name__ == "__main__":
      # unittest.main()
      solve()
  
problem_description: Statement
  
  Your friend gave you a dequeue D as a birthday present.
  
  D is a horizontal cylinder that contains a row of N jewels.
  
  The _values_ of the jewels are V_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 K operations on D, chosen from the following, at most
  K times (possibly zero):
  
    * Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.
  
    * Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.
  
    * Operation C: Choose a jewel in your hands and insert it to the left end of D. 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 D. 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.
samples: [{"input": "6 4\n    -10 8 2 1 2 6", "output": "14\n    \n\nAfter the following sequence of operations, you have two jewels of values 8\nand 6 in your hands for a total of 14, which is the maximum result.\n\n  * Do operation A. You take out the jewel of value -10 from the left end of D.\n  * Do operation B. You take out the jewel of value 6 from the right end of D.\n  * Do operation A. You take out the jewel of value 8 from the left end of D.\n  * Do operation D. You insert the jewel of value -10 to the right end of D.\n\n* * *"}, {"input": "6 4\n    -6 -100 50 -2 -5 -3", "output": "44\n    \n\n* * *"}, {"input": "6 3\n    -6 -100 50 -2 -5 -3", "output": "0\n    \n\nIt is optimal to do no operation."}]