"use strict";

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

require("fs");

!function () {
  "use strict";

  !function () {
    function splitNum(line) {
      return (Array.isArray(line) ? line : line.split(/\s+/)).map(function (n) {
        return parseInt(n);
      });
    }

    function* range(start, end) {
      for (var i = start; i < end; i++) {
        yield i;
      }
    }

    !function (input) {
      var lines = input.split("\n");

      var _splitNum = splitNum(lines[0]),
          _splitNum2 = _slicedToArray(_splitNum, 2),
          N = _splitNum2[0],
          K = _splitNum2[1];

      var V = splitNum(lines[1]).slice(0, N);
      var max = -1 / 0;
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = range(0, Math.min(K + 1, N + 1))[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var left = _step.value;
          var _iteratorNormalCompletion2 = true;
          var _didIteratorError2 = false;
          var _iteratorError2 = undefined;

          try {
            var _loop = function () {
              var right = _step2.value;
              var taken = 0 === right ? V.slice(0, left) : V.slice(0, left).concat(V.slice(-right));
              taken.sort(function (a, b) {
                return a - b;
              });
              var d = taken.findIndex(function (v, i) {
                return v >= 0 || i >= K - left - right;
              });
              var sum = taken.slice(d).reduce(function (a, b) {
                return a + b;
              }, 0);
              if (max < sum) max = sum;
            };

            for (var _iterator2 = range(0, Math.min(N + 1 - left, K + 1 - left))[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
              _loop();
            }
          } catch (err) {
            _didIteratorError2 = true;
            _iteratorError2 = err;
          } finally {
            try {
              if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
                _iterator2.return();
              }
            } finally {
              if (_didIteratorError2) {
                throw _iteratorError2;
              }
            }
          }
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return != null) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      console.log(max);
    }(require("fs").readFileSync("/dev/stdin", "utf8"));
  }();
}();


Measured memory_bytes: 22964