using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;
using System.Runtime.InteropServices.ComTypes;
using System.Xml.Schema;
using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Linq.Expressions;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            //Out.Flush();
            Read();
        }

        static void Solve()
        {
            var (N, K) = Slice2<int>();
            var Vi = Sarray();
            var Kd = 2 * N - 1 + 1;
            var dp = new long[2, Kd, N + 1];//操作回数、i個目の宝石まで操作
            for (var i = 0; i < 2; ++i)
            {
                var lst = new List<long>();
                var sum = 0L;
                for (var n = 0; n < N; ++n)
                {
                    lst.Add(Vi[n]);
                    sum += Vi[n];
                    dp[i, n + 1, n + 1] = sum;
                    lst.Sort();
                    var cdt = sum;
                    for (var j = 1; j <= n; ++j)
                    {
                        if (Kd < n + 1 + j) break;
                        cdt -= lst[j - 1];
                        dp[i, n + 1 + j, n + 1] = cdt;
                    }
                }
                Array.Reverse(Vi);
            }
            for (var i = 0; i < 2; ++i)
                for (var k = 1; k < Kd; ++k)
                    for (var n = 1; n < N + 1; ++n)
                        dp[i, k, n] = Max(dp[i, k, n], Max(dp[i, k - 1, n], dp[i, k, n - 1]));
            var cnt = 0L;
            if (2 * N - 1 <= K) cnt = Vi.Where(v => 0 < v).Sum();
            var uLk = Min(2 * N - 1, K);
            for (var k = 0; k <= uLk; ++k)
            {
                var uLh = Min(uLk, N);
                for (var h = 0; h <= uLh; ++h)
                    cnt = Max(cnt, dp[0, k, h] + dp[1, uLk - k, uLh - h]);
            }
            WriteLine(cnt);
        }

        static long Mod = (long)1e9 + 7;
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static public (T1 a, T2 b) Slice<T1, T2>()
        {
            var t = ReadLine().Split();
            return (
            (T1)Convert.ChangeType(t[0], typeof(T1)),
            (T2)Convert.ChangeType(t[1], typeof(T2)));
        }
        static public (T a, T b) Slice2<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)));
        }
        static public (T a, T b, T c) Slice3<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)),
            (T)Convert.ChangeType(t[2], typeof(T)));
        }
        static public (T a, T b, T c, T d) Slice4<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)),
            (T)Convert.ChangeType(t[2], typeof(T)),
            (T)Convert.ChangeType(t[3], typeof(T)));
        }
    }
}

Measured memory_bytes: 28380