let n, k = Scanf.scanf "%d %d" (fun n k -> n, k)
let v = Array.init n (fun _ -> Scanf.scanf " %d" (fun i -> i))

let lm = min k n

let rec f l ans =
  if l = lm + 1 then ans
  else
    let rm = lm - l in
    let rec g r ans =
      if r = rm + 1 then ans
      else
        let rec h i j (s, q) =
          if i = j then s, q
          else
            let x = v.(i) in
            if x < 0 then
              h (i + 1) j (x + s, x :: q)
            else
              h (i + 1) j (x + s, q) in
        let s, q = h (n - r) n (h 0 l (0, [])) in
        let rec h i s = function
            e :: q ->
              if i = k - l - r then s
              else h (i + 1) (s - e) q
          | _ -> s in
        let s = h 0 s (List.sort (fun i j -> i - j) q) in
        g (r + 1) (max s ans) in
    f (l + 1) (g 0 ans)

let () = Printf.printf "%d\n"  (f 0 0)
