using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    void Run()
    {
        int MIN = -100000000;
        var sc = new Scanner();
        int n = sc.NextInt();
        int k = sc.NextInt();
        int[] v = new int[n];
        var l = new LinkedList<int>();
        int neg = 0;
        for (int i = 0; i < n; i++)
        {
            int w = sc.NextInt();
            v[i] = w;
            l.AddLast(w);
            if (w < 0) neg++;
        }

        //if (n < k)
        //{
        //    int s = 0;
        //    for (int i = 0; i < n; i++) s += v[i];
        //    Array.Sort(v);
        //    for (int i = 0; v[i] < 0 && i < k - n; i++) s -= v[i];
        //    Console.WriteLine(s);
        //    return;
        //}

        var cl = new int[k + 1, k + 2]; // 左からi番目まで取るとき, j回操作できるときの最高価値
        var cr = new int[k + 1, k + 2]; // 右からi番目まで取るとき, j回操作できるときの最高価値
        for (int i = 1; i < k + 1; i++)
            for (int j = 0; j < k + 2; j++)
            {
                cl[i, j] = MIN;
                cr[i, j] = MIN;
            }

        var c = new List<int>();
        int sum = 0;
        for (int i = 0; i < Math.Min(k, n); i++)
        {
            sum += v[i];
            c.Add(v[i]);
            c.Sort();
            cl[i + 1, i + 1] = sum;
            int s = sum;
            for (int j = 0; j < k - i; j++)
            {
                if (j == i + 1 || c[j] >= 0) break;
                s -= c[j];
                cl[i + 1, i + j + 2] = s;
            }
        }

        c.Clear();
        sum = 0;
        for (int i = 0; i < Math.Min(k, n); i++)
        {
            sum += v[n - i - 1];
            c.Add(v[n - i - 1]);
            c.Sort();
            cr[i + 1, i + 1] = sum;
            int s = sum;
            for (int j = 0; j < k - i; j++)
            {
                if (j == i + 1 || c[j] >= 0) break;
                s -= c[j];
                cr[i + 1, i + j + 2] = s;
            }
        }

        int[] left = new int[k + 1];
        int[] right = new int[k + 1];
        for (int i = 0; i < k + 1; i++)
        {
            int rmax = MIN;
            int lmax = MIN;
            for (int j = 0; j < k + 1; j++)
            {
                if (cl[j, i] > lmax) lmax = cl[j, i];
                if (cr[j, i] > rmax) rmax = cr[j, i];
            }
            left[i] = lmax;
            right[i] = rmax;
        }
        int res = MIN;
        int e = MIN;
        for (int i = 0; i < k + 1; i++)
            for (int j = 0; j < k + 1; j++)
            {
                if (left[i] > e) e = left[i];
                if (right[j] > e) e = right[j];
                if (i + j > k) continue;
                if (left[i] + right[j] > res) res = left[i] + right[j];
            }
        Console.WriteLine(n <= k ? e : res);
    }

    static void Main(string[] args)
    {
        new Program().Run();
    }

    static long Gcd(long a, long b)

    {
        if (a < b) return Gcd(b, a);
        if (b == 0) return a;
        return Gcd(b, a % b);
    }
}

class Scanner
{
    int index = -1;
    string[] buf;

    public Scanner()
    {
    }

    public int NextInt()
    {
        CheckNext();
        return int.Parse(buf[index++]);
    }

    public long NextLong()
    {
        CheckNext();
        return long.Parse(buf[index++]);
    }

    public double NextDouble()
    {
        CheckNext();
        return double.Parse(buf[index++]);
    }

    public string NextString()
    {
        CheckNext();
        return buf[index++];
    }

    private void CheckNext()
    {
        if (index >= 0 && index < buf.Length) return;

        buf = Console.ReadLine().Split(' ');
        index = 0;
    }
}


Measured memory_bytes: 13300