#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector veci; typedef vector vecs; template using Hash=unordered_map; #define REP(i, a, n) for(ll i = a; i < n; i++) #define RREP(i, a, n) for(ll i = n-1; i >= a; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define MD 1000000007 template T read(){T a;cin >> a;return a;} template void read(T& a){cin >> a;} template void rarr(T a, int n){for(int i = 0; i < n; i++) {cin >> a[i];}} template void write(T a){cout << a << endl;} template void writes(vector a, char* c = " "){cout << a[0];for(int i = 1; i < a.size(); i++)cout << c << a[i];cout << endl;;} void write(double a){cout << fixed << setprecision(12) << a << endl;} template void warr(T a, int n, char* c = " "){cout << a[0];for(int i = 1; i < n; i++)cout << c << a[i];cout << endl;} ll gcd(ll a, ll b){while(true){ll k = a % b;if(k == 0)return b;a = b;b = k;}} ll comb(ll n, ll m){ll p=1;m=min(m,n-m);for(ll i=1;i<=m;i++){p*=n-i+1;p/=i;}return p;} int n,m,w[4000]; int dp[3010][3010][2][2]={0}; const int MA=10000*10000; int solve(int i,int gc,bool g,const bool& fg){ if(i>=n||gc==m){ if(i>=n&&gc==m)return (g&&fg)?w[n-1]:0; else if(gc==m)return 0; else return -MA; } if(m-gc>n-i)return -MA; if(dp[i][gc][g][fg]=m){ res=solve(i+1,gc,false,fg); } else{ res=solve(i+1,gc,false,fg); res=max(res,solve(i+1,gc+1,true,fg)+w[i-1]*g); } return dp[i][gc][g][fg]=res; } int main(void) { cin>>n>>m; rep(i,n)rep(j,n)rep(k,2)dp[i][j][k][0]=dp[i][j][k][1]=MA; rep(i,n)read(w[i]); int res=solve(1,0,false,false); res=max(res,solve(1,1,true,true)); write(res); return 0; }