using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;
		int[] a=sc.Ia;
		long ans=0;
		int[][] b=new int[s[1]+1][];
		int[][] c=new int[s[1]+1][];
		for(int i = 0;i<=s[1];i++) {
			b[i]=new int[s[0]+1];
			var pq=new Pq<int>(s[1],false);
			for(int j = 0;j<i;j++) {pq.Push(0);}
			for(int j = 0;j<s[0];j++) {
				int p=a[j];
				if(i!=0&&a[j]<0&&a[j]<pq.Top){
					p=pq.Top;
					pq.Pop();
					pq.Push(a[j]);
				}
				b[i][j]=(j!=0?b[i][j-1]:0)+p;
			}
		}
		for(int i = 0;i<=s[1];i++) {
			c[i]=new int[s[0]+1];
			var pq=new Pq<int>(s[1],false);
			for(int j = 0;j<i;j++) {pq.Push(0);}
			for(int j = s[0]-1;j>0;j--) {
				int p=a[j];
				if(i!=0&&a[j]<0&&a[j]<pq.Top){
					p=pq.Top;
					pq.Pop();
					pq.Push(a[j]);
				}
				c[i][j]=(j!=s[0]-1?c[i][j+1]:0)+p;
			}
		}
		for(int i = Math.Min(s[1],s[0]);i>0;i--) {
			for(int j = i;j>=0;j--) {
				for(int k = 0;k<=(s[1]-i);k++) {
					long q=(j==0?0:b[k][j-1])+c[(s[1]-i)-k][s[0]-(s[1]-j)+(s[1]-i)];
					ans=Math.Max(ans,q);
				}
			}
		}
		Console.WriteLine("{0}",ans);
	}
}
public class Mo<T>:IComparable{
	public int n;
	public T d;
	public Mo(int n,T d){this.n=n;this.d=d;}
	public int CompareTo(object obj){
		Mo<T> mymo=(Mo<T>)obj;
		if(mymo.n>n){return -1;}
		else if(mymo.n<n){return 1;}
		else{return 0;}
	}
}
public class Pq<T> where T:IComparable{
	private T[] he;
	public int cnt=0,max=0;
	private Func<T,T,int> compare;
	public Pq(int max,bool mm){
		this.max=max;
		he=new T[max];
		if(mm){compare=Ao;}
		else{compare=Do;}
	}
	public void Push(T x){
		if(cnt==max){Extend();}
		int j=cnt;
		while(j!=0&&compare(x,he[(j-1)>>1])>0){he[j]=he[(j-1)>>1];j=(j-1)>>1;}
		he[j]=x;
		cnt++;
	}
	public void Pop(){
		cnt--;
		T r=he[cnt];
		int j=0;
		while(true){
			if(j*2+1<cnt){
				if(compare(he[j*2+1],he[j*2+2])>0){j=j*2+1;}
				else{j=j*2+2;}
			}
			else if(j*2<cnt){j=j*2+1;}
			else{break;}
			if(compare(he[j],r)<=0){j=(j-1)>>1;break;}
			he[(j-1)>>1]=he[j];
		}
		he[j]=r;
	}
	private int Ao(T x,T y){return y.CompareTo(x);}
	private int Do(T x,T y){return x.CompareTo(y);}
	public T Top{get{return he[0];}}
	private void Extend(){
		T[] nhe=new T[max<<1];
		Array.Copy(he,nhe,max);
		he=nhe;
		max<<=1;
	}
}

public class Sc{
	private string[] a;
	private int n=-1,m=0;
	public int I{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return int.Parse(a[n]);}}
	public long L{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return long.Parse(a[n]);}}
	public double D{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return double.Parse(a[n]);}}
	public string S{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return a[n];}}
	public string Sz{get{return Console.ReadLine();}}
	public int[] Ia{get{if(n!=-1&&n!=a.Length-1){m=n;n=-1;return Array.ConvertAll(a.Skip(m+1).ToArray(),int.Parse);}return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public int[] Ia2(int b){return Array.ConvertAll((((b&1)!=0?"0 ":"")+Console.ReadLine()+((b&2)!=0?" 0":"")).Split(),int.Parse);}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public string[] Sa2{get{return (Console.ReadLine()+" 0").Split();}}
}


Measured memory_bytes: 13396